Parameters in TestNG

In the previous post, we have learnt about assertions and reporting in TestNG. In this post, we will learn about parameters in TestNG.

As we know, parameterization plays very important role in automation testing. It is required when we don’t want to hard code the value in our code and it is not a good practice as well. Then parameterization comes into picture, so we will not have to hard code any value and it will be easy to handle the new data also.

Suppose, any scenario needs to be run with different set of data. This can be achieved by parameterization. Selenium itself is not sufficient to handle parameterization , that’s why it takes help from TestNg Framework. TestNG provides one more good feature called Parameters.

TestNG lets you pass parameters directly to your test methods with your testng.xml.

Let’s understand with an example:

  1. Adding parameters into testng.xml file. Added 2 parameters named as ‘browser’ and ‘app’.
2. Pass both the parameters to the method “Test” by using @Parameters annotation. The same no. of arguments has to be passed in the method.
Output: This method will open a Firefox browser with Gmail appliacation.


 

Define Parameters at different scope:

Parameters can be defined at different scopes based on the need. It can be defined at suite level, test level, classes level or at the class level. If you define a parameter at suite level then all the classes defined under the suite can access it without the need of re-defining. You can also override a parameter at a lower scope. For example, in the below configuration file, we define Suite2 at suite level and then override it at class level.

Class1:

Class2:

TestNG output multiple scope


 

Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Print Friendly, PDF & Email
Assertions and Reporting in TestNG
DataProvider in TestNG
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...