Selenium - Grid
Selenium - Grid
Section titled “Selenium - Grid”Selenium Grid allows you to run your WebDriver tests across multiple machines, browsers, and operating systems in parallel. This significantly speeds up test execution time, especially for large test suites, and facilitates cross-browser compatibility testing.
Instead of running tests sequentially on your local machine, Grid distributes them to available ‘Nodes’ that match the requested browser and platform capabilities.
Selenium Grid 4 Architecture
Section titled “Selenium Grid 4 Architecture”Selenium Grid 4 introduced a modernized architecture, making setup and scaling easier. Key components include:
- Router: The entry point for all incoming
RemoteWebDriverrequests. It queries the Distributor to find an available Node. - Distributor: Manages the list of available Nodes. It receives test requests (Capabilities) from the Router and assigns them to a matching, free Node.
- Session Map: Keeps track of active sessions, mapping session IDs to the Node executing the session.
- Node: The machine (physical or virtual) where the actual browser automation runs. A Node registers itself with the Distributor, advertising the browsers and capabilities it supports.
- Event Bus (Optional): Facilitates communication between components, useful for distributed setups.
- New Session Queue: Holds incoming requests when no matching Node is immediately available.
Grid 4 supports different running modes:
- Standalone: Simplest mode. A single process acts as Router, Distributor, Session Map, and Node. Ideal for running a small number of parallel tests locally.
- Hub and Node(s): Classic mode. One machine runs the central Hub (Router, Distributor, Session Map, Event Bus), and other machines run Nodes that connect to the Hub.
- Distributed: Each component (Router, Distributor, Session Map, Event Bus, Nodes) can run as a separate process, allowing for maximum scalability and resilience.
- Docker: Selenium provides official Docker images for easily setting up Grid components in containers, highly recommended for dynamic scaling.
Setting up Selenium Grid (Standalone Mode)
Section titled “Setting up Selenium Grid (Standalone Mode)”This is the easiest way to start with Grid locally.
Step 1: Download Selenium Server: Download the latest Selenium Server JAR file from the official Selenium website: https://www.selenium.dev/downloads/
Step 2: Ensure Browser Drivers: Make sure the necessary browser drivers (e.g., chromedriver, geckodriver) are either in your system’s PATH or their location is specified when starting the server (Grid 4 often detects them automatically if they are in the PATH).
Step 3: Start Standalone Server: Open a terminal or command prompt, navigate to the directory where you downloaded the JAR, and run:
java -jar selenium-server-<version>.jar standaloneReplace <version> with the actual version number. This will start the Grid in standalone mode, usually listening on http://localhost:4444. It will automatically detect available browsers on the machine.
Step 4: Access UI (Optional): Open your web browser and navigate to http://localhost:4444. You should see the Grid UI, showing available nodes and running sessions.
Setting up Selenium Grid (Hub and Node Mode)
Section titled “Setting up Selenium Grid (Hub and Node Mode)”For running tests across multiple machines.
Step 1: Start the Hub: On your central machine (e.g., 192.168.1.100), run:
java -jar selenium-server-<version>.jar hubThe Hub will start, usually listening on port 4444.
Step 2: Start Node(s): On each machine where you want to run browsers (Node machines), download the Selenium Server JAR and ensure drivers are available. Then run:
# Example for a Node machine# Grid will auto-detect browsers/drivers on the nodejava -jar selenium-server-<version>.jar node --hub http://192.168.1.100:4444Replace 192.168.1.100 with the actual IP address or hostname of your Hub machine. The Node will register itself with the Hub. Repeat this step for all Node machines.
Step 3: Verify Setup: Check the Grid UI on the Hub (http://192.168.1.100:4444) to see the registered Nodes.
Running Tests on the Grid
Section titled “Running Tests on the Grid”To run your WebDriver tests on the Grid, you need to use RemoteWebDriver instead of a specific driver (like ChromeDriver). You also need to specify the Grid Hub’s URL and the desired capabilities (browser, platform).
package com.example.gridtests;
import org.openqa.selenium.WebDriver;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import org.testng.Assert;import org.testng.annotations.*;
import java.net.MalformedURLException;import java.net.URL;
public class GridTest {
WebDriver driver; URL gridUrl;
@Parameters("browser") // Get browser parameter from testng.xml @BeforeTest public void setup(String browser) throws MalformedURLException { gridUrl = new URL("http://localhost:4444"); // Or your Hub URL
DesiredCapabilities caps = new DesiredCapabilities(); if (browser.equalsIgnoreCase("chrome")) { caps.setBrowserName("chrome"); } else if (browser.equalsIgnoreCase("firefox")) { caps.setBrowserName("firefox"); } else { throw new IllegalArgumentException("Unsupported browser: " + browser); } // You can set other capabilities like platformName, browserVersion etc. // caps.setPlatformName(Platform.WINDOWS);
System.out.println("Requesting " + browser + " from Grid: " + gridUrl); driver = new RemoteWebDriver(gridUrl, caps); driver.manage().window().maximize(); }
@Test public void testGridExecution() { driver.get("https://www.google.com"); System.out.println("Page title on " + driver.getCapabilities().getBrowserName() + ": " + driver.getTitle()); Assert.assertEquals(driver.getTitle(), "Google"); }
@AfterTest public void tearDown() { if (driver != null) { driver.quit(); } }}You would typically run this using a TestNG XML suite file to pass the browser parameter and potentially run tests in parallel:
<?xml version="1.0" encoding="UTF-8"?><!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 -->
<test name="Firefox Test"> <parameter name="browser" value="firefox"/> <classes> <class name="com.example.gridtests.GridTest"/> </classes> </test> <!-- Test --></suite> <!-- Suite -->Further Considerations
Section titled “Further Considerations”- Docker: Using Selenium’s Docker images simplifies setup and scaling significantly.
- Configuration: Nodes can be configured via command-line flags or TOML configuration files to specify drivers, maximum sessions, stereotypes, etc.
- Cloud Grids: Services like Sauce Labs, BrowserStack, LambdaTest offer managed Selenium Grids in the cloud, eliminating the need to maintain your own infrastructure.