0

问题:-

  1. 数据提供者直接进入异常块
  2. 如何将单元格的值从数据提供者返回到@Test方法以实现参数化

    @DataProvider (name = "DP1")
    public void getData() throws IOException {
    try
    {
        FileInputStream file = new FileInputStream(new File("C:\\Users\\kk\\workspace\\K\\TestNG_Tutorial\\src\\Book1.xls"));
        System.out.println(file);
        //Get the workbook instance for XLS file 
        HSSFWorkbook workbook = new HSSFWorkbook(file);
    
        //Get first sheet from the workbook
        HSSFSheet sheet = workbook.getSheetAt(0);
    
        //Get iterator to all the rows in current sheet
        java.util.Iterator<Row> rowIterator = sheet.iterator();
    
        while(rowIterator.hasNext()) {
            Row row = rowIterator.next();
            //System.out.println("Row is" +row);
            java.util.Iterator<Cell> cellIterator = row.cellIterator();
            while(cellIterator.hasNext()){
    
                Cell cell = cellIterator.next();
                switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    break;
                }
            }
            System.out.println("");
        }
        file.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
    
    @Test(dataProvider = "DP1")
    public void LoginTest() {
        driver.findElement(By.id("username")).sendKeys("A");
    }
    
4

0 回答 0