Data Provider in TestNG

Some situations, we might need to execute the scenario with different data, at that times we cannot write a test case for each data.

  • To run the @Test multiple times with different inputs, we can use data provider option present in TestNG
  • The return type of Data Provider method should be always two-dimensional array Object object[][];
    public Object[][] searchGoogle()
  • Number of rows present in the array will be the number of iteration of the @Test
  • Number of columns present in the array will be the number of arguments of the @Test


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;
						import org.testng.annotations.DataProvider;
						import org.testng.annotations.Test;

						public class DataProviderClass 
						{
							WebDriver driver=new FirefoxDriver();
							@DataProvider(name="searchData")
							public Object[][] searchData()
							{
								Object[][] data=new Object[3][1];
								data[0][0]="https://google.com";
								data[1][0]="https://yahoo.com";
								data[2][0]="https://bing.com";
								return data;
							}
							
							@Test(dataProvider="searchData")
							public void doSearch(String site)
							{
								driver.get(site);
							}
						}
					

But in most of the case, we do not require a single argument from the table, the below example shows how to get the two arguments from the table.

In the below program we are getting username and password to enter into Gmail.

We can get any number of arguments.


						import org.testng.annotations.DataProvider;
						import org.testng.annotations.Test;

						public class DataProviderClass 
						{
							WebDriver driver=new FirefoxDriver();
							@DataProvider(name="searchData")
							public Object[][] searchData()
							{
								Object[][] data=new Object[3][2];
								data[0][0]="userkumar";
								data[0][1]="userkumar's password";
								data[1][0]="vikaskumar";
								data[1][1]="vikaskumar's password";
								data[2][0]="admin";
								data[2][1]="admin's password";
								return data;
							}
							
							@Test(dataProvider="searchData")
							public void doSearch(String username,String password) throws InterruptedException
							{
								//opens the gmail.com
								driver.get("https://gmail.com");
								//enter username from table data
								driver.findElement(By.id("Email")).sendKeys(username);
								//enter password from table
								driver.findElement(By.id("Passwd")).sendKeys(password);
								//submit the form
								driver.findElement(By.id("Passwd")).submit();
							}
						}
					

Limitations of the Data Provider

  • We cannot increase the array size as we know it from java itself
  • Object[2][2] is fixed
  • We cannot use this for large because it increases the program size
  • If the data is changing often then we cannot use this data provider because it requires recompilation
  • We cannot use data from another class data provider
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions