title: DataProvider in TestNG to pass data from Excel using Java WebDriver
body: <p>I have a login page which contains username and password. I have a class XcelParserTestNGLogin to create, update and to load from Excel sheet method. And another class Login which is a TestNG class. I am using DataProvider to pass data from Excel. But I am getting Exception <em>The data provider is trying to pass 4 parameters but the method takes 2</em>.  </p>
  
  <p>Here is my code for TestNG:</p>
  
  <pre><code>public class Login {
  
      private static WebDriver driver;
      XcelParserTestNGLogin login1 = new XcelParserTestNGLogin();
      Object[][] data1;
  
      /*public Login() throws IOException, InterruptedException {
          FileInputStream fis = new FileInputStream("Data//LoginPage.xls");
          XcelParserTestNGLogin login1 = new XcelParserTestNGLogin(fis, "Login");
  
          //this.data1 = login1.loadFromSpreadsheet(fis, "Login");
      }*/
  
      @BeforeClass
      public void test() throws Exception {
          System.setProperty("webdriver.chrome.driver",
                  "C:\\Chrome\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
          driver = new ChromeDriver();
          driver.get("Any Url");
      }
      @DataProvider
       public Object[][] dp() throws IOException {
          //login1.fileName = "Data//Login.xls";
          //login1.sheetName = "Sheet1";
          FileInputStream fis = new FileInputStream("Data//LoginPage.xls");
          String sheetName = "Login";
          login1.loadFromSpreadsheet(fis,sheetName);
          return login1.getData();        
      }
  
      @Test(dataProvider = "dp")
      public void devLogin(String UserName,String PassWord) throws InterruptedException, IOException {
  
          driver.findElement(By.name("txtUserName")).sendKeys(UserName);
          driver.findElement(By.name("txtPwd")).sendKeys(PassWord);
          driver.findElement(By.name("btnSignIn")).click();
          Thread.sleep(5000);
  
          if (driver.findElement(By.linkText("DashBoard")).isDisplayed()) {
              List&lt;String&gt; arrayList = new ArrayList&lt;String&gt;();
              arrayList.add("Pass");
              HSSFWorkbook workbook = new HSSFWorkbook();
              login1.createSheet("Login", workbook, arrayList);
          } 
          else {
              try{
              Alert alert=driver.switchTo().alert();
              String alertText=alert.getText();
  
              Assert.assertEquals("invalid username or password,please try again",alertText);
              alert.accept();
              }catch(UnhandledAlertException e){
                  e.printStackTrace();
              }
              List&lt;String&gt; arrayList = new ArrayList&lt;String&gt;();
              arrayList.add("Fail");
              HSSFWorkbook workbook = new HSSFWorkbook();
              login1.createSheet("Login", workbook, arrayList);
          }
      }
  }
  </code></pre>
  
  <p>Here is my code for XcelParserTestNGLogin()</p>
  
  <pre><code>public class XcelParserTestNGLogin {
      private transient Object[][] data;
      String fileName,sheetName;
  
      public XcelParserTestNGLogin() {
  
      }
  
      public XcelParserTestNGLogin(InputStream excelInputStream, String sheetName)
              throws IOException {
          this.data = loadFromSpreadsheet(excelInputStream, sheetName);
      }
  
      public Object[][] getData() {
          return data;
  
      }
  
      Object[][] loadFromSpreadsheet(InputStream excelFile, String sheetName)
              throws IOException {
          // TODO Auto-generated method stub
          HSSFWorkbook workbook = new HSSFWorkbook(excelFile);
          Sheet sheet = workbook.getSheet(sheetName);
  
          int numberOfColumns = countNonEmptyColumns(sheet);
          int numberOfRows = sheet.getLastRowNum() + 1;
  
          data = new Object[numberOfRows - 1][numberOfColumns - 1];
  
          for (int rowNum = 1; rowNum &lt; numberOfRows; rowNum++) {
              Row row = sheet.getRow(rowNum);
              if (isEmpty(row)) {
                  break;
              } else {
                  for (int column = 1; column &lt; numberOfColumns; column++) {
                      Cell cell = row.getCell(column);
                      if (cell == null
                              || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                          data[rowNum - 1][column - 1] = "";
                      } else {
                          data[rowNum - 1][column - 1] = objectFrom(workbook,
                                  cell);
                      }
                  }
              }
          }
  
          return data;
      }
  
      private boolean isEmpty(Row row) {
          // TODO Auto-generated method stub
          Cell firstCell = row.getCell(0);
          boolean rowIsEmpty = (firstCell == null)
                  || (firstCell.getCellType() == Cell.CELL_TYPE_BLANK);
          return rowIsEmpty;
      }
  
      /**
       * Count the number of columns, using the number of non-empty cells in the
       * first row.
       */
      private int countNonEmptyColumns(Sheet sheet) {
          // TODO Auto-generated method stub
          Row firstRow = sheet.getRow(0);
          return firstEmptyCellPosition(firstRow);
      }
  
      private int firstEmptyCellPosition(Row cells) {
          // TODO Auto-generated method stub
          int columnCount = 0;
          for (Cell cell : cells) {
              if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                  break;
              }
              columnCount++;
          }
          return columnCount;
      }
  
      private Object objectFrom(HSSFWorkbook workbook, Cell cell) {
          // TODO Auto-generated method stub
          Object cellValue = null;
          if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
              cellValue = cell.getRichStringCellValue().getString();
          } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              cellValue = getNumericCellValue(cell);
          } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
              cellValue = cell.getBooleanCellValue();
          } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
              cellValue = evaluateCellFormula(workbook, cell);
          }
  
          return cellValue;
      }
  
      private Object getNumericCellValue(final Cell cell) {
          Object cellValue;
          if (DateUtil.isCellDateFormatted(cell)) {
              cellValue = new Date(cell.getDateCellValue().getTime());
          } else {
              cellValue = cell.getNumericCellValue();
          }
          return cellValue;
      }
  
      private Object evaluateCellFormula(final HSSFWorkbook workbook,
              final Cell cell) {
          FormulaEvaluator evaluator = workbook.getCreationHelper()
                  .createFormulaEvaluator();
          CellValue cellValue = evaluator.evaluate(cell);
          Object result = null;
  
          if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
              result = cellValue.getBooleanValue();
          } else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              result = cellValue.getNumberValue();
          } else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) {
              result = cellValue.getStringValue();
          }
  
          return result;
      }
      public void updateExcel(final InputStream excelFile, String SheetName,
              List&lt;String&gt; list) {
          HSSFWorkbook workbook = new HSSFWorkbook();
          Sheet sheet = null;
          if (workbook.getSheetIndex(SheetName) &gt; 0) {
              sheet = workbook.getSheet(SheetName);
              if (list != null &amp;&amp; list.size() != sheet.getLastRowNum()) {
                  workbook.removeSheetAt(workbook.getSheetIndex(SheetName));
                  createSheet(SheetName, workbook, list);
              } else {
                  createSheet(SheetName, workbook, list);
              }
          }
  
      }
      void createSheet(String SheetName, HSSFWorkbook workbook, List&lt;String&gt; list) {
          // TODO Auto-generated method stub
          String[] Heading = {"UserName", "Password",
                  "Result" };
          Sheet sheet = workbook.createSheet(SheetName);
          HSSFRow row = null;
          HSSFCell cell = null;
  
          row = (HSSFRow) sheet.createRow(0);
          for (int cellNum = 0; cellNum &lt; Heading.length; cellNum++) {
              cell = row.createCell(cellNum);
              cell.setCellValue(Heading[cellNum]);
          }   
          for (int rowNum = 1; rowNum &lt;= list.size(); rowNum++) {
              String[] cellVals = {"uname",
                      "pswd", list.get(rowNum - 1) };
  
              row = (HSSFRow) sheet.createRow(rowNum);
              for (int cellNum = 0; cellNum &lt; cellVals.length; cellNum++) {
                  cell = row.createCell(cellNum);
                  if (!(cellNum == cellVals.length))
                      cell.setCellValue(cellVals[cellNum]);
                  else
                      cell.setCellValue(true);
              }
          }
          try {
              FileOutputStream out = new FileOutputStream("Data//LoginPage.xls");
              workbook.write(out);
              out.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      }
  </code></pre>