• selenium使用execl实现数据驱动测试


    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.junit.Assert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;

    public class TestDriverExcel2007 {
    WebDriver driver;
    String baseurl="http://www.sogou.com/";
    static String path = "d:/selenium/";
    static String fileName = "testData";
    static String fileType = "xls";
    static int sheetIndex=0;
    @BeforeMethod
    public void beforeMethod(){
    driver=new FirefoxDriver();
    driver.get(baseurl);
    }
    @AfterMethod
    public void afterMethod(){
    driver.quit();
    }
    @DataProvider(name="testData")
    public static Object[][]words()throws Exception{
    return getTestData(path,fileName,fileType,sheetIndex);

    }
    @Test(dataProvider="testData")
    public void searchTest(String searchWord1,String searchWord2){
    driver.findElement(By.id("query")).sendKeys(searchWord1+""+searchWord2);
    driver.findElement(By.id("stb")).click();
    (new WebDriverWait(driver,10)).until(new ExpectedCondition<Boolean>(){

    @Override
    public Boolean apply(WebDriver d) {
    // TODO Auto-generated method stub
    return d.findElement(By.id("s_footer")).getText().contains("搜索帮助");
    }

    });
    Assert.assertTrue(driver.getPageSource().contains(searchWord1));
    }


    public static Object[][] getTestData(String path,String fileName,String fileType,int sheetIndex) throws IOException
    {
    InputStream stream = new FileInputStream(path+fileName+"."+fileType);
    Workbook wb = null;
    if (fileType.equals("xls")) {
    wb = new HSSFWorkbook(stream);
    }
    else if (fileType.equals("xlsx")) {
    wb = new XSSFWorkbook(stream);
    }
    else {
    System.out.println("您输入的excel格式不正确");
    }
    Sheet sheet1 = wb.getSheetAt(sheetIndex);
    // for (Row row : sheet1) {
    // for (Cell cell : row) {
    // row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
    // System.out.print(cell.getStringCellValue()+" ");
    //
    // }
    // System.out.println();
    // }
    // return null;

    int rowCount=sheet1.getLastRowNum()-sheet1.getFirstRowNum();
    List<Object[]>records=new ArrayList<Object[]>();
    for (Row row : sheet1) {
    String fields[]=new String[row.getLastCellNum()];
    // for (Cell cell : row) {
    // row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
    // System.out.print(cell.getStringCellValue()+" ");
    // }
    for(int j=0;j<row.getLastCellNum();j++){
    row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
    fields[j]=row.getCell(j).getStringCellValue();
    }
    records.add(fields);


    System.out.println();
    }
    Object[][] results=new Object[records.size()][];
    for(int i=0;i<records.size();i++){
    results[i]=records.get(i);
    // System.out.println(results[i]);
    }
    return results;
    }

    }

  • 相关阅读:
    Kattis
    HDU
    回溯法理解
    算法第5章上机实践报告
    贪心算法理解
    [模板] Dijkstra(堆优化)算法求最短路 Apare_xzc
    【文件管理系统】 Apaer_xzc
    [CCF] 201403-2 窗口 Apare_xzc
    [CCF] 201412-2 Z字形扫描 Apare_xzc
    [CCF] 201503-5 最小花费 Apare_xzc
  • 原文地址:https://www.cnblogs.com/superroshan/p/4995081.html
Copyright © 2020-2023  润新知