Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Parallel test execution in Selenium with Java allows you to run multiple tests simultaneously, thereby reducing the total test execution time. This is particularly useful in a Continuous Integration (CI) environment where quick feedback is essential. Here’s a detailed look at how to achieve parallel test execution in Selenium using TestNG, a popular testing framework in the Java ecosystem.

Setting Up Parallel Test Execution

1. Maven Project Structure

Ensure your project is set up as a Maven project with the necessary dependencies in your ‘pom.xml‘:

				
					<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>selenium-parallel-tests</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

				
			

2. TestNG Configuration

TestNG allows you to configure parallel execution in its XML configuration file (‘testng.xml‘):

				
					<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>selenium-parallel-tests</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

				
			

In the above example, ‘parallel="tests"‘ specifies that the tests should be run in parallel, and ‘thread-count="4"‘ indicates the number of threads to use.

3. Example Test Classes

Here’s an example of two simple test classes:

TestClass1.java

				
					package com.example.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClass1 {

    private WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void testMethod1() {
        driver.get("https://www.example.com");
        // Add test steps
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

				
			

TestClass2.java

				
					package com.example.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClass2 {

    private WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void testMethod2() {
        driver.get("https://www.example.com");
        // Add test steps
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

				
			

Advanced Configuration

Parallel Execution at Method Level

You can also configure TestNG to run test methods in parallel within the same test class:

				
					<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ParallelTestsSuite" parallel="methods" thread-count="4">
    <test name="TestClass1">
        <classes>
            <class name="com.example.tests.TestClass1"/>
        </classes>
    </test>
    <test name="TestClass2">
        <classes>
            <class name="com.example.tests.TestClass2"/>
        </classes>
    </test>
</suite>

				
			

In this configuration, ‘parallel="methods"‘ ensures that individual test methods within a test class run in parallel.

Handling WebDriver Instances

For parallel execution, it’s crucial to ensure that WebDriver instances are not shared among parallel threads. Using the ThreadLocal class in Java can help manage WebDriver instances safely:

				
					public class WebDriverManager {

    private static ThreadLocal<WebDriver> webDriver = new ThreadLocal<>();

    public static void setWebDriver(WebDriver driver) {
        webDriver.set(driver);
    }

    public static WebDriver getWebDriver() {
        return webDriver.get();
    }

    public static void removeWebDriver() {
        webDriver.remove();
    }
}

				
			

Use this manager in your test setup and teardown methods:

				
					@BeforeMethod
public void setUp() {
    System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
    WebDriver driver = new ChromeDriver();
    WebDriverManager.setWebDriver(driver);
}

@AfterMethod
public void tearDown() {
    WebDriver driver = WebDriverManager.getWebDriver();
    if (driver != null) {
        driver.quit();
        WebDriverManager.removeWebDriver();
    }
}

				
			

With this setup, each test method will have its own WebDriver instance, avoiding conflicts and ensuring thread safety.

Parallel test execution in Selenium with Java using TestNG significantly improves test efficiency and speeds up the feedback loop. By carefully configuring your tests and managing WebDriver instances correctly, you can take full advantage of parallel execution to enhance your testing strategy.

Scroll to Top