Selenium Grid

1. Introduction to Selenium Grid

Selenium Grid is an important component of the Selenium suite, allowing you to execute tests in parallel on multiple different machines, browsers, and operating systems.

With Selenium Grid, you can achieve distributed testing, thereby improving test efficiency and reducing test time.

The core idea of Selenium Grid is to distribute test tasks to multiple nodes (Nodes) for execution, and these nodes are centrally managed by a hub node (Hub).

1.1 Key Features of Selenium Grid

  • Parallel Testing: Run tests simultaneously on multiple browsers and operating systems, significantly reducing test time.
  • Cross-Browser Testing: Supports running tests on different browsers (such as Chrome, Firefox, Edge, etc.), ensuring the application's compatibility across various environments.
  • Cross-Platform Testing: Supports running tests on different operating systems (such as Windows, macOS, Linux, etc.), verifying the application's performance on different platforms.
  • Dynamic Scaling: Nodes can be dynamically added or removed as needed, flexibly responding to changes in testing requirements.

1.2 Architecture of Selenium Grid

Selenium Grid uses a Hub-Node architecture:

  • Hub: The central node, responsible for receiving test requests and distributing tasks to appropriate nodes for execution.
  • Node: The execution node, responsible for actually executing test tasks. Each node can be configured with different browsers and operating systems.

2. Configuring Selenium Grid

Before using Selenium Grid, you need to perform some basic configuration. The following are the configuration steps based on Selenium 4.

2.1 Installing Java

Selenium Grid relies on the Java runtime environment, so you need to install Java first. You can install Java by following these steps:

  1. VisitOracle JavaorOpenJDKDownload the Java version suitable for your operating system.
  2. Install Java and configure environment variables.

2.2 Downloading Selenium Server

Selenium Server is the core component of Selenium Grid. You canSelenium Official WebsiteDownload the latest version of Selenium Server.

  1. Downloadselenium-server-<version>.jarthe file.
  2. Save the downloaded file to an appropriate directory.

2.3 Starting the Hub

After configuring Java and Selenium Server, you can start the Hub. Open a command-line tool, navigate to the directory whereselenium-server-<version>.jarthe file is saved, then run the following command:

Example

java -jar selenium-server-<version>.jar hub

This will start the Hub. By default, the Hub listens on port 4444. You can access via your browserhttp://localhost:4444to view the Hub's status.

2.4 Starting the Node

Next, you need to start one or more Nodes to execute test tasks. On another machine or in a different terminal on the same machine, run the following command:

Example

java -jar selenium-server-<version>.jar node --hub http://<hub-ip>:4444

Where,<hub-ip>is the IP address of the machine where the Hub is located. If the Hub and Node are on the same machine, you can uselocalhost。

2.5 Configuring the Node's Browser and Operating System

You can configure the Node's browser and operating system through command-line parameters. For example, the following command configures a Node that supports Chrome and Firefox browsers:

Example

java -jar selenium-server-<version>.jar node --hub http://<hub-ip>:4444 --browser "browserName=chrome" --browser "browserName=firefox"

3. Running Tests Remotely

After configuring Selenium Grid, you can run tests remotely by writing test scripts. The following is a Python example based on Selenium 4.

3.1 Installing the Selenium Python Bindings

First, you need to install the Python bindings for Selenium:

Example

pip install selenium

3.2 Writing a Test Script

The following is a simple test script that executes tests on a remote node via Selenium Grid:

Example

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Set Desired Capabilities
capabilities = DesiredCapabilities.CHROME.copy()
capabilities['platform'] = 'WINDOWS'  # Specify the operating system
capabilities['version'] = 'latest'    # Specify the browser version

# Connect to Selenium Grid Hub
driver = webdriver.Remote(
    command_executor='http://<hub-ip>:4444/wd/hub',
    desired_capabilities=capabilities
)

# Execute test
driver.get("https://www.example.com")
print(driver.title)
driver.quit()

3.3 Running the Test Script

Save the script and run it. The script will distribute the test task to an appropriate Node for execution through the Selenium Grid Hub.


4. Advanced Configuration of Selenium Grid

4.1 Configuring the Node's Browser and Operating System

When starting a Node, you can specify the supported browsers and operating systems via parameters:

java -jar selenium-server-<version>.jar node --hub http://<hub-ip>:4444 --browser "browserName=chrome,platform=WINDOWS"

4.2 Running Selenium Grid with Docker

Selenium provides official Docker images, allowing you to quickly start a Hub and Node:

# 启动 Hub
docker run -d -p 4444:4444 --name selenium-hub selenium/hub

# 启动 Node
docker run -d --link selenium-hub:hub selenium/node-chrome

4.3 Configuring Grid Load Balancing

You can configure load balancing by starting multiple Hubs and Nodes to improve test concurrency.

Other Extensions