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
3,950 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,910 points

Related Questions In Selenium

0 votes
1 answer
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,500 points
10,639 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 12, 2019 in Selenium by Vaishnavi
• 1,180 points
1,968 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
• 28,140 points
17,577 views
0 votes
2 answers

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,500 points
13,673 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,874 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,910 points
9,979 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,910 points
3,711 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP