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.
- 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.
- 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.
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.
In the Second step we would be adding a package and then create an execution file.
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.
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:
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.
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.
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:
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:
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:
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.
Executing the given code, we get the following result: