Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Parallel Selenium Tests: Speed Up Execution

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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 @BeforeMethod and quit it in @AfterMethod. Use ThreadLocal<WebDriver> to store the driver per thread.
  2. Configure TestNG for parallel execution. Edit your testng.xml file. Set the parallel attribute to methods (run test methods in parallel), classes (run classes in parallel), or tests (run <test> blocks in parallel). Set thread-count to the number of threads you want. Example: <suite name="MySuite" parallel="methods" thread-count="4">.
  3. Set up Maven Surefire to use TestNG. In your pom.xml, add the Surefire plugin and configure it to run your testng.xml. The key is to set parallel and threadCount in Surefire's configuration, or just let TestNG handle it via the suite file. For Maven, ensure the suiteXmlFiles points to your testng.xml.
  4. Create a driver factory. Write a class that returns a WebDriver instance per thread. Use ThreadLocal to 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.
  5. 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=chrome or -Dbrowser=firefox.
  6. 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 -Dbrowser values, or use a cloud grid that handles it for you.
  7. 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.
  8. 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 @AfterMethod leaves 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.xml is 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.