Selenium webdriver java TestNG DataProvider with jxl error

Answered

I have been searching for a solution to this problem but in vain. My question title suggests what framework I am using.
My problem:
Everytime I update my excel file, save it, and run my tests, I get the error on the lines of: The data provider is trying to pass 256 parameters but the test takes only 12.
So then I have to open my excel, and delete the “unwanted” blank columns. All 244 of them. The tests then run smoothly as long as i don’t “touch” the excel.

Please help.

Default Asked on December 14, 2015 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)
    Best answer

    So I myself found a solution:
    Instead of using getColumns() function, I now pass in the number of columns (which in my case remains fixed per sheet) to my getExcelData() code:

    public String[][] getExcelData(String filePath, String sheetName, int cols) throws BiffException, IOException {

    String[][] arrayExcelData = null;

    FileInputStream fis = new FileInputStream(filePath);
    Workbook wb = Workbook.getWorkbook(fis);
    Sheet sh = wb.getSheet(sheetName);

    int totalCols = cols; //this is where i was earlier using sh.getColumns() which was causing the error
    int totalRows = sh.getRows();

    arrayExcelData = new String[totalRows-1][totalCols]; //all files and sheets contain header

    for (int i= 1 ; i < totalRows; i++) {
    for (int j=0; j < totalCols; j++) {
    arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
    }
    }

    return arrayExcelData;
    }

    While calling the above function from my data provider, i just provide the number of columns as a parameter:

    @DataProvider(name=”DataPro”)
    public Object[][] MyData() throws BiffException, IOException {

    Object[][] arrayObject = getExcelData(XlsFilePath, SheetName, 12);
    return arrayObject;
    }

    Hope this helps someone 🙂

    Default Answered on December 15, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.