• testNG数据驱动Excel(java)


    eclipse环境下搭建的数据驱动框架,需要下载Apache POI 并将其内所有jar包导入Build Path

    package china;
    
    import org.testng.annotations.Test;
    import org.testng.Assert;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.DataProvider;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    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.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    
    public class TestDataDriverByExcelFile {
        public WebDriver driver;
        String baseUrl="http://www.sogou.com";
      @DataProvider(name="testData")
      public static Object[][] words()throws IOException{
          return getTestData("d:\", "testData.xlsx", "Sheet1");
      }
      @Test(dataProvider="testData")
      public void testSearch(String searchWord1,String searchWord2,String searchresult) {
          driver.get(baseUrl);
          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){
                return d.findElement(By.id("s_footer")).getText().contains("搜索帮助");
            }
        });
          Assert.assertTrue(driver.getPageSource().contains(searchresult));
      }
      @BeforeMethod
      public void beforeMethod() {
          System.setProperty("webdriver.chrome.driver", "C:\chromedriver\chromedriver.exe");
          driver=new ChromeDriver();
      }
    
      @AfterMethod
      public void afterMethod() {
          driver.quit();
      }
      public static Object[][] getTestData(String filepath,String filename,String sheetname)throws IOException{
          File file=new File(filepath+"\"+filename);
          FileInputStream inputStream=new FileInputStream(file);
          Workbook workbook=null;
          String fileExtensionName=filename.substring(filename.indexOf("."));
          if(fileExtensionName.equals(".xlsx")){
              workbook=new XSSFWorkbook(inputStream);
          }
          else if(fileExtensionName.equals(".xls")){
              workbook=new HSSFWorkbook(inputStream);
          }
         Sheet Sheet=workbook.getSheet(sheetname);
         int rowCount=Sheet.getLastRowNum() - Sheet.getFirstRowNum();
         List<Object[]> records=new ArrayList<Object[]>();
         for(int i=1;i<rowCount;i++){
             Row row=Sheet.getRow(i);
             String fields[]=new String[row.getLastCellNum()];
             for(int j=0;j<row.getLastCellNum();j++){
                 fields[j]=row.getCell(j).getStringCellValue();
             }
             records.add(fields);
         }
         Object[][] results=new Object[records.size()][];
         for(int i=0;i<records.size();i++){
             results[i]=records.get(i);
         }
         System.out.println(results);
         return results;
      }
    //  @SuppressWarnings("deprecation")
    //public static Object[][] getTestData(String filepath,String filename,String sheetname)throws IOException{
    //       File file=new File(filepath+"\"+filename);
    //       FileInputStream inputStream=new FileInputStream(file);
    //       Workbook workbook=null;
    //       String fileExtensionName=filename.substring(filename.indexOf("."));
    //       if(fileExtensionName.equals(".xlsx")){
    //        workbook=new XSSFWorkbook(inputStream);
    //       }
    //       else if(fileExtensionName.equals(".xls")){
    //        workbook=new HSSFWorkbook(inputStream);
    //       }
    //      Sheet Sheet=workbook.getSheet(sheetname);
    //      int rowCount=Sheet.getLastRowNum() - Sheet.getFirstRowNum();
    //      List<Object[]> records=new ArrayList<Object[]>();
    //      for(int i=1;i<rowCount;i++){
    //       Row row=Sheet.getRow(i);
    //       String fields[]=new String[row.getLastCellNum()];
    //       for(int j=0;j<row.getLastCellNum();j++){
    //           Cell cell=row.getCell(j);
    //           cell.setCellType(Cell.CELL_TYPE_STRING);
    //        fields[j]=cell.getStringCellValue();
    //       }
    //       records.add(fields);
    //      }
    //      Object[][] results=new Object[records.size()][];
    //      for(int i=0;i<records.size();i++){
    //       results[i]=records.get(i);
    //      }
    //      return results;
    
      }
    
    
    //cell.getStringCellValue()

    Excel文档放在D盘中 名字:testData

    数据如下

  • 相关阅读:
    .NET ------- 根据关键字查询后,点击详细页面对关键字标红
    .NET ------ 禁止文本输入的三种方式
    .NET ---- 借助repeater在行中进行下拉框编辑 (前端赋值给下拉框)
    Android ------ Android Studio 生成 apk 文件
    CentOS 8 Stream 简单的网络配置
    最受欢迎的 10 本编程书籍(文末附地址)
    优秀程序员必须掌握的 8 种通用数据结构
    一次阿里 P7 的面经,分享给大家
    如何在 Windows 上运行 Linux? 这里有一份攻略!
    为啥程序员下班后从不关电脑?
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/6439280.html
Copyright © 2020-2023  润新知