QA Interview Hub · Knowledge base · Quizzes · Українська

Selenium + Java › Framework and runs

Selenium Grid 4 and Docker: how it is built and how to run it

Selenium Grid routes RemoteWebDriver commands to remote browsers so tests run in parallel across machines, browsers and operating systems. Grid 4 consists of Router, Distributor, Session Map, New Session Queue, Node and Event Bus. A started container is not necessarily ready, so before the tests /status is polled until ready becomes true. Threads are matched to the slots.

Selenium · Grid

The Grid container is up, yet the first CI test fails with "could not start a session". Why?

On CI a job starts Grid in a container and runs tests right away. For the first few seconds Grid is still registering the node: a request for a new session either fails or waits in a queue, and the test is red through no fault of the application. Nothing strange: "started" does not mean "ready". To run Grid with confidence, you need to understand what it consists of, how a request passes through it and how to set up the Docker images. That is what the lesson covers.

How it works and why. Selenium Grid routes client commands to remote browsers: parallel runs across many machines, different browser versions and operating systems. A test connects through RemoteWebDriver to one URL (port 4444 by default), and Grid handles the rest. Grid 4 has three deployment modes:

  • Standalone: everything in one process; the simplest for development and small suites;
  • Hub and Node: one Hub and several Nodes, each with its own browsers, possibly on different operating systems;
  • Distributed: components as separate processes for large scale.

Inside, Grid 4 consists of components with their own roles: the Router accepts requests and routes them, the Distributor looks for a free slot on a Node, the Session Map knows which session is on which Node, the New Session Queue holds new session requests when there are no slots, the Node starts browsers, and the Event Bus exchanges events between components. State is visible at /status and in the UI at /ui.

The path of a new session request

Routeraccepts the request from RemoteWebDriverNew Session Queuewaits if there are no free slotsDistributorlooks for a Node with the needed browserNodestarts the browser, returns the session idSession Mapremembers: session → Node
Figure 1. If there are no free slots, the request waits in the queue, and once the node is registered the Router already knows where to send the next commands of that session.

Example: Docker and CI. Ready-made images (selenium/standalone-chrome, selenium/standalone-firefox, the pair selenium/hub and selenium/node-chrome) give a browser without installing it on the machine. The simplest start:

docker run -d -p 4444:4444 -p 7900:7900 --shm-size="2g" \
  selenium/standalone-chrome:4.53.0-20261120
  • --shm-size (or mounting /dev/shm) is needed because Chrome uses shared memory heavily: with a small amount the browser crashes on heavy pages;
  • port 7900 shows the session through noVNC in a browser, handy for debugging;
  • a container runs one session by default; SE_NODE_MAX_SESSIONS allows more (up to the container's processor count, beyond it only with SE_NODE_OVERRIDE_MAX_SESSIONS=true), but the limit is set by the container's resources, not by the number in a setting.

For several nodes, for example in docker compose, a Hub and a separate Node are started, and the Node finds the Hub through the Event Bus:

services:
  selenium-hub:
    image: selenium/hub:4.53.0-20261120
    ports: ["4444:4444"]
  chrome:
    image: selenium/node-chrome:4.53.0-20261120
    shm_size: 2gb
    depends_on: [selenium-hub]
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_SESSIONS=4   # how many sessions a node holds

The problem from the start of the lesson is solved by waiting for readiness: before running tests, poll /status until the ready field becomes true:

# wait for Grid to be ready before running tests
until curl -sf http://localhost:4444/status | grep -q '"ready": *true'; do
  sleep 1
done
mvn test -Dgrid.url=http://localhost:4444

The test receives the Grid URL from outside (a system property or an environment variable), so the same code works with a local browser and with Grid. Uploading a file to a site from a remote node needs a LocalFileDetector: without it the path points at the node machine, where the file does not exist.

ChromeOptions options = new ChromeOptions();
URL grid = URI.create(System.getProperty("grid.url",
    "http://localhost:4444")).toURL();
RemoteWebDriver driver = new RemoteWebDriver(grid, options);

// the upload file is on the test machine while the browser is on a node
driver.setFileDetector(new LocalFileDetector());
driver.findElement(By.cssSelector("input[type=file]"))
    .sendKeys(Path.of("data/report.csv").toAbsolutePath().toString());

Limits and alternatives. Grid does not make tests more stable: it adds a network between the test and the driver, so each call costs more (the lesson “How Selenium WebDriver works: client, protocol, driver, browser”), and node resources become a bottleneck. The number of parallel tests is matched to the number of slots and memory: excess threads only lengthen the queue and the timeouts. For a single browser in a single CI job Grid is often unnecessary: it is enough to run the browser in the same container through Selenium Manager (the lesson “Selenium Manager, browser options and RemoteWebDriver”). Grid is justified when different browsers and operating systems are needed, a shared farm for many teams, or isolation of browsers from the test machine. Cloud platforms offer the same as a service over the same protocol.

Common mistakes.

  • Running tests right after docker run without waiting for /status: the first sessions fail or hang.
  • Not setting --shm-size: Chrome in the container crashes on heavy pages with an unclear error.
  • Using more threads in tests than slots in Grid: extra sessions stand in the queue and fail by timeout.
  • Forgetting LocalFileDetector on upload: the path is looked up on the node, not on the test machine.
  • Using an image with no pinned version: a browser update changes test behaviour without any code change.

Check yourself

Grid is started as a job service container, the test starts at once and fails with a session creation error, while a rerun passes. What is the cause and what should change?

Answer

The container is running but Grid is not ready: the node has not registered, there are no slots. Before running tests, poll /status until ready is true, and in Docker Compose a healthcheck with a dependency on it can be added.

The tests use 8 threads while Grid has one node with SE_NODE_MAX_SESSIONS=4 and four processors. What happens, and what matters more than raising that number?

Answer

Four sessions start and four wait in the New Session Queue and may fail by timeout. Raising the number helps little if container resources (CPU, memory, shm) are insufficient: it matters more to match threads to real capacity or add nodes.

In an interview. Junior: Selenium Grid lets tests run on remote browsers in parallel. A test connects through RemoteWebDriver to a URL, and Grid distributes sessions across nodes. I start it with Docker images. Middle/Senior: I know the parts of Grid 4: Router, Distributor, Session Map, the new session queue and Node. In CI I wait for readiness at /status, pin the image version, set --shm-size and match the thread count to the slots. I use Grid when different browsers and operating systems are needed, not by default.

More articles in this chapter