简体   繁体   中英

Write in Excel Apache POI

i had created script which get element from 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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