Parameters Annotation in TestNG with example

@Parameters annotation in TestNG is an approach to perform data-driven automation. The main aim of TestNG is to remove the dependency on third-party tools for data-driven automation.

TestNG @Parameters annotation will help to pass the data at run time. So If we are not going to use the third party tool then how can we read the data and where we keep that data. Let’s understand this

Testng.xml file will help us to keep the data using <parameter name=”suite-param” value=”suite level parameter” />. Here the <parameter> is an XML tag and name is the name of the key that will use to call the value and finally the value will be your data that you want to pass.

Now @parameters annotation will use to call this data from testng.xml using the key.

How to define a parameter in testNG

<parameter name="Param_Key" value="This is an example of TestNG @Parameters annotation" />

How to read parameter value using @annotation is testNG.

@Parameters({ "Param_Key" })

Let’s see an example to understand @parameters Annotation in detail.

Create testng.xml

<?xml version="1.0" encoding="UTF-8"?>

<suite name="codedec">

 <test name="Test1">

  <parameter name="Param_Key" value="This is an example of TestNG @Parameters annotation" />

 <classes>

 <class name="com.AutomationTut.ParamterizationExample" /> 

 </classes>

 </test>

</suite>

Create TestNG class(ParamterizationExample )

package com.AutomationTut;

 

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

 

public class ParamterizationExample {

   

@Test   

@Parameters({ "Param_Key" })

  public void TC1( String value) {

      

      System.out.println(value);

  }

}

Now run the testng.xml to check the output.

Output:

I hope now you have enough idea how to use @parameters annotation in TestNG. In the above example, we have seen how to pass a single value but what if I need to pass multiple values. Let’s see

How to pass multiple parameters using @parameters annotation in TestNG.

Create multiple parameters in testng.xml

<parameter name="Param1" value="Value of parameter 1" />

  <parameter name="Param2" value="Value of parameter 2" />

  <parameter name="Param3" value="Value of parameter 3" />

Call multiple parameters

@Parameters({ "Param1", "Param2", "Param3" })

Let’s have an example we have 3 parameters in testng.xml

<?xml version="1.0" encoding="UTF-8"?>

 

<suite name="codedec">

 <test name="Test1">

 

  <parameter name="Param1" value="Value of parameter 1" />

  <parameter name="Param2" value="Value of parameter 2" />

  <parameter name="Param3" value="Value of parameter 3" />

 

 <classes>

 <class name="com.AutomationTut.ParamterizationExample" /> 

 </classes>

 

 </test>

</suite>

  Now let’s modify some code in test case class

 package com.AutomationTut;

 

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

 

public class ParamterizationExample {

   

@Test   

@Parameters({ "Param1", "Param2", "Param3" })

  public void TC1( String value1, String value2, String value3) {

      

      System.out.println(value1);

      System.out.println(value2);

      System.out.println(value3);

  }

}

Output:

It’s awesome right! But there is one more important annotation that will use with @parameter annotation is @Optional annotation in TestNG.

What is @Optional annotation in TestNG?

@Optional annotation in TestNG is used in the case of the parameter value is missing in the testng.xml file.

It means In case we call a parameter in test case but that is not defined or missing in the XML. In this case, the default value will print from the @Optional annotation let’s take an example.

I have two-parameter in testng.xml one is missing

<?xml version="1.0" encoding="UTF-8"?>

 

<suite name="codedec">

 <test name="Test1">

 

  <parameter name="Param2" value="Value of parameter 2" />

  <parameter name="Param3" value="Value of parameter 3" />

 

 <classes>

 <class name="com.AutomationTut.ParamterizationExample" /> 

 </classes>

 

 </test>

</suite>

And here In test case, I am trying to call 3 parameters.

@Test   

@Parameters({ "Param1", "Param2", "Param3" })

  public void TC1( @Optional("Value not found ")String value1, String value2, String value3) {     

      System.out.println(value1);

      System.out.println(value2);

      System.out.println(value3);

  }

}

So the value for the missing parameter will be ”Value not found” that we pass under the Optional annotation.

 Output:

 

This is all about the @parameter annotation in TestNG. I hope this testNG tutorial is helpful for you. If you have any questions or doubts on this topic you can check the attached video tutorial and comment me below the video.