如何在DataProvider的帮助下从工作簿中的床单数量传递数据



我喜欢在数据提供商的帮助下从工作簿中的床单中传递测试数据。是否可以通过DataProvider?

尝试以下代码,因为它已针对单个工作表进行编程,但是您可以使用getnumberofsheets关键字来实现多个表格。

以下是对我有效的代码

static Object[][] myObject = null;
    @DataProvider(name = "Authentication")
    public static Object[][] credentials() { 
        FileInputStream file;
        try {
            String filePath = System.getProperty("user.dir") + "\needful\Book1.xlsx";
            System.out.println(filePath);
            file = new FileInputStream(new File(filePath));
            // Get the workbook instance for XLS file
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            // Get first sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);
            // Get iterator to all the rows in current sheet
            Iterator<Row> rowIterator = sheet.iterator();
            Row row = rowIterator.next();
            int noOfrepeat = sheet.getLastRowNum();
            System.out.println("Test Repeat time " + noOfrepeat);
            short noOfData = row.getLastCellNum();
            System.out.println("No of Test Data " + noOfData);
            myObject = new Object[noOfrepeat][noOfData];
            int repeat_int = 0;
            while (rowIterator.hasNext()) {
                row = rowIterator.next();
                // For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();
                String str = null;
                int data_int = 0;
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        str = "" + cell.getBooleanCellValue();
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        str = "" + cell.getNumericCellValue();
                        break;
                    case Cell.CELL_TYPE_STRING:
                        str = "" + cell.getStringCellValue();
                        break;
                    }
                    myObject[repeat_int][data_int] = str;
                    System.out.println("repeat_int:" + repeat_int + " data_int:" + data_int + " - "
                            + myObject[repeat_int][data_int]);
                    data_int++;
                }
                System.out.println("");
                repeat_int++;
            }
            file.close();
            FileOutputStream out = new FileOutputStream(
                    new File(System.getProperty("user.dir") + "\needful\output.xlsx"));
            workbook.write(out);
            workbook.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return myObject;
    }
    // Here we are calling the Data Provider object with its Name
    @Test(dataProvider = "Authentication")
    public void test(String sUsername, String sPassword, String str) {
        System.out.println(sUsername + " , " + sPassword + " , " + str);
    }

是的,通过使用不同的数据型测试方法

,这是可能的
@Test(dataProvider="dp1")
public void testMethod1() {
    //Method body here
}
@Test(dataProvider="dp2")
public void testMethod2() {
   // Method body here
}
@DataProvider(name="dp1") 
public Object[][] dataProvider1{
   // Logic for DataProvider 1 here (return data from sheet 1)
}
@DataProvider(name="dp2") 
public Object[][] dataProvider2{
   // Logic for DataProvider 2 here (return data from sheet 2)
}

最新更新