Apache POI和TestNG

时间:2013-10-27 06:01:35

标签: apache-poi testng

的问题: -

  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");
    }
    

0 个答案:

没有答案