How to Run Selenium Tests in Parallel for Faster Automation
To run
Selenium tests in parallel, use a test framework like TestNG or JUnit 5 that supports parallel execution. Configure your test suite XML or annotations to specify parallelism, so multiple browser sessions run simultaneously, reducing total test time.Syntax
Parallel execution in Selenium is controlled by your test framework. For TestNG, you define parallelism in the testng.xml file. For JUnit 5, you use annotations and configuration properties.
parallel="tests"runs test classes in parallel.thread-count="N"sets how many threads run simultaneously.- Test methods or classes run in separate browser instances.
xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="tests" thread-count="3"> <test name="Test1"> <classes> <class name="tests.LoginTest"/> </classes> </test> <test name="Test2"> <classes> <class name="tests.SearchTest"/> </classes> </test> </suite>
Example
This example shows how to run two Selenium test classes in parallel using TestNG. Each test opens a browser, performs actions, and closes it. The testng.xml file configures parallel execution with 2 threads.
java and xml
package 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 LoginTest { WebDriver driver; @BeforeMethod public void setup() { driver = new ChromeDriver(); } @Test public void testLogin() { driver.get("https://example.com/login"); // Add login steps here assert driver.getTitle().contains("Login"); } @AfterMethod public void teardown() { driver.quit(); } } package 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 SearchTest { WebDriver driver; @BeforeMethod public void setup() { driver = new ChromeDriver(); } @Test public void testSearch() { driver.get("https://example.com"); // Add search steps here assert driver.getTitle().contains("Example"); } @AfterMethod public void teardown() { driver.quit(); } } <!-- testng.xml --> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="tests" thread-count="2"> <test name="LoginTest"> <classes> <class name="tests.LoginTest"/> </classes> </test> <test name="SearchTest"> <classes> <class name="tests.SearchTest"/> </classes> </test> </suite>
Output
PASSED: testLogin
PASSED: testSearch
==============================================
Default Suite
Tests run: 2, Failures: 0, Skips: 0
==============================================
Common Pitfalls
- Sharing WebDriver instances: Each test must create its own WebDriver to avoid conflicts.
- Thread safety: Avoid using static WebDriver or shared variables across threads.
- Resource limits: Running too many threads can overload your machine or browser.
- Test dependencies: Tests should be independent to run safely in parallel.
java
/* Wrong: Sharing WebDriver instance across tests causes failures */ public class WrongTest { static WebDriver driver = new ChromeDriver(); @Test public void test1() { driver.get("https://example.com"); } @Test public void test2() { driver.get("https://example.com/login"); } } /* Right: Each test creates its own WebDriver instance */ public class RightTest { WebDriver driver; @BeforeMethod public void setup() { driver = new ChromeDriver(); } @Test public void test1() { driver.get("https://example.com"); } @Test public void test2() { driver.get("https://example.com/login"); } @AfterMethod public void teardown() { driver.quit(); } }
Quick Reference
- TestNG parallel modes:
tests,classes,methods - Thread count: Controls how many tests run at once
- WebDriver: Must be created per test for thread safety
- Use
@BeforeMethodand@AfterMethodto setup and quit browser - Check your machine resources before increasing threads
Key Takeaways
Use TestNG or JUnit 5 to configure parallel execution of Selenium tests.
Create a separate WebDriver instance for each test to avoid conflicts.
Set thread-count carefully to balance speed and resource usage.
Keep tests independent and avoid shared state for safe parallel runs.
Configure parallelism in test suite XML or annotations depending on framework.