Parallel Selenium Tests: Speed Up Execution
In this article
Running Selenium tests one by one is slow. Parallel execution runs multiple tests at the same time across different browsers, machines, or threads, cutting total suite time dramatically. This guide shows you how to set it up with TestNG and Maven, the most common Java stack, and covers the key pitfalls to avoid.
What You'll Need
- A Java project with Selenium WebDriver and TestNG already configured
- Maven or Gradle as your build tool
- WebDriver binaries (e.g., chromedriver, geckodriver) or Selenium Manager (bundled with Selenium 4.6+)
- Enough CPU/RAM on your machine or access to a cloud grid (Sauce Labs, BrowserStack, etc.) if you need massive scale
Step-by-Step
- Make your tests thread-safe. Parallel execution means multiple threads run at once. Remove any shared static state (like a single WebDriver instance). Each test should create its own driver in
@BeforeMethodand quit it in@AfterMethod. UseThreadLocal<WebDriver>to store the driver per thread. - Configure TestNG for parallel execution. Edit your
testng.xmlfile. Set theparallelattribute tomethods(run test methods in parallel),classes(run classes in parallel), ortests(run<test>blocks in parallel). Setthread-countto the number of threads you want. Example:<suite name="MySuite" parallel="methods" thread-count="4">. - Set up Maven Surefire to use TestNG. In your
pom.xml, add the Surefire plugin and configure it to run yourtestng.xml. The key is to setparallelandthreadCountin Surefire's configuration, or just let TestNG handle it via the suite file. For Maven, ensure thesuiteXmlFilespoints to yourtestng.xml. - Create a driver factory. Write a class that returns a WebDriver instance per thread. Use
ThreadLocalto hold the driver. Example:private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();and set it in a method that initializes the driver based on a browser name passed via system property. - Pass browser type via system properties. To run tests on different browsers, read a system property like
System.getProperty("browser")in your driver factory. Then you can run Maven with-Dbrowser=chromeor-Dbrowser=firefox. - Run your tests with Maven. Execute
mvn test. If you want to run tests in parallel across multiple browsers, you can start multiple Maven processes with different-Dbrowservalues, or use a cloud grid that handles it for you. - Use a cloud grid for large scale. If you need 10+ threads, your local machine may struggle. Use a Selenium Grid (self-hosted) or a cloud provider. For cloud, you get a remote WebDriver URL and capabilities; your tests run the same, just pointing to the remote URL.
- Monitor and tune. Start with 2-4 threads and watch your test times and resource usage. Increase gradually until you hit diminishing returns or resource limits.
Common Mistakes
- Shared WebDriver instance: Using a single static driver across tests causes tests to interfere with each other. Always use ThreadLocal.
- Hard-coded test data: Tests that use the same user account or data will conflict when run in parallel. Use unique data per test (e.g., timestamp-based usernames).
- Not cleaning up: Forgetting to quit the driver in
@AfterMethodleaves browser processes open, eventually exhausting memory. - Over-parallelizing: Setting thread-count to 50 on a 4-core machine will cause thrashing and slower tests. Match threads to your CPU cores.
- Ignoring test independence: Tests that depend on a specific order (e.g., test B runs after test A) will fail when run in parallel. Refactor to make every test standalone.
Tips
- Use
@Test(priority = ...)only when necessary; prefer independent tests. - Use waits (explicit or implicit) instead of
Thread.sleep()to reduce flakiness. - Keep your test suite small and focused; parallel execution amplifies any inefficiency.
- Use a headless browser for CI to save resources.
- Consider using Selenium Grid with Docker to scale horizontally.
Troubleshooting
- Tests fail intermittently: Check for shared state. Look for static variables, test data collisions, or reliance on a specific execution order.
- OutOfMemoryError: Reduce thread count, increase JVM heap (
-Xmx), or use a cloud grid. - Browser crashes: Ensure WebDriver binaries are up to date and compatible with your browser version. Use Selenium Manager to auto-manage.
- Tests run sequentially despite config: Verify your
testng.xmlis actually being picked up by Surefire. Check the Surefire reports to see if parallel is enabled. - Port conflicts: If using a local Selenium Grid, ensure ports are free. For local drivers, each thread gets its own port automatically.
Frequently Asked Questions
Can I run parallel tests on the same machine?
Yes, but only if you have enough CPU and RAM. Each browser instance consumes ~200-500 MB. Start with 2-4 threads on a typical laptop.
What's the difference between parallel='methods' and 'classes'?
'methods' runs each test method in a separate thread, even within the same class. 'classes' runs each class in a separate thread, so methods within a class run sequentially. 'methods' gives more parallelism but requires even stricter test independence.
Do I need a Selenium Grid for parallel execution?
No. You can run parallel tests locally with TestNG's thread-count. Grid is only needed for large scale (10+ threads) or cross-browser testing across multiple machines.
How do I run tests in parallel on different browsers?
Use a system property to select the browser in your driver factory. Then launch multiple Maven processes with different -Dbrowser values, or use a cloud grid that lets you specify browser capabilities per test.
Will parallel execution make my tests flaky?
It can, if your tests aren't independent. The main causes are shared data and shared driver instances. Fix those, and parallel execution should be stable.
What is the ideal thread count?
It depends on your hardware and test complexity. A good starting point is the number of CPU cores. Monitor CPU usage and test times to find the sweet spot.