</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

What is Selenium Grid? How does it work?

Answer

Selenium Grid

Selenium Grid lets you run Selenium tests on multiple machines and multiple browsers in parallel, reducing overall test suite execution time.

Selenium Grid 3 Architecture (Hub-Node)

CODE
┌──────────────────────────────────┐
│           HUB (Central Server)    │
│  localhost:4444                   │
│  Receives test commands           │
│  Manages node registry           │
│  Routes tests to matching node    │
└──────────────────────────────────┘
          │            │
    ┌─────┴──┐   ┌─────┴──┐
    │ Node 1  │   │ Node 2  │
    │ Chrome  │   │ Firefox │
    │ Windows │   │ Linux   │
    └─────────┘   └─────────┘

Selenium 4 Grid (Unified Architecture)

CODE
# Selenium 4 Grid modes:

# 1. Standalone (Hub + Node in ONE process)
java -jar selenium-server-4.x.jar standalone

# 2. Distributed (separate components)
java -jar selenium-server-4.x.jar router   --port 4444
java -jar selenium-server-4.x.jar distributor
java -jar selenium-server-4.x.jar node     --port 5555

Starting Grid (Selenium 4 Standalone Mode)

Bash
# Terminal 1 — Start Grid
java -jar selenium-server-4.27.0.jar standalone --port 4444

# Open Grid UI at: http://localhost:4444/ui

Connecting Tests to Grid — RemoteWebDriver

Java
// Instead of new ChromeDriver(), use RemoteWebDriver
URL gridUrl = new URL("http://localhost:4444/wd/hub");

// Specify browser capabilities
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");

// Connect to Grid
WebDriver driver = new RemoteWebDriver(gridUrl, options);

driver.get("https://example.com");
System.out.println(driver.getTitle());
driver.quit();

Cross-Browser Parallel Test with TestNG + Grid

Java
// BaseTest.java — reads browser from TestNG parameter
@Parameters("browser")
@BeforeMethod
public void setUp(@Optional("chrome") String browser) throws MalformedURLException {
    URL gridUrl = new URL("http://localhost:4444/wd/hub");
    MutableCapabilities options;

    switch (browser.toLowerCase()) {
        case "firefox": options = new FirefoxOptions(); break;
        case "edge":    options = new EdgeOptions();    break;
        default:        options = new ChromeOptions();  break;
    }

    WebDriver driver = new RemoteWebDriver(gridUrl, options);
    driver.manage().window().maximize();
    DriverManager.setDriver(driver);
}
XML
<!-- testng.xml — run same tests on 3 browsers simultaneously -->
<suite name="Cross-Browser Grid" parallel="tests" thread-count="3">

    <test name="Chrome">
        <parameter name="browser" value="chrome"/>
        <classes><class name="com.automateqa.tests.LoginTest"/></classes>
    </test>

    <test name="Firefox">
        <parameter name="browser" value="firefox"/>
        <classes><class name="com.automateqa.tests.LoginTest"/></classes>
    </test>

    <test name="Edge">
        <parameter name="browser" value="edge"/>
        <classes><class name="com.automateqa.tests.LoginTest"/></classes>
    </test>

</suite>

Docker Selenium Grid (Industry Standard)

YAML
# docker-compose.yml — spin up Grid with Chrome + Firefox nodes
version: "3"
services:
  selenium-hub:
    image: selenium/hub:4.27.0
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

  chrome-node:
    image: selenium/node-chrome:4.27.0
    shm_size: 2g
    depends_on: [selenium-hub]
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_NODE_MAX_SESSIONS=3
    deploy:
      replicas: 3   # 3 Chrome nodes

  firefox-node:
    image: selenium/node-firefox:4.27.0
    shm_size: 2g
    depends_on: [selenium-hub]
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
Bash
docker-compose up -d
# Grid UI: http://localhost:4444/ui

Grid Benefits

Without GridWith Grid
100 tests × 2 min each = 200 min100 tests in parallel = ~10 min
1 browserMultiple browsers simultaneously
1 OSMultiple OS simultaneously
Local onlyRemote cloud machines

Follow AutomateQA

Related Topics