简体   繁体   English

用Excel编写Apache POI

[英]Write in Excel Apache POI

i had created script which get element from excel , . 我创建了从excel获取元素的脚本。

Here is my script 这是我的剧本

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();

    }
}

If my test cases pass i want to write Pass on next column and if fail then "Fail". 如果我的测试用例通过,我想在下一列上写“通过”,如果失败,则“失败”。

How to achieve this , what to need to be done !!! 如何做到这一点,需要做什么!

To Achieve this first you have to create a new cell in the given row and then set value as "pass" and "fail" to this cell. 为此,您必须在给定的行中创建一个新单元格,然后将该单元格的值设置为“通过”和“失败”。 Use the following code: 使用以下代码:

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

EDIT: In your code you are using Assert.assertEquals(actual, expected) function which is used with TestNg Annotations, but you are not using TestNG annotations here, So better way is simply compare your actual and expected strings by using equals() or equalsIgnoreCase() method and set your column pass or fail based on that, Here is the solution you want: 编辑:在您的代码中,您正在使用与TestNg批注一起使用的Assert.assertEquals(actual, expected)函数,但是您在此处未使用TestNG批注,因此更好的方法是使用equals()或简单比较实际字符串和期望字符串equalsIgnoreCase()方法并equalsIgnoreCase()设置您的列通过或失败,这是您想要的解决方案:

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

            }

And save your worksheet after the end of for loop like that: 并在for循环结束后保存工作表,如下所示:

FileOutputStream outFile =new FileOutputStream(new File(FilePath ));
    wb.write(outFile);
    outFile.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM