DataProvider in TestNG

Let us consider a scenario in which a test case (@Test) needs multiple test data, DataProvider (@DataProvider) is an annotation which can be used to provide multiple test data to a test case.

The return type of DataProvider is  an Object[][] array (two-dimensional), the size of the array represents the number of tests data and the number of variables used respectively.

If all the parameters are of same type(say String) then the return type can be changed to that data type (String[][]).

For Example – Object[5][2] – It represents a test data with 5 pairs of values.

Let us understand DataProvider with an example:

Let’s consider a scenario we want to test login of facebook site for 4 different credentials.

Code to login into Facebook website with 5 pairs of credentials provided through test data:

package testNg;

import java.util.concurrent.TimeUnit;

import org.bridj.objc.ObjCClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Facebook {
	WebDriver Driver;
	public void invokeBrowser(){
		Driver = new FirefoxDriver();
		Driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
		Driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
	@Test (dataProvider="getData")
	public void facebookLogin(String username, String password){
	public Object[][] getData(){
		Object[][] data = new Object[4][2];
		data[0][0] = "";
		data[0][1] = "password1";
		data[1][0] = "";
		data[1][1] = "password2";
		data[2][0] = "";
		data[2][1] = "password3";
		data[3][0] = "";
		data[3][1] = "password4";
		return data;


Here, in the above code, a test case is written which accepts two inputs “username” and “password”, data to these attributes are provided through one of the DataProvider, “getData” below.

The return type of getData() method is Object[][], a 2-Dimensional array of size 4 * 2, where 4 is the number of test data and 2 is the number of variable.

An attribute dataprovider=”getData” is passed in the test case. In case this getData() method is created in some other class then another attribute called dataProviderClass = <full path of the class> can be passed.

Once we execute this class. facebookLogin Method will be executed 4 times with 1 set data (username and password) in each run.

We can have multiple DataProviders in a class file for different methods.

PS: For any questions queries or comment feel free to write us at or Happy Learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: