Blogs

Introduction:

TestNG.xml file is a setup/configuration file that helps us in organizing our test cases in the right pattern. It also helps us in making Test Suites and Test methods. 

By using the XML file approach, it makes it easy to understand the code. It also grants us the power to control the flow and execution of tests. 

Features: 

  • Helps us in parallel execution. 
  • It helps us in prioritizing our test methods 
  • It allows us to group our test cases. 
  • Helps in better reporting in case of any failure with the help of HTML reports, and extent reports. 
  • Using listeners, we can create logs. 
  • Helps in various kinds of assertions and validation 

Pre-Requisite: 

  • Download Eclipse IDE 
  • Create a maven project in Eclipse 
  • Add various dependencies like selenium, TestNG, WebDriver Manager 
  • Download and install TestNG from the eclipse marketplace 

Configuration: 

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

TestNG.Xml new maven project

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

New Java class

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

File Structure: 

TestNG.Xml file structure

Test Case:

1. Code in First Test: 

Using the code given below we can launch google and search for ‘neova solutions’ and Facebook using the chrome driver as shown below. 

TestNG.Xml Code in First Test

2. Code in Second test: 

Using the code given below we will be launching Firefox from the gecko driver and navigating to google get the title. 

TestNG.Xml Code in Second Test

3. Code in Third test: 

Using the below code, we will launch the browser and navigate to https://practicetestautomation.com/ and log in using the login credential. 

TestNG.Xml Code in Third Test

In the above code to login, we have divided the task to navigate to the URL and to login by providing login credentials into two different methods. We can add additional methods as the test cases grow and thus later decide on which test case to execute. 

After writing the code we will be creating xml file by right-clicking on the test folder. 

Generating testing.xml

We will be giving the name “mytest.xml” and would be setting the location to the “src/test/resources” folder. 

After that, we would get an initial setup xml file as shown below. 

TestNG.Xml code

But, in the third test case of login, since we have divided it into methods, we would be making a few changes to the code given above. 

TestNG.Xml Code

After making a few changes and including methods our XML code would be looking as shown above. 

We notice that under the test case name “Login test” we have added method names that we will be executing in our test scenario. 

By executing the above code, we get results pass as shown below. 

Execution: 

After the Execution of the code, we will see the results below, which signify that our Test has passed. 

Execution code

Test Report: 

TestNG.Xml Test report

Conclusion:

In this blog, we explored TestNG.xml in detail. The various features and concepts used in TestNG.xml were explained in detail with the help of a TestNG Example.

Click here to read the next blog on TestNG.