0

Using Selenium, I have written the below code to read data from an Excel sheet and key in an email and password into Facebook's login page. Error is Unhandled expression to use try/catch. How can I resolve this?

package Excelpack;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

 public class ClassTwo {

    WebDriver driver;

     String username;
     String password;

     @Test(dataProvider="testdata")
     public void testFireFox(String uname, String password1){

      driver.findElement(By.id("email")).clear();
      driver.findElement(By.id("email")).sendKeys(uname);
      driver.findElement(By.id("pass")).clear();
      driver.findElement(By.id("pass")).sendKeys(password1); 

     }   

    @DataProvider(name="testdata")
    public  Object[][] TestDataFeed1()  
    {

          FileInputStream fis=new FileInputStream("D:\\book3.xlsx");
          XSSFWorkbook wb=new XSSFWorkbook(fis);
          XSSFSheet sh1= wb.getSheetAt(0);
         int  numrow = sh1.getLastRowNum()-sh1.getFirstRowNum();  
           int  colnum =sh1.getRow(1).getLastCellNum()+1; 
             Object [][] facebookdata=new Object[numrow][colnum];
      for(int i=0;i<numrow;i++)
      {

         facebookdata[i][0]=sh1.getRow(0).getCell(i).getStringCellValue();
         facebookdata[i][1]=sh1.getRow(1).getCell(i).getStringCellValue();   

      }
        return facebookdata;
    }



  @BeforeTest
  public void Setup(){
  driver=new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("https://www.facebook.com/");
  }
}
sushain97
  • 2,752
  • 1
  • 25
  • 36

1 Answers1

0

If you are using any IDE like eclipse or intellijI then IDE would prompt you with the solution as to add "throws necessary exception" or write the block of code in try/catch. from the looks of the code it seems you would need to declare throws exception on your dataprovider TestDataFeed1.

Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71