Parallel Testing with Java
Last Updated :
02 Aug, 2025
In parallel testing, multiple tests can be run simultaneously in different execution modes, reducing execution time. This approach is particularly useful when running tests across multiple browsers or operating systems because it simplifies cross-browser testing.
Running Parallel Test Cases in SeleniumParallel tests using TestNG and Selenium are a powerful way to run multiple tests simultaneously, thereby reducing overall execution time. Here are the steps to run parallel tests using TestNG and Selenium:
Step 1: Create a Maven Project
Create a Maven project in your IDE (e.g., IntelliJ IDEA, Eclipse) or use the following Maven command to create it via the terminal:
mvn archetype:generate -DgroupId=com.example -DartifactId=selenium-parallel-test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Step 2: Add Dependencies to pom.xml
Once the Maven project is created, open the pom.xml
file and add dependencies for TestNG and Selenium WebDriver.
XML
<dependencies>
<!-- TestNG Dependency -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version> <!-- Replace with the latest version -->
<scope>test</scope>
</dependency>
<!-- Selenium WebDriver Dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version> <!-- Replace with the latest version -->
</dependency>
<!-- Selenium WebDriver for ChromeDriver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.0.0</version> <!-- Replace with the latest version -->
</dependency>
<!-- Selenium WebDriver for FirefoxDriver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>4.0.0</version> <!-- Replace with the latest version -->
</dependency>
</dependencies>
Step 3: Create the Test Class
In this step, we create a TestNG test class called LoginTest.java
to test login functionality on SauceDemo using Chrome and Firefox browsers in parallel.
Java
package ParallelTesting;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class LoginTest {
@Test
public void testLoginInChrome() {
WebDriver driver = new ChromeDriver(); // ChromeDriver setup
driver.get("https://www.saucedemo.com/");
driver.findElement(By.id("user-name")).sendKeys("standard_user");
driver.findElement(By.id("password")).sendKeys("secret_sauce");
driver.findElement(By.id("login-button")).click();
driver.quit();
}
@Test
public void testLoginInFirefox() {
WebDriver driver = new FirefoxDriver(); // FirefoxDriver setup
driver.get("https://www.saucedemo.com/");
driver.findElement(By.id("user-name")).sendKeys("standard_user");
driver.findElement(By.id("password")).sendKeys("secret_sauce");
driver.findElement(By.id("login-button")).click();
driver.quit();
}
}
Step 4: Create the testng.xml
File
This file defines how TestNG will execute the tests in parallel. In this case, we are running tests for both Chrome and Firefox in parallel.
XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Parallel Testing Suite" parallel="methods" thread-count="2">
<test name="Chrome Test">
<classes>
<class name="ParallelTesting.LoginTest" />
</classes>
</test>
<test name="Firefox Test">
<classes>
<class name="ParallelTesting.LoginTest" />
</classes>
</test>
</suite>
Step 5: Run the Tests
Eclipse: Right-click on the testng.xml
file and Select Run As > TestNG Suite.
From Command Line using Maven: Open the terminal and navigate to your project directory and run the following Maven command:
mvn test
Step 6: View Results
Once the tests are executed, you can view the results in the Eclipse console or in the TestNG HTML report. You will find the report in the test-output
folder within your project directory.
Parallel Testing test ReportParallelization vs Serialization
Parallelization aims to improve test performance by executing tests simultaneously, while serialization ensures that tests are executed one after another. The choice of this method depends on factors such as the needs of the test, available resources, and execution speed.
Parallelization | Serialization |
---|
In parallelization, multiple tests run simultaneously across different environments or configurations, reducing overall test execution time. | Serialization involves executing tests one after the other without overlapping with test execution. |
This approach uses a testing framework such as TestNG to distribute test data across multiple threads, processes, or machines. | Tests are executed sequentially, with each test starting only after the previous one has finished, preventing overlap between test executions. |
Parallelization can reduce execution times, especially for high-pressure tests or cross-browser tests. | Serialization is easier but potentially slower, especially for many tests or long tests. |
The use and control of parallelism will include monitoring parallel work and ensuring parallelism of tests. | Serialization is easier to implement and manage because tests run without having to deal with compatibility issues. |
Parallelization works well in situations where efficiency is required, such as managing multiple tests or running cross-browser tests. | For simpler tests or where specifying the application is important, serialization may be the preferred method. This option is important for the ease and speed of management, it speeds up the process for better management. |
Serialization refers to the process of executing operations one by one in a thread of execution. In short, tasks are performed sequentially and each task starts only after the completion of the previous task.
SerializationParallelization means executing a series of tasks on multiple threads, where one task is assigned to one thread. So these tasks are done together. This approach makes better use of multiple CPU cores and can improve application performance and performance.
ParallelizationTime Taken Report of Parallelization and Serialization
Consider the case where we have code with two test methods. This process will run simultaneously in two different browsers: one test will run in Chrome and the other test will run in Firefox. This setup illustrates how parallelization enhances testing speed and provide a better return on investment (ROI).
In the example, we will do this step by step, one after the other. This approach contrasts with other examples where two methods work in parallel, creating two processes that can be done simultaneously. The image below shows the times required for the two methods, respectively:
1. Time Taken Report of Parallelization
Time Taken report of Parallelization2. Time Taken Report of Serialization
Time Taken report of SerializationBoth image indicates the time taken to execute the methods in parallel.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING