In this tutorial, you will learn-

Type of Parameterization in TestNG- Parameters annotation with Testng.xml Troubleshooting Parameters using Dataprovider Invoke DataProvider from different class Types of Parameters in Dataprovider

Type of Parameterization in TestNG-

To make parameterization more clear, we will go through the parameterization options in one the most popular framework for Selenium Webdriver – TestNG. There are two ways by which we can achieve parameterization in TestNG

With the help of Parameters annotation and TestNG XML file.

With the help of DataProvider annotation.

Parameters from Testng.xml can be suite or test level Parameter from DataProvider can take Method and ITestContext as the parameter. Let’s study them in detail –

Parameters annotation with Testng.xml

Select parameterization using annotations when you do want to deal with complexity & the number of input combinations are less. Let see how this works Test Scenario Step 1) Launch browser & go to Google.com Step 2) Enter a search keyword

Step 3) Verify the inputted value is same as that provided by our test data Step 4) Repeat 2 & 3 until all values are inputted

A Study, the above example. Just imagine how complex the code will become when we do this for 3 input combinations Now, let’s parameterize this using TestNG To do so, you will need to

Create an XML file which will store the parameters

In the test, add annotation @Parameters

Here is the complete code Test Level TestNG.xml

Instructions to run the script, select the XML file and Run as Test NG Suite Right Click on .xml file -> Run as -> Testng Suite (Note : Suite)

Now, parameters can be defined at 2 levels

Suite level – The parameters inside the tag of TestNG XML file will be a suite level parameter. Test Level — The parameters inside the tag of testing XML file will be a Test level parameter.

Here is the same test with suite level parameters

NOTE: In case if the parameter name is same in suite level and test level then test level parameter will get preference over suite level. So, in that case, all the classes inside that test level will share the overridden parameter, and other classes which are outside the test level will share suite level parameter.

Troubleshooting

Issue # 1 Parameter value in testng.xml cannot be typecasted to the corresponding test method’s parameter it will throw an error. Consider the following example

Here, ‘author’ attribute is equal to ‘Guru99’ which is a string and in corresponding test method its expecting an integer value, so we will get an exception here. Issue # 2 Your @Parameters do not have a corresponding value in testing.xml. You can solve this situation by adding @optional annotation in the corresponding parameter in the test method.

Issue # 3: You want to test multiple values of the same parameter using Testng.xml The Simple answer is this can not be done! You can have multiple different parameters, but each parameter can only have a single value. This helps prevent hardcoding values into the script. This makes code reusable. Think of it as config files for your script. If you want to use multiple values for a parameter use DataProviders

Parameters using Dataprovider

@Parameters annotation is easy but to test with multiple sets of data we need to use Data Provider. To fill thousand’s of web forms using our testing framework we need a different methodology which can give us a very large dataset in a single execution flow. This data driven concept is achieved by @DataProvider annotation in TestNG.

It has only one attribute ‘name’. If you do not specify the name attribute then the DataProvider’s name will be same as the corresponding method name. Data provider returns a two-dimensional JAVA object to the test method and the test method, will invoke M times in a MN type of object array. For example, if the DataProvider returns an array of 23 objects, the corresponding testcase will be invoked 2 times with 3 parameters each time.

Complete Example

Invoke DataProvider from different class

By default, DataProvider resides in the same class where test method is or its base class. To put it in some other class we need to make data provider method as static and in test method we need to add an attribute dataProviderClass in @Test annotation.

Code Example

TestClass ParameterDataproviderWithClassLevel.java DataproviderClass.java

Types of Parameters in Dataprovider

There are two type of parameters supported by DataProvider method. Method– If the SAME DataProvider should behave differently with different test method , use Method parameter.

In the following example ,

We check if method name is testMethodA. If yes return one set of value Else return another set of value

Here is the output

ITestContext– It can use to create different parameters for test cases based on groups. In real-life, you can use ITestContext to vary parameter values based on Test Methods, hosts, configurations of the test.

In the following code example

We have 2 groups A & B Each test method is assigned to a group If value of group is A, a particular data set is returned If value of group is B, another data set is returned

Note: If you directly run your testng class, it will first call dataprovider which can’t get groups information as groups are not available. But instead if you call this class via testng.xml, it will have groups info available with ITestContext. Use the following XML to call the test

Summary

Parameterization is require to create Data Driven Testing. TestNG support two kinds of parameterization, using @Parameter+TestNG.xml and using@DataProvider

In @Parameter+TestNG.xml parameters can be placed in suite level and test level. If The Same parameter name is declared in both places; test level parameter will get preference over suit level parameter. using @Parameter+TestNG.xml only one value can be set at a time, but @DataProvider return an 2d array of Object. If DataProvider is present in the different class then the class where the test method resides,DataProvider should be static method. There are two parameters supported by DataProvider are Method and ITestContext.