Executing Parallel Test Sessions with TestNG and Selenium WebDriver
In today’s fast-paced world, the primary goal of every business is to release their websites or mobile applications to the end users as early as possible. As a result, businesses constantly search for ways to test, measure, and improve their products. With the increase in competition, faster time to market has become an important aspect for any business to survive in today’s competitive market. However, one of the possible challenges many businesses face is the release cycle time, which usually gets extended for various reasons.
Different techniques and approaches are tried to check the business requirement, development, quality, and overall progress of the delivery required to overcome the challenges. All of this ensures releasing the software smoothly to the market with best quality.
Following are some of the reasons which I have faced in my experience while working with many projects:
• Coding and business requirement issues.
• Automation pipeline taking longer than expected to run, which eventually slows down the delivery time to production.
• Build generation and deployment takes a lot of time, which causes a delay in running automated regression tests and eventually performing manual exploratory testing at the end.
• Flakiness of the automated tests.
• Prioritization of the features at the last moment when the release is near which pushes the engineering team ultimately risking the release.
And likewise, there might be much more to list. However, I have listed a few things in common with many teams.
From the reasons listed above, let’s talk about why the Automation pipeline takes longer than expected to run, which eventually slows down the delivery time to production, and also take a look at the possible solution of how the pipeline can be optimized and time to production could be minimized.
Consider a scenario of an e-commerce website which is built to run on Chrome, Firefox, Microsoft Edge and Safari browsers. You need to run the automation regression test suites on all the browsers before you release it to Production. Let’s say, each test suite has around 50+ test cases to run. So considering we have 4 browsers, Chrome, Firefox,
Microsoft Edge and Safari we need to run 200 tests, after that only we would be able to provide feedback for the builds.
Consider a single test taking around 10 seconds to run, so considering 50 test cases, it would take 500 seconds to run a single suite while performing testing on Chrome browser alone and again 500 seconds for Firefox and then for Edge and so on. So, considering all the 4 browsers it would roughly take around 2000 seconds to run all the tests.
This is where parallel testing comes to the rescue!
In further sections of this Parallel testing tutorial, we will discuss parallel testing and show how to perform Parallel testing using TestNG and Selenium WebDriver.
So get ready to take this journey of running automation tests in parallel with Selenium WebDriver and TestNG.
Table of Contents
• What is Parallel Testing?
• What are the advantages of Parallel Testing?
• How to perform Parallel Execution of Tests on the pCloudy platform using Selenium and TestNG?
What is Parallel Testing?
Parallel testing is the way to execute multiple automation tests at the same time, where tests are run on different threads and consume different resources. We can run tests on different platforms and browsers. So, in the example of the e-commerce application above, it took 500 seconds to run tests on Chrome browser and again 500 seconds to run on Firefox browser.
So, in total we would need 1000 seconds to run the tests on both Chrome as well as Firefox browsers. We can save 50% of our time by running the tests parallel.
Let’s consider that there are multiple versions for Chrome and Firefox browsers where the website needs to be tested upon, the time will be multiplied by the number of versions we add to run the tests on plus if we consider different OS’s and their respective multiple versions. By running the tests in parallel, we can save 50% of our time as all the tests would run at the same time providing us with faster feedback.
Let’s discuss in detail about how to run the tests in parallel with Selenium WebDriver using cloud hosted mobile and web browser platform pCloudy and how it can help you save time involved in running the tests.
If you look closely at Figure 1 above, it shows the orchestration of the tests of how they are designed to get executed on Browser 1 and Browser 2 respectively, when they are run serially.

Consider “Browser 1” as Chrome browser with the latest version and “Browser 2” as Firefox browser with latest version or maybe another Chrome browser with a different version, it all depends on where you want to run your tests. Once the tests get executed, “Browser 1” will be set up and Test 1 to Test 4 will get executed on it.
Once the execution finishes on “Browser 1”, then only execution of another set of same tests will begin on “Browser 2” . Here, the time taken to run tests on “Browser 2” may be more depending upon the waits, locator strategy used,etc. And also the browser and platform, by platform, I mean the OS and its respective version, used for running the tests.
Now, let’s talk about running the browser tests in parallel. In Figure 2, all the tests are running parallel on multiple browsers. Here, as soon as you execute the tests, all the 4 browsers in the respective OS and OS Versions are set up at the same time.
These 4 browsers may be Chrome, or combination of Chrome, Firefox, Edge, Safari, etc., depending on the test strategy chosen to run the tests, and could be run on OS like Windows or may belong to different OS’s like one browser running on “Windows” and another one on “Mac”, the goal is to execute all the tests at once, so we save time and get quick feedback on the tests.
Advantages of running the tests in Parallel
I think by now, you have a fair idea about what parallel testing means and why we use it. Now, let’s discuss some advantages of Parallel testing.
The following are some of the advantages of running tests in Parallel:

1. Reduces the overall execution time of the tests
2. Provides leverage to run tests on multiple browsers with different browser versions, different OS’s and OS’s versions.
3. Accelerated feedback on the builds/deployment.
4. Fast feedback helps the team to fix the issue early and remove the bottlenecks.
5. Achieve faster and frequent release while keeping up pace with the Continuous Integration and Continuous Development.
How to perform Parallel Execution of Tests using Selenium WebDriver on pCloudy with TestNG?
The following programming language and tools have been used in writing and running the tests:
• Programming Language: Java 11
• Web Automation Framework: Selenium WebDriver (Version 4.5.3)
• Test Runner: TestNG
• Build Tool: Maven
• Cloud Platform for test execution: pCloudy.com
Parallel Execution Strategy:
As mentioned in the initial part of this blog, we would be running all the tests in parallel as follows:
We would be creating a json file and reading all the above mentioned configuration params from that file. This would help us in adding/removing the configurations easily without having to modify the code every time. The only change that would be required would be to the json file.
Testing Strategy
Let’s discuss the testing strategy for the pCloudy website on what and how we automate it?
1. We would be navigating to https://pcloudy.com
2. Next, get text displayed in the center of the screen and assert it. Following is the text that would be checking:
Continuous Testing Cloud
Deliver Flawless Digital Experience
through our Device and Browser Cloud
Here is the screenshot of the page we would be checking the text from:
1. Next, we would hover the mouse over the “Resources” menu and after it opens the menu lists, we would find the “Blogs” menu and click on it.
2. Once the “Blogs” page opens we would be checking the page title -> “pCloudy Blogs” on the Blogs page.
Here is the screenshot of the page we would be checking the text from:

Getting Started

As discussed earlier, this project has been created using Maven. TestNG is used as a test runner. Once the project is created, we need to add the dependency for Selenium WebDriver, TestNG and jackson-databind in the `pom.xml` file.
pom.xml

Versions of the dependencies are set in a separate properties block. This is done for maintainability, so if we need to update the versions, we can do it easily without having to search the dependency throughout the pom.xml file.
For reading JSON configuration file, we are using the JsonNode class from the jacksondatabind dependency. It has some rich features and allows us to read the JSON file seamlessly with less code to write. Appropriate approach and logic needs to be applied to get the respective values.

Selenium Java and TestNG dependencies are added which will actually help us writing the web automation tests and running them respectively.

What is TestNG?
TestNG is a test automation framework which helps in running the tests, “NG” stands for “Next Generation”. It is designed to make end to end testing easy for the testers.
How does TestNG help in running tests in parallel?
TestNG allows running the tests in parallel by setting the “parallel” attribute in the “<suite>” tag respectively to “tests”, “methods”, “classes”, “instances”.

As per the setting provided to run the tests, it will start executing the tests in separate threads.

TestNG provides the following options to run the tests in parallel as per their documentation:
parallel=”tests”: Providing this value for the parallel attribute in the <suite> tag will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread.
parallel=”methods”: Providing this value for the parallel attribute in the <suite> tag will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
parallel=”classes”: Providing this value for the parallel attribute in the <suite> tag will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
parallel=”instances”: Providing this value for the parallel attribute in the <suite> tag will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
As we saw, there are multiple options provided by TestNG, however it all depends upon your test design and orchestration to run your tests in parallel. [Source]
Automation Tests
I won’t be discussing finding the locators using Selenium WebDriver and using the Page Object model. You can check the A Comprehensive Guide To Locators In Selenium blog for getting details on locator strategies in Selenium WebDriver . You can also check the Everything you need to know about Page Object model and Page Factory in Selenium blog for more details page object model and page factory in Selenium.
Code Repository
All of the code which is showcased in this blog can be found in this Github repository