TestNG – 15 || DataProvider with parameters.

DataProvider with parameters provides ways to work with different type of datasets using single DataProvider.

In our last chapter, we have learned how to use DataProvider annotation and attribute in a TestNG program. I would recommend to go through with the previous chapter then move with this.

DataProvider parameterization using Method.

DataProvider Parameter Types

DataProvider method supports two types of Parameters. Method and ITestContext. These Parameters are very easy to use.

Method

By using this parameter we could return the data value set based on the method name(which is declared under @Test Annotation). We could use a single DataProvider method to return multiple data set for various methods.

Method is a keyword used to declare a variable to access the information about the methods declared under @Test Annotations. This can be done using getName().

  • DataSource.class
    • dataSource(Method testMethod)
      • String[][] data1
      • String[][] data2
      • String[][] data3
  • GenerateReports.class
    • productDetails(String productName, String productQuantity)
    • userDetails(String userName)
    • defaultMethod(String errorMessage)

In this example, we could see that in GenerateReports.java file, we have three different methods which require a different set of data for processing. There is no change while coding this file.

Now, look into the DataSource.java file. Firstly, we have used parameter Method. Then we have used getName() method to access method information. And at the end, we have to use a conditional statement if to return the data set based on the method name.

DataSource.java

package dataProviderParameterization;
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;

/**
 * @author ashok.kumar
 *
 */
public class DataSource {
	
	@DataProvider(name="database")
	public static Object[][] dataSource(Method testMethod)
	{
		String methodName=testMethod.getName();
		String[][] data1= {{"Television","50"},
							{"Fridge","100"},
							{"Cooler","80"},
							{"Microwave","10"}
						};
		
		String[][] data2= {{"Cred"},{"Lucky"},{"Bred"}};
		
		String[][] errorMessage= {{"No data present."}};
		
		
		if(methodName.equalsIgnoreCase("productDetails"))
		{
			return data1;
		}
		else if(methodName.equalsIgnoreCase("userDetails"))
		{
			return data2;
		}
		else
		{
			return errorMessage;
		}
	}
}

GenerateReports.java

package dataProviderParameterization;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 *
 */
public class GenerateReports {
	
	@Test(dataProvider="database",dataProviderClass = DataSource.class)
	public void productDetails(String productName, String productQuantity)
	{
		System.out.println("Product Name: "+productName +" Quantity: "+productQuantity);
	}

	@Test(dataProvider = "database",dataProviderClass = DataSource.class)
	public void userDetails(String userName)
	{
		System.out.println("User Name: "+userName);
	}
	
	@Test(dataProvider = "database",dataProviderClass = DataSource.class)
	public void defaultMethod(String errorMessage)
	{
		System.out.println(errorMessage);
	}
}

Result Analysis

As we know, that sequence of execution would be alphabetical, as no Priority and dependsOnMethod attribute have been used in the program.

So, first of all, defaultMethod() will get execute with an user-defined error message as we haven’t define any dataset for this method in the data source. After this will observe that relevant data set like Product Details and User Details returned for the first two methods.

Console Output for Method parameter.
output result in index.html file.

ITestContext

If you have understood the concept of Method parameter then using ITestContext is too easy. As the previous parameter returns data set based on the methods name. ITestContext returns dataset based on the group name.

Suppose, we have to use same set of data in multiple test methods. In that case, we need to add multiple conditions in our program. But, using ITestContext interface we could achieve this in a very simple manner as we did in the Method parameter.

ITestContext is an interface which uses getIncludedGroups() method to extract a list of all the group names. We have to use the first instance from this group list. After that, we could use the same conditional statement if for validation on the group name.

To make this program more simple and understandable we have used TestSuite.xml file in this program. By using XML file we could invoke a particular group from the various classes.

DataSource.java

package dataProviderParameterization2;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;

/**
 * @author ashok.kumar
 *
 */
public class DataSource {

	@DataProvider(name = "database")
	public static Object[][] getData(ITestContext groups) {
		String[] groupName = groups.getIncludedGroups();
		String currentGroup=groupName[0];
		
		String[][] cartItems=null;
		
		System.out.println("Current Group: "+currentGroup);
		if(currentGroup.equalsIgnoreCase("stage"))
		{
			cartItems=new String[][] 
					{ {"Car" }, 
					  { "Television" }, 
					  { "Air Conditioner" } };
		}
		else
			cartItems=new String[][] {{"Clothes"}};			
		return cartItems;
	}
}

GetData.java

package dataProviderParameterization2;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 */
public class GetData {

	@Test(groups = {"stage","production"})
	public void userLogin() {
		System.out.println("Logged in with correct credentials.");
	}

	@Test(groups = {"stage","production"},dataProvider = "database",dataProviderClass = dataProviderParameterization2.DataSource.class)
	public void verifyCart(String Items) 
	{
		System.out.println(Items+" added to cart.");
	}

	@Test(groups = {"stage","production"},dataProvider = "database",dataProviderClass = dataProviderParameterization2.DataSource.class)
	public void verifyItems(String Items) {
		System.out.println(Items+" present in your cart.");
	}

	@Test(groups = { "production", "stage" }, dependsOnMethods = "userLogin")
	public void logout() {
		System.out.println("User logout successfully.");
	}
}

DataProviderParamterization2.XML

<?xml version="1.0" encoding="UTF-8"?>
<suite name="DataProviderParamterization2">
	<test name="test1">
		<groups>
			<run>
				<include name="stage"></include>
			</run>
		</groups>
	
		<classes>
			<class name="dataProviderParameterization2.GetData">
			</class>
		</classes>
	</test>
</suite>

Result Analysis

After running DataProviderParamterization2.XML file, we could observe that all the methods associated with the group “stage” will execute. Also, we have used this group for all the test methods and used dataProvider attribute in two test methods(verifyCart() and verifyItems()). But in the DataProvider method, we have defined data set only against group name.

Hence, the same data set will be returning for all the test methods associated with the given group as per requirement.

Console output using ITestContext parameter.
Result Analysis via index.html file for DataProvider ITestContext parameter.

TestNG:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


589total visits,7visits today

Leave a Reply

Your email address will not be published. Required fields are marked *