Error in for loop to fetch data from excel in selenium

0 votes

I’m generating the extent report and I’m fetching the data from excel file. Condition is if the string fetched from excel is "Found" then my test should pass otherwise fail, but in this code it only fetches the data from the first row in the excel.

 

@Test

    public void passTest() throws IOException {

        File src = new File("F:\\drive f\\DemoReport.xlsx");

        FileInputStream fis = new FileInputStream(src);

        XSSFWorkbook wb = new XSSFWorkbook(fis);

        XSSFSheet sheet1 = wb.getSheetAt(0);

 

        int rowcount = sheet1.getLastRowNum();

        for(int i=1;i<=rowcount;i++) {

 

            String str = sheet1.getRow(i).getCell(2).getStringCellValue();

            String str1 = sheet1.getRow(i).getCell(1).getStringCellValue();

            double j = sheet1.getRow(i).getCell(3).getNumericCellValue();

            fis.close();

 

            String exp1 = "Found";

            String exp2 = "Not Found";

            if(str.equals(exp1)) {

                logger=extent.createTest(str1);

                Assert.assertTrue(true);

                logger.log(Status.PASS, MarkupHelper.createLabel(str1 + " found at index " + j, ExtentColor.GREEN));

            }

            else if(str.equals(exp2)) {

                logger=extent.createTest(str1);

                Assert.assertTrue(true);

                logger.log(Status.FAIL, MarkupHelper.createLabel(str1 + "is not found", ExtentColor.RED));

            }

        }

    }
Jun 20, 2018 in Selenium by Martin
• 4,240 points
769 views

1 answer to this question.

0 votes

You are closing theFileInputStream inside the for-loop. Use fis.close(); after for-loop ends.

answered Jun 20, 2018 by Samarpit
• 5,170 points

Related Questions In Selenium

0 votes
1 answer

Reading data from Excel in my Selenium test

Log4j problems are usually thrown because of ...READ MORE

answered Apr 6, 2018 in Selenium by nsv999
• 5,430 points
5,546 views
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,430 points
1,182 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,170 points
1,268 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,170 points
1,025 views
0 votes
1 answer

How to eliminate this error”Cannot instantiate the type Select in selenium webdriver”

Try below code. Select sc = new Select(driver.findElement(By.xpath("your ...READ MORE

answered May 18, 2018 in Selenium by Samarpit
• 5,170 points
1,971 views
0 votes
1 answer

Read data from excel sheet in python

Try to use xrld. A python module used ...READ MORE

answered Jun 28, 2018 in Selenium by Samarpit
• 5,170 points
971 views