Join us on :

Data Provider in TestNG

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

  • In order to run the @Test multiple times with different inputs we can use data provider option present in TestNG
  • Return type of Data Provider method should be always two dimensional array Object object[][];
    public Object[][] searchGoogle()
  • Number of rows present in array will be theiteration of the @Test
  • Number of columns present in the array will be the number of argument 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, below example shows how to get the two arguments from the table.

    In the below program we are getting username and password to enter in to gmail.

    We can get any number of arguments.

    
    						import org.openqa.selenium.By;
    						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][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();
    								
    								//wait for 5 seconds
    								Thread.sleep(5000);
    								
    							}
    						}
    					

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 increase the program size
  • If the data is changing often then we cannot use this data provider because it requires recompilation
  • We cannot use a data from another class data provider

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions