简体   繁体   English

使用 Apache POI 将多种数据类型数据从 excel 发送到 Testng Dataprovider 的最佳方法是什么

[英]What is the best way to send multiple data type data from excel to Testng Dataprovider using Apache POI

I am new to java.I have been trying to automate a website using selenium using TestNG in POM model.I need to enter data for a web form,the data which is stored in a.xlsx file.There are multiple data in multiple columns having o data type string,integer,boolean.I am using Apache POI to extract data from the file using the following code: I am new to java.I have been trying to automate a website using selenium using TestNG in POM model.I need to enter data for a web form,the data which is stored in a.xlsx file.There are multiple data in multiple columns具有 o 数据类型字符串,integer,boolean。我正在使用 Apache POI 使用以下代码从文件中提取数据:

ArrayList<Object> arrlist = new ArrayList<Object>(); try {
      
      FileInputStream file = new FileInputStream( new File("C:\\Users\\dell i7\\Desktop\\imp docs\\TestData.xlsx"));
      
      // Create Workbook instance holding reference to .xlsx file
      
      
      XSSFWorkbook workbook = new XSSFWorkbook(file);
      
      // Get first/desired sheet from the workbook XSSFSheet sheet =
      workbook.getSheetAt(0);
      
      
      
      // Iterate through each rows one by one
      Iterator<Row> rowIterator =sheet.iterator(); 
      while (rowIterator.hasNext()) 
      { Row row = rowIterator.next(); // For each row, iterate through all the columns
      Iterator<Cell> cellIterator = row.cellIterator();
      
      while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); 
      // Check the cell type and format accordingly switch (cell.getCellType()) {
      case NUMERIC: 
      //System.out.println((int) cell.getNumericCellValue());
      arrlist.add((int) cell.getNumericCellValue()); 
      break; 
      case STRING:
      System.out.println(cell.getStringCellValue());
      arrlist.add(cell.getStringCellValue());
      break; 
      default: break; } }
      System.out.println("");
      
      } file.close(); } 
      catch (Exception e
      ) { 
      e.printStackTrace(); 
      }

Now a TestNG Data provider requires an Object[][] to get the data and send it to the Test method.What is the best way to do so?现在一个 TestNG 数据提供者需要一个 Object[][] 来获取数据并将其发送到测试方法。这样做的最佳方法是什么?

@DataProvider(name ="Testlogin") public Object[][] getData() @DataProvider(name ="Testlogin") public Object[][] getData()

{
    //your data from excel sheet 
    return data;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM