selenium利用Excel进行参数化(简单示例)

上篇搭建好环境后,正真开始我的自动化之旅了....

开始之前特别说明一下testNG的版本,不能直接使用Eclipse直接线上下载的版本,线上版本太高,不能兼容,运行程序会报以下错误,需要自行下载低一级的版本。

1、下载jexcelapi_2_6_12.tar.gz,在上篇的项目基础上创建com.testNG包,并且引入下载的jar包,用于Excel函数调用。

2、创建IData接口

复制代码
1 package com.testNg;
 2 /**
 3  * @author bjlijia
 4  * @version 1.0.0
 5  */
 6 public interface IData {
 7    
 8     public Object[][] getData(String caseName, String dataFile);
 9    
10     public Object[][] getData(String caseName, String dataFile,int colNum) ;
11    
12     public Object[][] getData(String caseName, String dataFile,int beginNum,int endNum) ;
13    
14 }
复制代码

3、创建类BaseExcelData

复制代码
package com.testNg;

import java.io.*;
import java.util.ArrayList;

import jxl.*;
import jxl.read.biff.BiffException;

/**
 * @author kaixie
 * @version 1.0.0
 */

public class BaseExcelData implements IData{
    String defaultPath = "D:/";
       
    public Object[][] getData(String caseName, String dataFile){
        return getData(caseName, dataFile, 0);
    }

    public Object[][] getData(String caseName, String dataFile, int rowNum)  {
        Object[][] data = null;
        try {
            data = addList(caseName, dataFile, rowNum);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data;
    }
   
    public Object[][] getData(String caseName, String dataFile, int beginRowNum, int endRowNum) {
        Object[][] data = null;            
                try {
                    data = addList2(caseName, dataFile, beginRowNum, endRowNum);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        return data;
    }
   
   
    private Object[][] addList(String caseName,String dataFile,int rowNum) throws FileNotFoundException{
          ArrayList<Object> list = new ArrayList<Object>();
            // 文件路径
            InputStream is = new FileInputStream(defaultPath + dataFile);
            Object[][] data = null;
            try {
                Workbook wb = Workbook.getWorkbook(is);
                Sheet rs = wb.getSheet(caseName);
                //获取表格总行数
                 int rsRows = rs.getRows();
                //获取表格总列数
                 int rsColumns = rs.getColumns();

                 if(rs!=null){                     
                         for (int i = 1; i <= rsRows-1; i++){
                                for (int j=0; j <= rsColumns-1; j++){
                                    Cell c = rs.getCell(j, i);
                                    String cz = c.getContents();
                                    list.add(cz);
                                }
                            }
                            //System.out.println(list);   
                    }
                 
                    if (rowNum <= 0 || rowNum >= rsRows) {
                         data = new Object[rsRows-1][rsColumns];
                        int k = -1;
                        for (int i = 0; i < rsRows-1; i++) {
                            for (int j = 0; j < rsColumns; j++) {
                                if (k < list.size())
                                    k++;
                                data[i][j] = list.get(k);
                                //System.out.println("i="+i+","+"j="+j+","+data[i][j]);
                            }
                        }
                    } else {
                        int k = -1;
                        data = new Object[rowNum][rsColumns];
                        for (int i = 0; i < rowNum; i++) {
                            for (int j = 0; j < rsColumns; j++) {
                                if (k < list.size())
                                    k++;
                                if (i <= (rowNum - 1)) {
                                    data[i][j] = list.get(k);
                                }
                            }
                        }
                    }   
                    wb.close();
            } catch (BiffException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return data;       
        }


   
    private Object[][] addList2(String caseName, String dataFile, int beginNum,
            int endNum) throws FileNotFoundException {
       
          ArrayList<Object> list = new ArrayList<Object>();
            // 文件路径
            InputStream is = new FileInputStream(defaultPath + dataFile);
            Object[][] data = null;
            try {
                Workbook wb = Workbook.getWorkbook(is);
                Sheet rs = wb.getSheet(caseName);
                //获取表格总行数
                 int rsRows = rs.getRows();
                //获取表格总列数
                 int rsColumns = rs.getColumns();

                 if(rs!=null){                     
                         for (int i = 1; i <= rsRows-1; i++){
                                for (int j=0; j <= rsColumns-1; j++){
                                    Cell c = rs.getCell(j, i);
                                    String cz = c.getContents();
                                    list.add(cz);
                                }
                            }
                            //System.out.println(list);   
                    }
                 
                    int sub = (endNum - beginNum) + 1;
                    data = new Object[sub][rsColumns];
                    if (beginNum <= 0 || endNum > rsRows) {
                        if(beginNum <= 0&&endNum > rsRows){
                            beginNum=0;
                            endNum=rsRows;
                            data = new Object[rsRows][rsColumns];
                            for (int i = 0; i < sub; i++) {
                                for (int j = 0; j < rsColumns; j++) {
                                    if (beginNum < rsRows*rsColumns)
                                        data[i][j] = list.get(beginNum);
                                    beginNum++;
                                }
                            }
                        }else if (beginNum <= 0&&endNum <= rsRows){       
                            beginNum=0;
                            sub=(endNum - beginNum);
                            data = new Object[endNum][rsColumns];
                            for (int i = 0; i < sub; i++) {
                                for (int j = 0; j < rsColumns; j++) {
                                    if (beginNum < sub * rsColumns)
                                        data[i][j] = list.get(beginNum);
                                    beginNum++;
                                }
                            }
                        }else {
                            endNum=rsRows;
                            sub=(endNum - beginNum) + 1;
                            data = new Object[sub][rsColumns];
                            for (int i = 0; i < sub; i++) {
                                for (int j = 0; j < rsColumns; j++) {
                                    if (beginNum <=sub * rsColumns)
                                        data[i][j] = list.get(beginNum-1);
                                    beginNum++;
                                }
                            }
                           
                        }
                    } else {
                        int k = 0;
                        int a=beginNum*rsColumns;
                        for (int i = 0; i < sub; i++) {
                            for (int j = 0; j < rsColumns; j++) {
                                if (k < sub * rsColumns)
                                        data[i][j] = list.get(a+k-2);
                                        k++;

                            }
                        }
                    }   
                    wb.close();
            } catch (BiffException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return data;       
        }
}
复制代码

4、创建类ExcelDataProvider

复制代码
package com.testNg;

import java.io.FileNotFoundException;
import java.lang.reflect.Method;

import org.testng.annotations.DataProvider;

/**
 * @author kaixie
 * @version 1.0.0
 */


public class ExcelDataProvider {

    // 从XML文本文件中获得数据
        @DataProvider(name = "GetDataFromXml")
        public static Object[][] getTestDataFromXml(Method m) throws FileNotFoundException {

            // 通过反射获得函数名称,可以为多个测试方法提供数据驱动
            Object[][] o = new Object[][] {};

            // 取用例数据集d1Test的全部数据(excel数据源)
            if (m.getName().equals("test1")) {
                return new BaseExcelData().getData("testA", "DataProvider.xls",0,3);
            }
            
            if (m.getName().equals("test2")) {
                return new BaseExcelData().getData("testB", "DataProvider.xls",2,5);
            }
            
            //取用例数据集d2Test的全部数据(excel数据源)
            /*if (m.getName().equals("test1")) {
                return new BaseExcelData.getData("d1Test","caipiaoTest.xls")
            }*/
            
            
            return o;
        }
}
复制代码

5、创建测试类Baidutest

复制代码
package com.testNg;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;



public class Baidutest {
    @Test(dataProvider = "GetDataFromXml", dataProviderClass = ExcelDataProvider.class)
    public void test1(String URL, String context) {
        //String expected = "";

        //如果火狐浏览器没有默认安装在C盘,需要制定其路径
        //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 
        WebDriver driver = new FirefoxDriver();
        driver.get(URL);
        
        driver.manage().window().maximize();
        
        WebElement txtbox = driver.findElement(By.name("wd"));
        txtbox.sendKeys(context);
        
        WebElement btn = driver.findElement(By.id("su"));
        btn.click();
        
        driver.close();
       
}
  
    }
复制代码

6、测试用例如下:

7、右击选择TestNG运行测试并且测试结果如下:

也可以在项目路径下D:\study\WebDriverDemo\test-output\Default suite\Default test.html,TestNG默认的HTML测试报告,如下所示:

posted on 2017-06-28 10:43  IT弄潮儿  阅读(865)  评论(0编辑  收藏  举报

导航