用Excel编写Apache POI

时间:2015-08-17 12:08:01

标签: java selenium-webdriver apache-poi

我创建了从excel获取元素的脚本。

这是我的剧本

public void readExcel() throws BiffException, IOException {
        String script = "return rlSerial;";
        WebDriver driver;
        String baseUrl;
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
        driver = new FirefoxDriver();
        baseUrl = "http://website.com/";
        String SiteWindow = driver.getWindowHandle();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        String FilePath = "D:\\TestData.xls";
        FileInputStream fs = new FileInputStream(FilePath);
        Workbook wb = Workbook.getWorkbook(fs);

        // TO get the access to the sheet
        Sheet sh = wb.getSheet(0);

        // To get the number of rows present in sheet
        int totalNoOfRows = sh.getRows();
        int totalNoOfCol=sh.getColumns();
        sh.getColumns();

        for (int row =1; row < totalNoOfRows; row++)
        {
            for (int col = 0; col < totalNoOfCol; col++){   
                if (col == 0)
                {
                    System.out.println("Check for Elink "+sh.getCell(col,row).getContents());
                }
                if (col == 1) {
                    driver.get(baseUrl +sh.getCell(col,row).getContents());
                }
                if (col ==2 ) {
                    driver.findElement(By.xpath(sh.getCell(col,row).getContents())).click();
                    for (String PromoWindow : driver.getWindowHandles()) {
                        driver.switchTo().window(PromoWindow); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
                    }

                }
                if (col ==3 ) {
                    String exSerial  = (String) ((JavascriptExecutor)driver).executeScript(script);
                    System.out.println("Actual rlSerial = "+ exSerial   + "\t" +"Excpected rlSerial = "+sh.getCell(col,row).getContents());
                    Assert.assertEquals(exSerial ,sh.getCell(col,row).getContents());
                    System.out.println("Pass");
                    driver.close();
                    driver.switchTo().window(SiteWindow);

                }

            }

        }
    }

    public static void main(String args[]) throws BiffException, IOException {
        runTest DT = new runTest();
        DT.readExcel();

    }
}

如果我的测试用例通过,我想在下一列写入Pass,如果失败,则“失败”。

如何实现这一点,需要做什么!!!

1 个答案:

答案 0 :(得分:0)

要首先实现此目的,您必须在给定行中创建一个新单元格,然后将值设置为&#34;传递&#34;并且&#34;失败&#34;到这个细胞。使用以下代码:

sheet.getRow(rowNumber).createCell(columnNumber).setCellValue("pass");

编辑: 在您的代码中,您使用的是Assert.assertEquals(actual, expected)函数,该函数与TestNg Annotations一起使用,但您没有在此处使用TestNG注释,因此更好的方法是使用equals()或{{1来比较您的实际和预期字符串方法并根据它设置列传递或失败,这是您想要的解决方案:

equalsIgnoreCase()

并在for循环结束后保存工作表:

if (col ==3 ) {
                String exSerial  = (String) ((JavascriptExecutor)driver).executeScript(script);
                System.out.println("Actual rlSerial = "+ exSerial   + "\t" +"Excpected rlSerial = "+sh.getCell(col,row).getContents());
                //Assert.assertEquals(exSerial ,sh.getCell(col,row).getContents());
                if(exSerial.equals(sh.getCell(col,row).getContents())){
                    sh.getRow(row).createCell(totalNoOfCol).setCellValue("Pass");
                    System.out.println("Pass");
                }
                else{
                    sh.getRow(row).createCell(totalNoOfCol).setCellValue("Fail");
                    System.out.println("Fail");
                }                    
                driver.close();
                driver.switchTo().window(SiteWindow);

            }