Data Parameterization in TestNG

Parameterization using TestNG plays a crucial role while creating the framework and automation scripts.It assists in running multiple iteration with different set of data.

For example, we can achieve Parallel execution using TestNG, in that case browsers are passed as the parameters.

There are two ways to parameterize in TestNG:

1.Using Parameters Annotation and TestNG XML

Scenario 1:-
Fill the username and Password values using Parameters approach

We need to work with TestNG.xml file and the @parameter Annotation.

In XML file we will create parameters as name/value pairs and using its tag.

Our xml file will look like this :-

<!DOCTYPE suite SYSTEM "" >
<suite name="Suite">
<test name="uftHelp">
<parameter name="userName1" value="Test1"></parameter>
<parameter name="pwd1" value="Pwd1"></parameter>
<class name="srcTest.DataParameterization"/>

Parameter annotation:-
We will map our XML file parameters with our main code, so that we can utilize its values, it is done using @parameter annotations.

Our parameters code will look like this:-

@Parameters ({“userName1″, “pwd1″ })


package srcTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class DataParameterization {
private static WebDriver driver;
//Declaring the Parameters to receive values from TestNG.xml
public void login(String sUserName,String sPwd)
driver = new FirefoxDriver();
//Adding Implicit wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Maximize browser
//Open the Login Application
//Fill the UserName and Password fields
//Click the Sign in Button
//Clicking on the alert message
//Destroying the object
Scenario 2:-
We would utilize the above approach to implement the running of same test case on various browsers (say Firefox, chrome)
  • @Parameter is applied On any method that already has a @Test, @Before/After or @Factory annotation, it can be applied atmost on one constructor of the test class.
  • Incase @Parameters do not have a corresponding value in testing.xml.We can set @optional annotation value in the code.
  • Remember @parameters can be placed in suite level and test level in our .xml file. But  If same parameter name is declared in both places, test level parameter will get preference over suit level parameter.
  • We cannot have duplicate name value pairs in the TestNG.XMLfile, every parameter name should be unique.Thus to test multiple set of data for the same name, we need to implement DataProviders.
  • XML parameters should be mapped to the Java parameters in the code in the same order as they are found in the annotation, else TestNG will generate an mismatch error
2.Using Dataprovider Annotation

A Data Provider is used in case we need to pass complex parameters in the Test method.  A Data Provider is simply a method annotated with @DataProvider; here, the Data Provider itself acts as a data source. An array of objects with parameters can also be drawn from an Excel, CSV, or Database file using third-party APIs such as JXL or Apache POI.

The @DataProvider method supplies the parameters to the @Test method. In order to receive the data from the @dataProvider method, the name of the Data Provider must match on both annotations.
@DataProvider(name = “TestMe”)
@Test (dataProvider =”TestME”)

Data Provider is a unique feature in TestNG; it is not available in JUnit. Many prefer TestNG because of its effective parameterization results.

Highlight in Selenium

Leave a Reply

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