What Is Selenium Grid?

Selenium Grid allows you to run tests on multiple machines (nodes) in parallel, across different browsers and operating systems. Instead of running 100 tests sequentially on one machine (taking 2 hours), you can run them across 10 nodes in parallel (taking 12 minutes).

Why You Need Grid

Without GridWith Grid
1 browser at a timeMultiple browsers simultaneously
Sequential executionParallel execution
Single OSMultiple OS (Windows, Linux, macOS)
100 tests × 2 min = 200 min100 tests ÷ 10 nodes = 20 min
One machineDistributed across machines

Selenium Grid 4 Architecture

Selenium Grid 4 introduced a modernized architecture:

┌─────────────────────────────────────┐
│           Selenium Grid             │
│                                     │
│  ┌──────────┐  ┌──────────────┐    │
│  │  Router   │  │  Distributor │    │
│  └────┬─────┘  └──────┬───────┘    │
│       │               │            │
│  ┌────┴─────┐  ┌──────┴───────┐    │
│  │  Session  │  │   Node       │    │
│  │  Map      │  │   Manager    │    │
│  └──────────┘  └──────────────┘    │
└─────────────────────────────────────┘
         │              │
    ┌────┴────┐   ┌────┴────┐
    │  Node 1 │   │  Node 2 │
    │ Chrome  │   │ Firefox │
    └─────────┘   └─────────┘

Components:

  • Router — entry point, receives WebDriver requests
  • Distributor — assigns sessions to available Nodes
  • Session Map — tracks which Node owns which session
  • Node — runs browsers and executes tests

Setting Up Selenium Grid

Standalone Mode (Single Machine)

# Download selenium-server
wget https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.18.0/selenium-server-4.18.0.jar

# Run standalone (hub + node on same machine)
java -jar selenium-server-4.18.0.jar standalone

Grid is now available at http://localhost:4444.

Hub and Node Mode (Multiple Machines)

# On the hub machine
java -jar selenium-server-4.18.0.jar hub

# On node machine 1 (Chrome)
java -jar selenium-server-4.18.0.jar node --hub http://hub-ip:4444

# On node machine 2 (Firefox)
java -jar selenium-server-4.18.0.jar node --hub http://hub-ip:4444

Connecting Tests to Grid

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.browser_version = "latest"
options.platform_name = "linux"

driver = webdriver.Remote(
    command_executor="http://localhost:4444",
    options=options
)

driver.get("https://app.example.com")
# ... run your tests
driver.quit()
ChromeOptions options = new ChromeOptions();
WebDriver driver = new RemoteWebDriver(
    new URL("http://localhost:4444"),
    options
);

Docker-Based Selenium Grid

Docker is the recommended way to run Selenium Grid. It provides consistent, reproducible environments.

Docker Compose Setup

# docker-compose.yml
version: "3"
services:
  selenium-hub:
    image: selenium/hub:4.18.0
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

  chrome-node:
    image: selenium/node-chrome:4.18.0
    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
    deploy:
      replicas: 2

  firefox-node:
    image: selenium/node-firefox:4.18.0
    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

  edge-node:
    image: selenium/node-edge:4.18.0
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
# Start the grid
docker compose up -d

# Scale chrome nodes
docker compose up -d --scale chrome-node=5

# View grid status
open http://localhost:4444/ui

Dynamic Grid with Docker

Selenium Grid 4 supports dynamic Docker-based node creation:

# grid-config.toml
[docker]
url = "http://localhost:2375"
video-image = "selenium/video:latest"
assets-path = "/opt/selenium/assets"

[[docker.configs]]
image = "selenium/standalone-chrome:latest"
stereotype = '{"browserName": "chrome"}'
max-sessions = 4

[[docker.configs]]
image = "selenium/standalone-firefox:latest"
stereotype = '{"browserName": "firefox"}'
max-sessions = 4

Parallel Test Execution

TestNG (Java)

<!-- testng.xml -->
<suite name="Parallel Suite" parallel="tests" thread-count="3">
  <test name="Chrome Tests">
    <parameter name="browser" value="chrome"/>
    <classes>
      <class name="tests.LoginTest"/>
      <class name="tests.CheckoutTest"/>
    </classes>
  </test>
  <test name="Firefox Tests">
    <parameter name="browser" value="firefox"/>
    <classes>
      <class name="tests.LoginTest"/>
      <class name="tests.CheckoutTest"/>
    </classes>
  </test>
  <test name="Edge Tests">
    <parameter name="browser" value="edge"/>
    <classes>
      <class name="tests.LoginTest"/>
      <class name="tests.CheckoutTest"/>
    </classes>
  </test>
</suite>

pytest (Python)

# Run tests in parallel using pytest-xdist
pip install pytest-xdist
pytest tests/ -n 4  # 4 parallel workers

WebdriverIO (JavaScript)

// wdio.conf.js
exports.config = {
  maxInstances: 10,
  capabilities: [
    { browserName: 'chrome', maxInstances: 5 },
    { browserName: 'firefox', maxInstances: 3 },
    { browserName: 'MicrosoftEdge', maxInstances: 2 }
  ],
  services: ['selenium-standalone'],
};

Cloud Grid Providers

For enterprise-scale testing, cloud providers offer managed Selenium Grids:

ProviderBrowsersReal DevicesPricing
BrowserStack3,000+YesFrom $29/mo
Sauce Labs2,000+YesFrom $39/mo
LambdaTest3,000+YesFrom $15/mo

BrowserStack Integration

from selenium import webdriver

options = webdriver.ChromeOptions()
options.set_capability('bstack:options', {
    'os': 'Windows',
    'osVersion': '11',
    'browserVersion': 'latest',
    'projectName': 'E-Commerce Tests',
    'buildName': 'Build 1.0',
})

driver = webdriver.Remote(
    command_executor=f'https://{USERNAME}:{ACCESS_KEY}@hub-cloud.browserstack.com/wd/hub',
    options=options
)

Grid Monitoring and Debugging

Grid UI Dashboard

Access http://localhost:4444/ui to see:

  • Connected nodes and their capabilities
  • Active sessions
  • Queue length
  • Node health status

Video Recording

Docker Selenium supports video recording of test sessions:

chrome-node:
  image: selenium/node-chrome:4.18.0
  volumes:
    - ./videos:/opt/selenium/assets
  environment:
    - SE_RECORD_VIDEO=true

Exercise: Set Up a Selenium Grid

  1. Create a Docker Compose file with Hub, 2 Chrome nodes, 1 Firefox node
  2. Start the grid and verify all nodes appear in the dashboard
  3. Modify an existing Selenium test to use RemoteWebDriver pointing to the grid
  4. Run the same test on Chrome and Firefox using capabilities
  5. Run tests in parallel using pytest-xdist or TestNG
  6. Compare execution time: sequential vs parallel

Key Takeaways

  • Selenium Grid enables parallel, distributed test execution across browsers and machines
  • Docker Compose is the easiest way to set up and manage a Grid
  • Grid 4 has modernized architecture with Router, Distributor, Session Map, and Nodes
  • Cloud providers (BrowserStack, Sauce Labs) offer managed grids for enterprise scale
  • Parallel execution can reduce test suite time by 80-90%
  • Always monitor Grid health through the built-in dashboard