TestNG Tutorials 42: Parameters In TestNG or Parameterization of Methods in a TestNG Class

Hello Folks,

Arguments or Parameters help us to achieve reusability of codes and cleanliness of codes. We can do overloading of methods using different number, types and order of parameters to serve differently for different set of values.

TestNG also allows you to parameterized your tests. We will see how can we achieve parameterisation of methods in a TestNG class.

Usage of parameterized methods of TestNG in Selenium:

Some real time examples are as below:

  1. You need to login to your application with different users with different roles. You can not create multiple methods for each user with role type. Instead of that you can create a parameterized methods which will accept user type and login as required.
  2. You may need to connect different databases and each database may have different credentials. This can be achieved easily using parameterized methods.

 

You can parameterized @Test annotated methods as well as @BeforeXXXX @AfterXXXX methods. It is not like that you can parameterized on test methods. It is an interview question. 

Let’ create some configuration methods and test methods with parameters:

[java]
package Parameters;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ParametersThroughXML {

@BeforeMethod
public void configurationBeforeMethod(String confBeforeParameter)
{
System.out.println("Paramters for before configuration method: "+confBeforeParameter);
}

@Test
public void testMethod(String testParameters)
{
System.out.println("Paramters for test method: "+testParameters);
}

@AfterMethod
public void configurationAfterMethod(String confAfterParameter)
{
System.out.println("Paramters for after configuration method: "+confAfterParameter);
}
}

[/java]

Run above testng class as a testng suite:

Got some error as below??

I see many people get this error but they can not understand why they are getting this. So when you run any test class which has parameterized methods without parameterized testng xml, you will get above error message.

How to run parameterized methods of testng class?

There are two ways of running parameterized methods of a testng class. They are:

  1. Using testng xml
  2. Using DataProviders annotated methods.

Passing parameters values to methods from testng xml:

Step 1: Update your parameterized methods with @Parameters annotation:

To provide a parameter from testng xml, you need to use Parameters annotation with method. Remember a method can have multiple annotations.

Example:

[java]
package Parameters;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParametersThroughXML {

/* We need to add Parameters annotation to read value from testng xml. Note here that
* attribute name passed in Parameters must be same as testng xml.
*/
@Parameters({"confBeforeParameter"})
@BeforeMethod
public void configurationBeforeMethod(String confBeforeParameter)
{
System.out.println("Paramters for before configuration method: "+confBeforeParameter);
}

@Parameters({"testParameters1","testParameters2"})
@Test
public void testMethod(String testParameters1, String testParameters2)
{
System.out.println("Paramters one for test method: "+testParameters1);
System.out.println("Paramters two for test method: "+testParameters2);
}

@Parameters({"confAfterParameter"})
@AfterMethod
public void configurationAfterMethod(String confAfterParameter)
{
System.out.println("Paramters for after configuration method: "+confAfterParameter);
}
}

[/java]

Notes:

  1. Parameters annotation is used to read parameters from testng xml for methods in a testng class.
  2. Attribute names provided in Parameters annotations must be same as parameters name provided in testng xml.
  3. Count of attributes in Parameters annotation must be same as count of arguments in that method on which it is used otherwise you will get exception. For Example: If a method accepts two arguments, then @Parameters must have only two attributes.
  4. Attributes are mapped in order as it appears in Parameters annotation with method arguments. For example: First attribute in Parameters annotation is mapped to first attribute in method and so on.

 

Step 2: Update your testng xml with parameter tags:

Update your testng xml with parameter tags as below for all parameters of methods in testng class as below:


Understand mapping of parameterized methods of testng call with testng xml below:

 

Now run testng xml. Please note you must need to run from testng xml otherwise you will get injection exception again.

Output:

Hope it will be clear for you how can we use Parameters annotation in a TestNG class with methods and parameter tag in testng xml to achieve parameterization of methods in testng.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

 

Leave a Reply

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