TestNG Parameters

TestNg parameters are nothing but the arguments that we pass to the given test methods. The data is provided in the main XML file. These testNg parameters can either be applied inside the tags or outside the tags. 

The main function of TestNg parameters is when we want to set up global variables like URLs, usernames, passwords, API keys, etc. that cannot be changed frequently.

Features: 

  • Passing values inside the method is easy. 
  • Storing and reusing the values in code can be done. 
  • Easy to check the stream of data for various test cases. 

Pre-Requisite:

  • Download Eclipse IDE.
  • Create a maven project in Eclipse.
  • Add various dependencies like Selenium, Test Ng, and WebDriver Manager. 
  • Download and install TestNG from the Eclipse marketplace. 

Configuration: 

In the First step, we would be creating a maven project inside Eclipse. We would fill in basic details like name, group id, description, etc. 

Parameterization in TestNG

In the Second step we would be adding a package and then create an execution file. 

Parameterization in TestNG

After creating the package, we would be creating different classes each performing different sets of actions as shown below.

For our first example, we would be creating 2 Test files that would perform addition and subtraction as shown below.

File Structure: 

Parameterization in TestNG file structure

Test Cases: 

Example 1: 

In our first example, we would be creating two test cases, The action performed by both test cases would be addition and subtraction. As shown below:

Addition: 

Parameterization in TestNG addition

Subtraction: 

Parameterization in TestNG Subtraction

We notice that in both scenarios we have not hard coded the values of ‘a’ and ‘b’ respectively. We would hence add these values in the XML file in the coming sections. 

We would create an XML file for adding parameter values. 

Create new file

XML code: 

Parameterization in TestNG XML code

We notice that in the parameters section of line 4 and 5, we have added the given values for the same, and through here we are going to pass the values for the same which would be further encapsulated into code and be accessed. 

 After executing the given code, we get the result.

Results: 

Parameterization in TestNG result

Example 2: 

We will be using a dummy login application for this example.  

We would navigate to https://practicetestautomation.com/practice-test-login/ with the help of Chrome browser and thus provide a username and password and log in. 

We have both username and password for the dummy login page, thus after providing for same we get the following:

With the help of the code below, we will understand how to achieve the given task. 

Without parameters code: 

Without parameters code

In the above code, we see that the URL, browser name, user name, and password all these values are hardcoded. Because of this, the code reusability is totally reduced thus using this approach is inefficient in most cases. 

With parameters code: 

With parameter code

In the above code, we notice that we have added the @parameters tag for the browser name, URL, user name, and password. Using the parameters tag we have passed the values. 

From Line no. 21-35, with the help of parameters we have specified the browser we would be using. In our Test case, we have generally used two browsers (chrome and Firefox) respectively. 

In the case of username and password shown in line no 51, we see that they are passed in the form of array values. This is done whenever we are passing two or more values in a given parameter.  

We have encapsulated the given values in an XML file as shown below:

Parameterization in TestNG XML Code

We see that in line no. 4 we have specified the browser through which we want to execute the code (chrome browser).

Note: Parameters name should generally be in the form of camel case in XML file.

Results: 

Executing the given code, we get the following result:

Parameterization in TestNG Result

Test Report: 

Parameterization in TestNG Test Report

Click here to read the next blog on TestNG.