Test frameworks and CI · Write code
A static WebDriver in a parallel TestNG suite
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
We switched this suite to run methods in parallel on four threads. Now tests type into each other's browsers, and some fail with 'invalid session id'. Why? How would you fix it?
// testng.xml: <suite name="regression" parallel="methods" thread-count="4">
public class BaseTest {
public static WebDriver driver;
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
driver.get(Config.get("baseUrl"));
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
public class CartTest extends BaseTest {
@Test
public void addToCart() {
new ProductPage(driver).open("p-101").addToCart();
Assert.assertEquals(new CartPage(driver).itemCount(), 1);
}
}
Then
Follow-ups the interviewer asks next
- Walk me through what happens with two threads, step by step.
- What does ThreadLocal do?
- Where would you call
remove(), and why does it matter? - What else that's static in a typical framework breaks the same way?
- How would DI or Playwright's fixtures solve this differently?
- How many threads would you run on a 4-core CI machine?
The answer
What a strong answer covers
staticmeans one field shared by all threads: thread A creates a driver, thread B overwrites the field, and A's test now drives B's browser; when onetearDownquits, the other thread's commands hit a closed session ("invalid session id"), and A's browser is never closed.- Fix:
private static final ThreadLocal<WebDriver> DRIVER = new ThreadLocal<>();,DRIVER.set(new ChromeDriver(options))in setup, agetDriver()accessor, and in teardownDRIVER.get().quit(); DRIVER.remove();, withalwaysRun = true. remove()matters because TestNG reuses pool threads; a leftover reference can leak into the next test.- Other shared state that breaks: static page objects, static test data (the same user for every thread), static report nodes, a static
SimpleDateFormat. - Alternatives: pass the driver through constructors from a per-test factory; JUnit extensions or Spring DI; Playwright Test gives each test its own
pageand browser context. - Threads: about the core count for local browsers (each Chrome uses roughly a CPU and several hundred MB), more against a remote Grid; measure.
The red pen
Common traps
Avoid:synchronized on every method (which kills the parallelism); making the field volatile; switching to parallel="classes" and hoping.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.