How to read multiple sets of value in the excel sheet using DataProvider in Selenium WebDriver with Java and TestNG

user3012639 picture user3012639 · Jun 10, 2014 · Viewed 36.5k times · Source

I'm fairly new to Selenium WebDriver. I wrote code to read the login credentials and values from the excel sheet by using Data Provider. It runs through the first set up data(login functionality) perfectly giving me the green status bar.

In my application, after login, I want to select values by sending index and Selection(In Administration Method) from the same excel sheet but I failed to read values. For hard coded values its working fine.

can anyone please give me the idea how to write it. Used Excel Sheet:

Below is my code:

import java.io.File;

import jxl.Sheet;
import jxl.Workbook;

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.AfterClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.testng.annotations.BeforeClass;

public class TestCase {

String[][] tabArray = null;
Workbook workbk;
Sheet sheet;
int rowCount, colCount;
String sheetPath = "test/Resources/Data/Auto_Increment.xls";
WebDriver login;
//int eRow, eCol, sRow = 0, sCol = 0;


@BeforeSuite
public void setUp(){
    login = new FirefoxDriver();
    login.get("http://etazo.tangosoftware.com");
    System.out.println("select the etazo web link..");
}

@DataProvider
public Object[][] getLoginData() throws Exception {
    Object[][] retObjArr = getExcelData(sheetPath, "Sheet1");
    System.out.println("getData function executed!!");
    return retObjArr;
}

//  Excel API to read test data from excel workbook
public String[][] getExcelData(String xlPath, String shtName)
        throws Exception {
    Workbook workbk = Workbook.getWorkbook(new File(xlPath));
    Sheet sht = workbk.getSheet(shtName);
    rowCount = sht.getRows();
    colCount = sht.getColumns();
    tabArray = new String[rowCount][colCount - 2];
    System.out.println("erow: " + rowCount);
    System.out.println("ecol: " + colCount);
    for (int i = 0; i < rowCount; i++) {
        for (int j = 0; j < 3; j++) {
            tabArray[i][j] = sht.getCell(j, i).getContents();
        }
    }
    return (tabArray);
}

@Test(dataProvider = "getLoginData")
public void LoginData(String distID, String asmtId, String studID)
        throws InterruptedException, BiffException, IOException {
    Administartion(distID, asmtId, studID);
}
public void Administartion(String distID, String asmtId, String studID)
        throws BiffException, IOException {
    Workbook workbk = Workbook.getWorkbook(new File(sheetPath));
    Sheet sht = workbk.getSheet("Sheet1");
    int currRow = sht.findCell(studID).getRow();
    //login.findElement(By.xpath("//*[@id='question-"+sIndex+"']/bubbles/circle["+sValue+"]")).click();     
    System.out.println(sht.getCell(3, currRow).getContents() + " Index ");
    System.out.println(sht.getCell(4, currRow).getContents() + " Answer selection");
}

}

Answer

Praveen picture Praveen · Jun 14, 2014

edited the code. Now this will fetch all the records.

And your excel sheet should look something similar to this: enter image description here

import java.io.File;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestClass {
    String[][] tabArray = null;
    Workbook workbk;
    Sheet sheet;
    int rowCount, colCount;
    String sheetPath = "D:\\Download\\Auto_Increment.xls";

    // Excel API to read test data from excel workbook
    public String[][] getExcelData(String xlPath, String shtName)
            throws Exception {
        Workbook workbk = Workbook.getWorkbook(new File(xlPath));
        Sheet sht = workbk.getSheet(shtName);
        rowCount = sht.getRows();
        colCount = sht.getColumns();
        tabArray = new String[rowCount][colCount - 2];
        System.out.println("erow: " + rowCount);
        System.out.println("ecol: " + colCount);
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < 3; j++) {
                tabArray[i][j] = sht.getCell(j, i).getContents();
            }
        }
        return (tabArray);
    }

    @DataProvider
    public Object[][] getLoginData() throws Exception {
        Object[][] retObjArr = getExcelData(sheetPath, "Sheet1");
        System.out.println("getData function executed!!");
        return retObjArr;
    }

    @Test(dataProvider = "getLoginData")
    public void LoginData(String distID, String asmtId, String studID)
            throws InterruptedException, BiffException, IOException {
        Administartion(distID, asmtId, studID);
    }

    public void Administartion(String distID, String asmtId, String studID)
            throws BiffException, IOException {
        Workbook workbk = Workbook.getWorkbook(new File(sheetPath));
        Sheet sht = workbk.getSheet("Sheet1");
        int currRow = sht.findCell(studID).getRow();
        System.out.println(sht.getCell(3, currRow).getContents() + " Index ");
        System.out.println(sht.getCell(4, currRow).getContents() + " Answer selection");
    }
}