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,340 points
1,647 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,890 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
7,740 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
653 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,930 points
12,083 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,510 points
3,557 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31 in Selenium by Sri
• 2,970 points
4,565 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,890 points
2,611 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 2 in Selenium by Sri
• 2,970 points
2,902 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,890 points
4,203 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,890 points
1,907 views