How to do Multiple Browser Parallel Testing in a selenium TestNG project

Manul Wickramanayaka
Test Automation Hub
6 min readMay 26, 2021

Topics to be covered in this article.

What is Parallel Testing?
What is testing.xml?
Adding Dependencies to the pom.xml file
Creating a Java class
Creating a testing.xml file
Demo Test Run
Thread count

What is Parallel Testing?

In parallel testing, we test different modules or applications on multiple browsers in parallel rather than one by one. The parallel test execution is different from sequential testing, where we test different modules or functionalities one after the other. This approach of testing is very time-consuming. Parallel testing helps to reduce execution time and efforts and results in a faster time to delivery. It proves to be handy specifically in the case of cross-browser testing.

So in this article, we are going to work with Selenium, TestNG using testing.xml file. And practically discuss parallel testing, parallel thread count, and how to run tests in multiple browsers in parallel. Then let's go for it…

What is testing.xml?

If you are using TestNG as your testing framework in your selenium project then testing.xml file is a configuration file in TestNG and we can use this file to set some execution settings to our tests.

Basically testing.xml the file is used to control the execution of a TestNG test like what packages/ classes/ methods should be included, what should not be excluded, setting dependencies, and so on…

So let's start a simple test using TestNG by adding dependencies in the pom.xml file.

Adding Dependencies to the pom.xml file

First, create a maven project and name it Testing.XML. Then add the following dependencies to the pom.xml file.

First, copy the selenium dependency and set it in the pom.xml file.
https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/3.141.59

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>

Copy the TestNG dependency and set it in the pom.xml file.
https://mvnrepository.com/artifact/org.testng/testng

<! —  https://mvnrepository.com/artifact/org.testng/testng
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>

As well as set the WebDriver maven dependency for Chrome and Firefox drivers. https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager/4.4.3

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.3</version>
</dependency>

That all we have to do as prerequisites and we are good to go…

Creating a Java Class

Create a new Java class called test.java inside the package “test”.

For a simple test, let's try to navigate “www.ebay.com”, validate the site based on title and search something and hit enter. Paste this code into your project and run it as a TestNG Test.

test.java

You will see that two browsers are opening one after the other, validate the title, input “Mobile” as the value, and hit enter.

This TestNG class contains two @Test annotations and sites are running on one browser. In this case, I have written the code to choose the browser @BeforeTest by declaring it, and if you want you can call it from another class. You can pass it from a parameter and change it.

So let's try to run the same thing using the testing.xml file by passing a parameter value to the class.

Creating a testing.xml file

Go to your test.java class right-click on it, go to the TestNG section and click on the convert to TestNG option. You will see a window like this.

Generate testng.xml

AS you can see I have name my Suite name “Suite1” and my Test name “Test1”

Class selection: Here we can select what level we want to create our XML document. (class or package)

Parallel mode: This will allow us to choose whether we want to run our tests in parallel mode (classes/methods/tests) or not.

Thread count: This will choose the thread count we want to run our tests

For the moment let's create an XML file at the class level. You will see our testing.xml file below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite1">
<test thread-count="5" name="Test1">
<classes>
<class name="test.test"/>
</classes>
</test> <!-- Test1 -->
</suite> <!-- Suite1 -->

When you are running the XML file run it as TestNG Suite. You will see the browser is opening and do the actions accordingly. Now the case is what if we want to run the test in multiple browsers (both chrome and firefox) and parallelly.

Demo Test Run

Before our test run change the @BeforeTest as follow because we are passing the browserNameas a parameter from testing.xml file

@Parameters("browserName")
@BeforeTest
public void setUp(String browserName){
if(browserName.equalsIgnoreCase("chrome")) {
//initializing and starting the Chromebrowser
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}else if(browserName.equalsIgnoreCase("firefox")) {
//initializing and starting the Chromebrowser
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
}

Also, make sure you remove one @Test annotation because we can launch multiple browsers from thetesting.xml file hereafter.

You just need to make a little change in the testing.xml file to run on multiple browsers and run the suite.

Inside the <Test> tag passes the browser parameter to the class by putting this code. In here I have passed chrome and firefox.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite1" parallel="tests" thread-count="2">

<test name="test on firefox">
<parameter name="browserName" value="firefox"></parameter>
<classes>
<class name="test.test"/>
</classes>
</test> <!-- Test1 -->

<test name="test on chrome">
<parameter name="browserName" value="chrome"></parameter>
<classes>
<class name="test.test"/>
</classes>
</test> <!-- Test1 -->

</suite> <!-- Suite1 -->

I have written two <test> tags for each browser mentioning its name. You will see two browsers are running parallelly.

So this is how we run multiple browsers parallelly using testing.xml the file.
But if you noticed I have given a thread count as 2. What is meant by this?

Thread count

It’s the number of tests that are run at the same time. It’s like saying how many cars in a fleet that you want on the highway at the same time. If you say you want them to take up three lanes, then they will take up no more than three lanes, but it will take longer to get all cars through. If you say five lanes, then they will take up no more than five lanes, but it will take less time to get all cars through.

So in here as I running this test only from two browsers I have mentioned thread count as 2. So let's see How it works. For better understanding, I have put another firefox browser<test> tag in thetesting.xml file to understand the thread count.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite1" parallel="tests" thread-count="2">

<test name="test on firefox">
<parameter name="browserName" value="firefox"></parameter>
<classes>
<class name="test.test"/>
</classes>
</test> <!-- Test1 -->

<test name="test on chrome">
<parameter name="browserName" value="chrome"></parameter>
<classes>
<class name="test.test"/>
</classes>
</test> <!-- Test1 -->

<test name="test2 on firefox">
<parameter name="browserName" value="firefox"></parameter>
<classes>
<class name="test.test"/>
</classes>
</test> <!-- Test1 -->
</suite> <!-- Suite1 -->

First, put this code at the end of test() the method and see the output in the console. This will show the current thread ID the test is running.

System.out.println("This test is on: " + Thread.currentThread().getId());

In the below results, you will see two tests are running on two different threads. First, it runs on thread 15 at the same time thread 16 is used. In the third test, it searches for the available thread and uses that. In this case, thread 15 was free and launched test 2 on firefox.

This test is on: 15
This test is on: 16
This test is on: 15

So this is how we use thread count and maximize our efficiency. It's recommended to use a higher thread count as it won’t make any unnecessary traffic in the memory. In here I have only used one class but you can use multiple classes and also we can run our tests calling methods using the <method> tag. You can <include> or <exclude> any given method.

<class name="class_name" />
<method>
<include name="MethodName"></include>
<exclude name="MethodName"></include>
</method>

Here in this article, we have used testing.xml files to run our TestNG tests parallelly in a selenium project. Also, we used multiple browsers to run them parallelly.

For your reference find my GitHub project here

THINK QUALITY!

--

--