Quick revision
The short answer
The WebDriver is almost certainly stored in an instance field of the test class or in a static field, and because TestNG uses one instance per class, parallel methods share one browser: one test navigates while another clicks, and one test quits the session another is still using. The fix is one driver per thread, usually a ThreadLocal<WebDriver> created in @BeforeMethod and quit and removed in @AfterMethod(alwaysRun = true), or parallel = classes with one driver per class instance, plus removing other shared state such as static page objects, fixed test data and shared accounts.
What to remember
In this answer, we saw that parallel = methods runs methods of one TestNG instance at the same time, so a WebDriver held in an instance or static field is shared, and threads overwrite, drive and quit each other's browser. We fixed it with a ThreadLocal<WebDriver> created in @BeforeMethod and quit and removed in @AfterMethod(alwaysRun = true), or by switching to parallel = classes with one driver per instance, then removed the remaining shared state in page objects, test data and accounts, and raised the thread count gradually.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The WebDriver is almost certainly stored in an instance field of the test class or in a static field, and because TestNG uses one instance per class, parallel methods share one browser: one test navigates while another clicks, and one test quits the session another is still using. The fix is one driver per thread, usually a ThreadLocal<WebDriver> created in @BeforeMethod and quit and removed in @AfterMethod(alwaysRun = true), or parallel = classes with one driver per class instance, plus removing other shared state such as static page objects, fixed test data and shared accounts.
Why this question is asked
Moving a Selenium suite from one thread to eight is the obvious way to cut a 40-minute run to something reasonable, and the random failures that follow are just as predictable. The error messages look like browser problems, invalid session id, stale element reference, element not found, but the cause is in how the test code shares objects between threads. Interviewers ask this because it combines TestNG's threading model with WebDriver's lifecycle, and because a good answer goes beyond the driver to the other shared state that breaks parallel suites. In this answer, we will recap how a WebDriver session works, look at the broken setup, see exactly how two threads collide, fix it with a ThreadLocal, and then deal with the rest of the shared state.
1A Quick Recap of WebDriver Sessions
A WebDriver object, such as a ChromeDriver, controls one browser through one session. Every call, driver.get(url), findElement(), click(), is sent to that session, and the browser executes them in the order they arrive. driver.quit() ends the session and closes the browser. A WebDriver instance is not designed to be used by several threads at once; it has no idea which thread a command belongs to.
So the rule for parallel tests is simple: each test that runs at the same time as others needs its own driver and its own browser. Everything below is about making the code follow that rule.
2The Broken Setup
Here is a typical base class written for sequential runs. The driver lives in a field, created before each method and quit after it.
Sequentially this works: one method at a time, one browser at a time. With parallel = "methods" and thread-count = "8", TestNG runs paysByCard() and paysByPayPal() at the same time on the same CheckoutTest instance, so they share the single driver field. A static field would be shared even more widely, by every instance in the JVM.
3How Two Threads Collide
Thread 1 runs startBrowser() and sets the field to browser A. Thread 2 runs startBrowser() a moment later and overwrites the field with browser B. Browser A is now orphaned: nobody holds a reference to it, so it will never be quit. Both tests now drive browser B. Thread 1 navigates to the card page and thread 2 to the PayPal page, so each test finds elements from the other's page. When thread 1 finishes first, its stopBrowser() quits browser B, and thread 2's next command fails with an invalid session id error because its session has just been closed.
7 messages in order. Play them, or step through them one at a time.
Both threads talk to the same browser, and the first to finish closes it for the other.
4Fix: One Driver Per Thread With ThreadLocal
A ThreadLocal is a variable where each thread sees its own value. If we keep the driver in a ThreadLocal, each test thread gets and uses its own browser, no matter how many threads share the test instance.
Notice three details. The tests call driver() instead of reading a field, so each call returns the current thread's browser. alwaysRun = true makes the cleanup run even when the test failed or was skipped, so browsers do not leak. And remove() matters because TestNG runs tests on a pool of reused threads; without it, a thread could carry a dead driver into its next test.
An alternative is to change the suite to parallel = "classes" and keep one driver per class instance, created in @BeforeClass. Then each class runs on one thread and owns one browser. It gives less parallelism but needs fewer code changes, and it is a reasonable first step for a large suite.
5Checking the Rest of the Shared State
Fixing the driver often reveals the next layer of problems. Page objects, the classes that wrap one page's elements and actions, are sometimes stored in static fields or created once with a reference to the old shared driver; they must be created per test from driver(). Test data with fixed values, such as every checkout test using order id 1001 or the email test@example.com, collides when two tests create or change the same record at once; each test should generate unique data, for example with a UUID in the email. Shared accounts are the same problem at a larger scale: if eight tests log in as the same user and one changes the address or empties the cart, the others see it.
Finally, tests must not rely on order. In a parallel run, a test that expects the cart to be empty because the previous test cleared it will fail whenever another test runs in between.
6Rolling It Out Safely
Once tests are independent, we raise the thread count gradually: 2, then 4, then 8, running the suite several times at each step and watching for new failures. The limit is often not the code but the infrastructure. Eight headless Chrome instances need several CPU cores and a few gigabytes of memory, and a Selenium Grid or a container per browser may be needed beyond that. In a typical suite, moving from one thread to eight cuts a 40-minute run to around 7 or 8 minutes, not 5, because setup and the slowest classes set a floor.
- Is the driver in an instance or static field?YesMove it to a ThreadLocal or use classes modeNoCheck page objects
- Are page objects static or shared?YesCreate them per test from driver()NoCheck test data
- Do tests share ids, emails or accounts?YesGenerate unique data per testNoCheck ordering
- Does any test rely on another's result?YesMake it set up its own stateNoRaise thread-count step by step
4 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Why not just make the WebDriver field synchronized?
Synchronising access would stop two threads from sending commands at the same instant, but they would still share one browser with one current page, so the tests would still see each other's pages and quit each other's session. The problem is not concurrent access to the object but sharing the browser at all. Each test needs its own.
Does Selenium Grid fix this by itself?
No. A Grid gives us more browsers on more machines, but our code still decides which driver each test uses. If the code shares one RemoteWebDriver between threads, it breaks in exactly the same way. The Grid only helps once each thread creates its own driver.
What is the difference between quit() and close() here?
close() closes the current window but keeps the session open if other windows remain; quit() ends the whole session and closes every window and the browser process. In @AfterMethod we want quit(), so the session and the browser are released.
8Conclusion
In this answer, we saw that parallel = methods runs methods of one TestNG instance at the same time, so a WebDriver held in an instance or static field is shared, and threads overwrite, drive and quit each other's browser. We fixed it with a ThreadLocal<WebDriver> created in @BeforeMethod and quit and removed in @AfterMethod(alwaysRun = true), or by switching to parallel = classes with one driver per instance, then removed the remaining shared state in page objects, test data and accounts, and raised the thread count gradually.
Open with
The symptoms, closed sessions and elements from the wrong page, tell me that tests are sharing a browser. TestNG creates one instance per test class, and with parallel equals methods it runs methods of that instance at the same time.
Then, if they want more
If the WebDriver is a field set in @BeforeMethod, the second thread overwrites it, both tests drive the same browser, each lands on the other's page, and whichever finishes first quits the session the other is still using. A static field makes it worse because it is shared across all instances. The fix is one driver per thread.
I would hold it in a static ThreadLocal of WebDriver, create it in @BeforeMethod, have tests get it through a method rather than a field, and in @AfterMethod with alwaysRun equals true call quit and then remove, because TestNG reuses pool threads.
A simpler step is parallel equals classes with one driver per class instance, which gives less parallelism but fewer changes. Then I would check the rest of the shared state: static page objects holding an old driver, test data with fixed ids or emails, and shared login accounts that tests modify.
Every test should create unique data and not depend on another test's result. Finally I would raise thread-count gradually, two, four, eight, running the suite several times at each step, and make sure the machines have the CPU and memory for that many browsers.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After switching a Selenium TestNG suite to parallel = "methods" with thread-count = 8...": https://scrivio-interview-library.netlify.app/java/testng/2315ffb4aa/