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.

1. In order to run the @Test multiple times with different inputs we can use data provider option present in TestNG

2. Return type of Data Provider method should be always two dimensional array Object object[][];

Eg:public Object[][] searchGoogle()

3. Number of rows present in array will be theiteration of the @Test

4. 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


1. We cannot increase the array size as we know it from java itself

2. Object[2][2] is fixed.

3. We cannot use this for large because it increase the program size .

4. If the data is changing often then we cannot use this data provider because it requires recompilation.

5. We cannot use a data from another class data provider

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Copyright © CherCher Tech