Scrivio Interview library
CodingIntermediate7 min read · updated 12 November 2026

Run a JUnit 5 Selenium suite in parallel against Selenium Grid 4. Show how the tests create a RemoteWebDriver, how JUnit parallelism is enabled, and how you keep tests from sharing a browser.

Each test creates its own RemoteWebDriver, new RemoteWebDriver(new URL("http://grid:4444"), new ChromeOptions()), in @BeforeEach and quits it in @AfterEach, keeping it in an instance field of JUnit's default per-method test instance, never in a static field. Parallelism is switched on in junit-platform.properties with junit.jupiter.execution.parallel.enabled=true and junit.jupiter.execution.parallel.mode.default=concurrent, with a fixed parallelism that matches the Grid's free slots. Grid 4 can run standalone, as hub and nodes, or fully distributed, and every parallel test must use its own data, such as a unique user.

Why this question is asked

A UI suite that runs one test at a time grows from minutes to hours as it gains tests, and the usual remedy is to run many browsers at once on Selenium Grid. But parallel UI tests fail in new ways when two tests share a browser, a driver variable or a user account. Interviewers ask this coding question to see whether we can wire JUnit 5 and Grid together correctly and keep tests independent. In this answer, we will recap what Grid 4 does, write the per-test driver, enable JUnit parallelism, see why a static driver breaks, size parallelism to the Grid, and keep test data from colliding.

1A Quick Recap of Selenium Grid 4

Selenium Grid is a server that accepts WebDriver sessions and runs them on a pool of machines that have browsers. Our tests talk to one address, and Grid routes each new session to a node with a free slot for the requested browser. A slot is room for one browser session; a node with four slots can run four browsers at once.

Grid 4 can run in three shapes. Standalone runs everything in one process, good for a laptop or a small CI job. Hub and node splits the router from the machines with browsers, the classic setup. Fully distributed runs the router, distributor, session map and new session queue as separate processes, for large installations. With Docker, the selenium/hub and selenium/node-chrome images, or selenium/standalone-chrome, are the usual starting point, and the number of sessions a node accepts is set with an environment variable such as SE_NODE_MAX_SESSIONS (check the image documentation for your version).

2One RemoteWebDriver per Test

RemoteWebDriver is the WebDriver implementation that sends commands over HTTP to a Grid instead of to a local driver. We create one at the start of each test and quit it at the end, so every test gets its own browser session.

Java
class SearchTest {
private static final URL GRID = gridUrl(); // http://grid:4444 from a system property
private WebDriver driver; // instance field: one per test instance
@BeforeEach
void startBrowser() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1366,900");
driver = new RemoteWebDriver(GRID, options); // Grid picks a free Chrome slot
}
@AfterEach
void stopBrowser() {
if (driver != null) driver.quit(); // frees the slot even if the test failed
}
@Test void findsProductByName() { /* uses driver */ }
@Test void showsEmptyState() { /* uses driver */ }
private static URL gridUrl() {
try { return URI.create(System.getProperty("grid.url", "http://grid:4444")).toURL(); }
catch (MalformedURLException e) { throw new IllegalStateException(e); }
}
}

Notice that the driver is an instance field. JUnit 5 by default creates a new instance of the test class for every test method, the per-method lifecycle, so each test, even when running in parallel, has its own driver field. The quit() in @AfterEach matters as much as the creation: a session that is never quit keeps its Grid slot until Grid's session timeout expires, and a few leaked sessions can starve the whole run. Grid 4 accepts sessions at its root URL; older Grid 3 setups used a /wd/hub path.

3Enabling Parallel Execution in JUnit 5

JUnit 5 runs tests sequentially unless we switch parallelism on. The configuration goes in src/test/resources/junit-platform.properties.

Properties
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent
junit.jupiter.execution.parallel.mode.classes.default=concurrent
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=8
# 8 tests at a time, matching 8 Grid slots (for example 2 nodes x 4 sessions)
Step throughTwo tests running at once against Grid
Test A threadTest B threadGrid routerChrome node
new session (Chrome)
new session (Chrome)
start browser in slot 1 and slot 2
session id a1
session id b2
commands for a1
commands for b2
quit a1: slot 1 free
quit b2: slot 2 free

9 messages in order. Play them, or step through them one at a time.

Each test owns one session id; nothing is shared between threads.

The first line turns the feature on. The two mode lines say that test methods and test classes may run concurrently by default. The fixed strategy with parallelism 8 tells JUnit to run up to eight tests at once. We choose a fixed number on purpose: the default dynamic strategy sizes the pool from the number of CPU cores on the machine running the tests, which has nothing to do with how many browsers the Grid can host. Recent JUnit versions also accept a max-pool-size setting for the fixed strategy; check your version's user guide. A test that must not run in parallel, for example one that changes global settings, can be marked @Isolated or use @ResourceLock.

4Why a Static Driver Breaks Everything

The most common parallel bug is a driver kept in a static field, often in a base class, created once for the whole suite. With parallel tests, every thread then sends commands to the same browser: test A navigates to search while test B is typing into checkout, and both fail with errors that make no sense.

If a framework needs a driver outside the test class, for example in page objects created by helpers, the safe pattern is a JUnit extension that creates the driver in beforeEach, stores it in the extension context's store or in a ThreadLocal, injects it as a parameter, and quits it in afterEach. A ThreadLocal works because JUnit runs each test's callbacks and the test itself on the same thread. But the simplest correct design is the one above: an instance field, created and quit around each test.

Java
// Broken in parallel: one browser shared by every thread
abstract class BaseTest { protected static WebDriver driver = new RemoteWebDriver(GRID, new ChromeOptions()); }
// test A: driver.get("/search") test B: driver.get("/checkout") -> both see the wrong page

5Keeping Test Data From Colliding

Separate browsers are only half of isolation. If two parallel tests log in as the same user and one changes the user's address while the other checks it, they still interfere through the application's database. So each test creates its own data, usually through the application's API, with unique values such as a user named with a UUID. Tests that need shared reference data must only read it. With that rule, the order and the timing of parallel tests stop mattering.

Java
String email = "user-" + UUID.randomUUID() + "@test.example"; // user-3f9c...@test.example
api.createUser(email, "Secret-123"); // only this test uses it

6Follow-up questions

What happens when tests request more sessions than Grid has slots?

Grid 4 does not reject the extra requests immediately. It puts them in its new session queue, and the distributor hands each one to a node as soon as a matching slot frees up. A queued request waits up to the session request timeout, which is 300 seconds by default and configurable, and if no slot becomes free in that time, the client gets a SessionNotCreatedException. So a parallelism higher than the slots mostly makes tests wait in the queue, which inflates their apparent duration and can cause timeouts, which is why we match parallelism to capacity. With autoscaled Grids, such as Kubernetes with KEDA, the queue length is exactly the signal used to add nodes.

Should each test class share one browser to save start-up time?

It is possible with a per-class lifecycle, but then tests in the class share cookies and state and cannot run concurrently with each other. A fresh session per test is slower to start but far more reliable; on Grid, session start is usually a second or two, small compared with a flaky failure.

How do we see what a parallel test did when it failed?

We take a screenshot and capture the page source in an extension before quit, and many Grid setups can record video per session. The session id should be in the test log so we can match logs, video and test.

7Conclusion

In this answer, we created a RemoteWebDriver per test in @BeforeEach and quit it in @AfterEach, kept in an instance field of JUnit's per-method test instance, enabled concurrency in junit-platform.properties with a fixed parallelism that matches Grid slots, and saw why a static driver makes threads share one browser. We recapped Grid 4's standalone, hub and node, and distributed modes and Docker images with a max sessions setting, and we kept test data isolated with unique API-created users.

What to say in the interviewAbout 90 seconds aloud

Open with

Each test gets its own browser. In @BeforeEach I create new RemoteWebDriver(gridUrl, new ChromeOptions()), pointing at the Grid router, for example http://grid:4444, and in @AfterEach I always call driver.quit(), so the slot is freed even when the test fails.

Then, if they want more

The driver lives in an instance field; JUnit 5 creates a new test instance per method by default, so each test, even in parallel, has its own field. I never use a static driver, because then parallel threads send commands to the same browser.

If page objects or helpers need the driver, I would use a JUnit extension that creates it in beforeEach, stores it in the extension store or a ThreadLocal, and injects it as a parameter.

To enable parallelism, I put junit.jupiter.execution.parallel.enabled=true and junit.jupiter.execution.parallel.mode.default=concurrent in junit-platform.properties, and I use the fixed strategy with a parallelism that matches Grid capacity, say eight for two nodes with four sessions each, rather than the dynamic strategy, which counts local CPU cores.

On the Grid side, version 4 can run standalone, hub and node, or fully distributed; with Docker I would use the selenium hub and node images and set the max sessions per node. Finally, data must not collide, so each test creates its own user through the API with a unique email.

If tests ask for more sessions than slots, Grid queues the requests and fails them with SessionNotCreatedException after the session request timeout, five minutes by default.

About this answer

Last updated
12 November 2026
Versions it names
JUnit 5
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.
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 "Run a JUnit 5 Selenium suite in parallel against Selenium Grid 4. Show how the tests...": https://scrivio-interview-library.netlify.app/java/ui-testing/722bd070aa/