Skip to content

selenium_grid

Selenium Grid allows you to run your WebDriver tests across multiple machines, operating systems, and browsers in parallel. This significantly speeds up test execution time, especially for large test suites, and facilitates cross-browser/cross-platform testing.

Selenium Grid 4, the latest version, introduced a redesigned architecture making it more robust, scalable, and easier to manage compared to previous versions.

Grid 4 consists of several components that work together:

  • Router: The entry point for all incoming new session requests from your tests (RemoteWebDriver). It directs requests to the appropriate component.
  • Distributor: Manages the available Nodes and assigns new session requests to a suitable Node based on the requested capabilities (browser, version, platform).
  • Session Map: Keeps track of the active sessions and maps session IDs to the Node executing the session.
  • Node: The machine (physical or virtual, potentially a Docker container) where the actual browser is launched and controlled by WebDriver. A Grid can have multiple Nodes.
  • Event Bus: Facilitates communication between the different components (Router, Distributor, Session Map, Nodes).

Unlike Grid 3, you don’t download separate Hub and Node JARs. The same Selenium Server JAR (selenium-server-X.Y.Z.jar) contains all components. Grid 4 also has built-in Docker support.

  • Standalone: The simplest mode. Runs all Grid components (Router, Distributor, Session Map, Node) within a single process on a single machine. Useful for simple local parallel execution. Automatically detects installed browsers/drivers.
  • Hub and Node: The traditional setup. One machine runs the core Grid components (Router, Distributor, Session Map, Event Bus) acting as the ‘Hub’. Other machines run only the Node component and register themselves with the Hub.
  • Distributed: Each Grid component (Router, Distributor, Session Map, Event Bus, Nodes) can run as a separate process, potentially on different machines, offering maximum scalability and resilience.

Step 1: Download the Selenium Server JAR from the official Selenium website: https://www.selenium.dev/downloads/

Step 2: Ensure you have the necessary browser drivers (e.g., chromedriver, geckodriver) installed and available in your system’s PATH, or specify their locations when starting the Nodes/Standalone server.

Open your terminal or command prompt and run:

java -jar selenium-server-X.Y.Z.jar standalone

The server will start, detect available drivers/browsers, and be ready to accept sessions at http://localhost:4444.

1. Start the Hub: On the machine designated as the Hub, run:

java -jar selenium-server-X.Y.Z.jar hub

The Hub will start and listen for Nodes (default port 4444).

2. Start the Node(s): On each machine that will run browsers, run:

# Ensure drivers (e.g., chromedriver) are in PATH or specify path:
# java -Dwebdriver.chrome.driver=/path/to/chromedriver -jar selenium-server-X.Y.Z.jar node
java -jar selenium-server-X.Y.Z.jar node --hub http://<HUB_IP_ADDRESS>:4444

Replace <HUB_IP_ADDRESS> with the actual IP address or hostname of the Hub machine. The Node will detect local drivers/browsers and register with the Hub.

You can specify driver locations using -Dwebdriver.chrome.driver=/path/to/driver before the -jar argument, or configure nodes using TOML files for more advanced setups (e.g., specific browser versions, number of parallel sessions per node).

Once the Grid (Standalone or Hub) is running, you can access its UI in your browser, typically at http://localhost:4444. This UI shows registered nodes, available browser slots, and active sessions.

To run your tests on the Grid, you need to:

  1. Use RemoteWebDriver instead of a specific driver class (like ChromeDriver).
  2. Provide the URL of the Grid Hub/Standalone server.
  3. Specify the desired capabilities (browser name, version, platform) using Browser Options classes (e.g., ChromeOptions, FirefoxOptions).

Example using Java, TestNG, and RemoteWebDriver:

package com.example.gridtests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import java.net.MalformedURLException;
import java.net.URL;
public class GridTest {
private WebDriver driver;
private final String gridUrl = "http://localhost:4444"; // Or your Hub URL
@Parameters("browser")
@BeforeMethod
public void setUp(String browser) throws MalformedURLException {
URL url = new URL(gridUrl);
if (browser.equalsIgnoreCase("chrome")) {
ChromeOptions options = new ChromeOptions();
// Add any specific Chrome options if needed
// options.addArguments("--headless");
driver = new RemoteWebDriver(url, options);
System.out.println("Starting Chrome session on Grid...");
} else if (browser.equalsIgnoreCase("firefox")) {
FirefoxOptions options = new FirefoxOptions();
// Add any specific Firefox options if needed
driver = new RemoteWebDriver(url, options);
System.out.println("Starting Firefox session on Grid...");
} else {
throw new IllegalArgumentException("Unsupported browser: " + browser);
}
driver.manage().window().maximize();
}
@Test
public void simpleGridTest() {
driver.get("https://www.google.com");
System.out.println("Page Title: " + driver.getTitle());
// Add your test steps here...
assert driver.getTitle().contains("Google");
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
System.out.println("Browser session closed.");
}
}
}

You would typically run this using a TestNG XML suite file that passes the browser parameter, allowing parallel execution across different browsers registered on the Grid.

<!-- Example testng.xml -->
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Grid Suite" parallel="tests" thread-count="2">
<test name="Chrome Test">
<parameter name="browser" value="chrome"/>
<classes>
<class name="com.example.gridtests.GridTest"/>
</classes>
</test>
<test name="Firefox Test">
<parameter name="browser" value="firefox"/>
<classes>
<class name="com.example.gridtests.GridTest"/>
</classes>
</test>
</suite>
  • Parallel Execution: Drastically reduces total test execution time.
  • Cross-Browser/Platform Testing: Easily test on different configurations.
  • Scalability: Add more Node machines as your testing needs grow.
  • Centralized Management: Manage browser versions and configurations from the Grid nodes.

Selenium Grid is an essential tool for any team serious about comprehensive and efficient web application testing.