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,320 points
912 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,870 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,510 points
6,000 views
0 votes
0 answers

How to get the text from the HTML5 input error message in Selenium?

On giving invalid data in the email ...READ MORE

Mar 11, 2019 in Selenium by Vaishnavi
• 1,180 points
287 views
0 votes
1 answer

How can we read data from an excel sheet in Selenium webdriver?

Hi Tarun, to read data from an ...READ MORE

answered May 8, 2019 in Selenium by Abha
• 27,760 points
4,399 views
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,510 points
1,582 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,870 points
1,505 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,870 points
1,294 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,870 points
2,237 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,870 points
1,082 views