Data providers

A major advantage of automating testing is its ability to test large amount of data on the system quickly. We can achieve this using data provider. Follow below steps in order to understand and implement data provider method.

  1. Write the annotation @DataProvider before the data provider method and its return type is 2 dimensional object array
  2. Define an object Object[][] data = new Object[row][column];


Rows – Number of times your test has to be repeated.

Columns – Number of parameters in test data.

For example

Object[][] data = new Object[3][2];

// 1st row

data[0][0] =”Vaishali”;

data[0][1] = “vaishali”;


// 2nd row

data[1][0] =”admin”;

data[1][1] = “renu”;


// 3rd row

data[2][0] =””;

data[2][1] = “”;

  1. Return this object g. return data;
  1. Now before writing test method write data providers method name in front of @Test tag.

e.g.  @Test(dataProvider=”getData”). Get data is the  data providers method name.

  1. We need to pass parameters to this test case. Lets understand this with the example.
  1. Below is the example given for testing a login page using data provider. The 2 parameters are passed username and passwords. This test takes the input from data providers and the test method takes this inputs and runs the test multiple times depending upon the rows defined for the object returning in the data providers method.

Example code for Data providers

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Date;

import java.util.concurrent.TimeUnit;

public class DataProviders

    public void LoginTest(String username,String password)


        WebDriver driver = new FirefoxDriver();



        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);





    public Object[][] getData()
        //Rows - Number of times your test has to be repeated.
        //Columns - Number of parameters in test data.
        Object[][] data = new Object[4][2];

        // 1st row
        data[0][0] ="Vaishali";
        data[0][1] = "vaishali";

        // 2nd row
        data[1][0] ="admin";
        data[1][1] = "renu";

        // 3rd row
        data[2][0] ="";
        data[2][1] = "";

        // 4th row
        data[3][0] ="admin";
        data[3][1] = "admin";

        return data;



Example Code for reading excel file : To implement this you must have POI Library. to download click here

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class excelTest {

    public  static void main(String[] args)
        try {
            FileInputStream fileInputStream = new FileInputStream("dataProvider/username.xls");

            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);

            HSSFSheet worksheet = workbook.getSheet("Sheet1");

int rowCount= worksheet.getPhysicalNumberOfRows();

for(int i=1;i<rowCount;i++) {
    HSSFRow row = worksheet.getRow(i);
    HSSFCell cellA1 = row.getCell(0);
    String a1Val = cellA1.getStringCellValue();
    HSSFCell cellA2 = row.getCell(0);
    String a2Val = cellA2.getStringCellValue();

    System.out.println("username:" + a1Val+", Password:" + a2Val);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {



Subscribe to YouTube

Training on Selenium

Like us on facebook



Cooking with Amol