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
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Protractor Training

new tutorial I am starting new Protractor training batch on Weekends. Course starts from 08-june-2019 [10am IST to 1 pm IST] based on online for 6 days, and course fee is INR 10, 000.

Interested people can confirm the seat by calling to +91 8971673487 or whatsapp to +91 9003381224

Find the course content : View Content