Create TestNG XML File & Execute Parallel Testing

Praveen Mishra

Posted On: August 13, 2020

view count79760 Views

Read time9 Min Read

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on A Detailed TestNG Tutorial.

In the previous article, we learned about creating a TestNG project in Eclipse and how to run our first ever Selenium Test Script. Now that you know how to run a TestNG project Eclipse, it is time to dig deeper into TestNG. So far, you’ve run only a single test script, but in practical scenarios, that simply won’t do. When it comes to test automation, running one test at a time will not be very beneficial. You will have to run multiple test cases to make the most of a Selenium Grid. In this tutorial, let’s dive deep into how we can execute multiple test files from a single file known as the TestNG XML file. We will also see how we can create and execute a parallel testing suite using a TestNG XML file.

In case you are late to the TestNG tutorial series, here’s a step-by-step guide to install TestNG in Eclipse.

What’s the purpose of a TestNG XML File?

The entire point of using a TestNG framework for Selenium automation is to run tests in parallel, i.e. run multiple tests simultaneously. In Java, you have to create test cases separately and then define the test later in the primary method. On the other hand, TestNG allows you to create multiple test cases simultaneously, and that is where the TestNG XML file comes in. A TestNG XML file eases this task, and we can write various test cases all within single or multiple classes using annotations. (Refer: TestNG Annotations Tutorial for Selenium test automation)

Uses of TestNG XML file:

  • Seamless parallel execution
  • Executing multiple test cases for various Java class files
  • Group execution of test cases
  • Supports Parameterisation in TestNG and data-driven testing
  • Including or excluding a particular test method
  • Includes names of class, folders, and methods
  • Clearer test summary with separate HTML and extent reports
  • TestNG listeners automatically create logs

Creating a TestNG XML File

Now that you understand how vital a TestNG XML file is for running parallel tests in a Selenium Grid, it is time to learn how to create a TestNG XML file. Before we begin, let us go back to the first test we created using TestNG in the previous article and perform the following steps. There are two methods to create a TestNG XML file, feel free to use either of them-

Method 1-

  • Right-click on the project folder & towards the bottom of the list, select TestNG and then “Convert to TestNG.”

    Convert to TestNG

  • A screen like below would appear. Click on Next.

    testng tutorial

  • The next window that pops up will have the refactored source code, which would be applicable after you click Finish.

    testng xml

  • A new addition to your project directory would be displayed named as testng.xml.

    lambdatest testng

  • Double click on testng.xml to open the xml file which looks like below-

    xml file

Free Testing

Method 2-

  • Right-click on Project, then New, and finally select File.

    testngxml

  • Next, enter the following file name “testng.xml” and then click on Finish.

  • In this step, you will have to enter the XML code manually. That is how the two methods differ from each other. After entering the XML code, you can select a name for your liking suite and class.

    This is what a basic XML file looks like-

Now you can copy the above code (with your choice of names) on the TestNG XML file we’ve created above.

TestNG XML file

Voila! You have your very first TestNG XML file ready. That is all you have to do to create a TestNG XML file on a Selenium Grid or for Selenium test automation.

With TestNG certification, you can challenge your skills in performing automated testing with TestNG and take your career to the next level.

Here’s a short glimpse of the TestNG certification from LambdaTest:

Running a TestNG XML File

The hierarchy of an XML file is quite straightforward. It goes something like this- first < suite > tag, second < test > tag and lastly, < classes > tag. As mentioned above, you can give any name to the suite and test tag, but you have to be careful while naming classes since it is a combination of the Package name and Test name.

Since suite is the topmost hierarchy, this is where multiple test classes can be placed. For example, we have three test classes, one for opening Google, another for searching on Google, and one to open Gmail. All these three classes can be defined under the same test tag for execution.

This example will help you understand it better.

Here, I have created two Java classes- one that opens up Google & verifies its title and another one which again opens Google & searches for a string.

First Class File-

Second Class File-

Next, we will create the TestNG XML file that uses both the above-defined test cases.

In the XML above, you can see two class names are written under the test name tag. These are the classes that I would want to run using the TestNG XML file. Also, note that I named my test “Google Test” and suite “Test Suite” respectively. Go ahead and choose any name as per your convenience for these two.

The next step is to run the project using the XML file. Right-click on the testng.xml file and navigate to “Run As >> TestNG Suite.”

TestNG Suite

By doing this, you would not have to run the classes individually; instead, we will be executing both the classes by a simple click. The output after the execution would look like below-

You can see for yourself that the test classes mentioned in the XML ran successfully, and with it, you can now create your own test suite and run multiple tests all in one go and subsequently get the results.

Parallel Testing Using TestNG.XML File On Selenium Grid Cloud

Now that you have executed your TestNG.xml file successfully. It is pivotal to note the changes you need to make into the TestNG.xml for Selenium test automation over a cloud-based Grid. Because at the end of the day you want a more practical approach and configuring hundreds of browsers and operating systems in your nodes doesn’t look feasible enough.

You can leverage a cloud Selenium Grid such as LambdaTest which provides 2000+ real browsers and operating systems online. That way, you only need to focus on writing better Selenium automation scripts.

So let me show you how you can leverage testng.xml to run parallel testing with Selenium on LambdaTest.

We will take a sample to-do application for the demo. The Selenium testing script will go to the Sample to-do application and click on the checkboxes to indicate the items are done and will add them to the list. After adding it would verify if the items are added.

TestNG 4

Let us have a look at our Java test file.

To make your Selenium test script run over LambdaTest Selenium Grid, you will need to declare a few changes to the script. This would involve specifying your LambdaTest username, your LambdaTest access key and the HubURL in the remote webdriver, for routing your test script to LambdaTest Selenium Grid.

All of these details can be found on your LambdaTest automation dashboard.

LambdaTest automation dashboard

By far I am sure you’ve realized the importance of using TestNG.xml for running scripts on cloud Selenium Grid. It is simple really, you don’t want to write down hundreds of test files to test the same scenario over hundreds of browser drivers. Which is why you will need a TestNG.xml file which will allow you to run a single test file over numerous combinations of parameters and values specified in it.

For this purpose, you invoked the Parameters annotations in TestNG. – @org.testng.annotations.Parameters(value={“browser”,”version”,”platform”})

Now you need to provide values to these parameters in your testng.xml file. Then you will be able to run a single test across several browsers, simultaneously.

If you go to your LambdaTest platform now. You will notice that the same test has been triggered over multiple browsers + operating systems combinations. Just as we specified in our TestNG.xml file.

Selenium test automation

This is the reason why Parallel testing is one of the most in-demand features of Selenium automation testing thus learning how to maintain Selenium test automation will help you.

A Selenium Grid would really boost up your test build practices. Instead of creating new test cases for individual environments, you can leverage testng.xml to define those environments as parameters and simply run a test on various parameters.

Why break your back when you can run parallel tests on a Selenium Grid cloud?

LambdaTest provides a cloud Selenium Grid where you can run your Selenium test scripts over 2000+ real browsers in parallel.

In our upcoming posts, we will learn about TestNG reports and how we can use the different types provided by the framework. Stay tuned and Happy TestNG! 😉

Author Profile Author Profile Author Profile

Author’s Profile

Praveen Mishra

Praveen is a Computer Science Engineer by degree, and a Digital Marketer by heart who works at LambdaTest. A social media maven, who is eager to learn & share about everything new & trendy in the tech domain.

Blogs: 28



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free