0

I am still a novice in Selenium and tried to develop a basic framework. Framework is not working. I have used TESTNG and DataProvider annotation.

Please help or suggest an alternate framework/code.

This is the program i made to read an excel.

@SuppressWarnings("resource")
    public Sheet readExcel(String filePath, String fileName,String sheetName) throws Exception {
        // TODO Auto-generated method stub

        File src = new File(filePath);
        FileInputStream fis = new FileInputStream(src);
        Workbook FlipkartWorkbook = null;
        String fileExtension = fileName.substring(fileName.indexOf(".")); 
        if(fileExtension.equalsIgnoreCase(".xlsx"))
        {
        FlipkartWorkbook = new XSSFWorkbook(fis); //for xlsx file & HSSFWrokbook for xls file
        }
        else if(fileExtension.equalsIgnoreCase("xls"))
        {
            FlipkartWorkbook = new HSSFWorkbook(fis);
        }
        Sheet  FlipkartSheet = FlipkartWorkbook.getSheet(sheetName);
        //XSSFSheet sheet1 = wb.getSheetAt(0);
        return FlipkartSheet;
    }

And this is the test case I am trying.

public class TestCase {

     public WebDriver driver;

    @BeforeMethod
    public void startBrowser()
    {
    File path = new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
    FirefoxBinary ffBinary = new FirefoxBinary(path);
    ProfilesIni allprofs = new ProfilesIni();
    FirefoxProfile firefoxProfile = allprofs.getProfile("Selenium");

/*  firefoxProfile.setPreference("browser.cache.disk.enable", false);
    firefoxProfile.setPreference("browser.cache.memory.enable", false);
    firefoxProfile.setPreference("browser.cache.offline.enable", false);
    firefoxProfile.setPreference("network.http.use-cache", false)*/;

    driver = new FirefoxDriver(ffBinary, firefoxProfile);

    //driver.manage().deleteAllCookies();

    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://flipkart.com");

    }

    @Test(dataProvider="FlipkartData")
    public void TestCase_AutoComplete(String searchText, String uselessData) throws Exception
    {   
        Objects obj = new Objects(driver);
        obj.Search(searchText);
        //driver.findElement(By.xpath(".//*[@id='container']/div/div/header/div[2]/div/div[2]/div/form/div/input")).sendKeys("shoes");
    }



    @DataProvider(name="FlipkartData")
    public Object[][] getData() throws Exception
    {
        Object[][] data;
        ReadExcelData file = new ReadExcelData();
        Sheet FlipSheet = file.readExcel("F:\\WORKSPACE\\ZFlipkartPractice\\FlipkartTestData.xlsx","FlipkartTestData.xlsx", "Search Data");
        int lastrow;
        lastrow = FlipSheet.getLastRowNum();
        int rowCount = (FlipSheet.getLastRowNum()- FlipSheet.getFirstRowNum())- 1;
        System.out.println(rowCount);
        data = new Object[rowCount][1];
        for (int i = 0; i < rowCount; i++) 
        {
            //Loop over all the rows
            Row row1 = FlipSheet.getRow(i+1);
            //Create a loop to print cell values in a row
            for (int j = 1; j < 2; j++) 
            {
                //Print excel data in console
                data[i][j] = row1.getCell(j).getStringCellValue();
                System.out.println(data[i][j]);
            }
        }
        System.out.println("");
        return data;
        }

}
3
  • what is the error you are getting? please share stacktrace or error log Commented Jun 25, 2016 at 19:24
  • java.lang.RuntimeException: java.lang.NullPointerException at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:161) Commented Jun 25, 2016 at 19:34
  • I have tried to read same excel using jxl jars and I was able to do that. But I am only able to read ".xls" file that way. Commented Jun 25, 2016 at 19:37

1 Answer 1

0

Try to call readExcel(String filePath, String fileName,String sheetName) with file.readExcel("F:\\WORKSPACE\\ZFlipkartPractice\\","FlipkartTestData.xlsx", "Search Data");

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.