Parameterization In TestNG XML
Avoid hard coding 💻
TestNG supports two ways to provide parameters to test methods i.e. @Parameters and @DataProvider.
In this article, we’re going to learn about parameterization (@Parameters annotation ) in the TestNG XML file. So, Let’s get started 👇
What is Parameterization in TestNG ❔
Parameterization is to pass the values to the test methods as an argument using an XML file.
When to use ❔
There are some values that are common across all the test cases. For Example, if you have any global variable like password, username, URL settings, package name, activity name, API keys, etc then you can drive these common values from XML files to avoid hard coding.
Hard Coding means the main detail should come from one centralized file or should not be accessed from all files.
How to use parameterization ❔
To use parameterization, you have to deal with both files i.e .xml and .java. Let’s see how 👇
✔️XML File:
First, you need to give the value of your global variable in the <parameter> tag in the XML file like in the example below :
Here, in the above example, There is a xyz.apk that is common in all the test cases. So, I used that in an XML file to get access from this single XML file to all the test cases.
✔️Java file:
Here, in the above example, I need to use the value of apk in the test method that is assigned in the <parameter> tag in the XML file. So, I used the @parameter annotation above the test method and give one parameter in the method i.e. String apkName.
Now, when you will execute the XML file then xyz.apk from the XML file will come to the method have @parameter annotation defined above then that value will enter into the method parameter. As explained in the picture below 👇
Code:
Use the code below on your side and see the results 💻
Output:
Run the XML file and you’ll get the result as below 👇
Voila! 😃 We have done with @Parameters annotation. follow my next article to learn the second-way @DataProvider to pass value to test method.
Best of Luck! 👍