![](/img/trans.png)
[英]How to write excel using Apache POI with 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具有 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();
}
現在一個 TestNG 數據提供者需要一個 Object[][] 來獲取數據並將其發送到測試方法。這樣做的最佳方法是什么?
@DataProvider(name ="Testlogin") public Object[][] getData()
{
//your data from excel sheet
return data;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.