Selenium Grid 分布式浏览器测试

FreeGuideOnline 最新 2026-07-08

mermaid graph LR A[测试脚本] -->|WebDriver 请求| B(Hub) B -->|分配| C[Node 1 - Chrome/Firefox] B -->|分配| D[Node 2 - Safari/Edge] C & D -->|返回结果| B B -->|返回| A


### 环境准备与安装
Selenium Grid 自 Selenium 4 起可以独立下载 Jar 包运行,也支持通过容器或 WebDriverManager 简化配置。

#### 1. 系统要求
- Java 11 或更高版本(Hub 和 Node 均基于 Java 运行)
- 节点机器上需安装对应的浏览器(Chrome、Firefox 等)及对应版本的 WebDriver 可执行文件(或使用自动管理工具)

#### 2. 下载与启动 Grid
官方提供单机快速体验模式(Standalone),也支持完整 Hub-Node 模式。

**使用独立 Jar 启动(适用于测试和小规模)**
```bash
# 下载最新 Selenium Server Jar
wget https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.x/selenium-server-4.x.y.jar

# 以 Standalone 模式启动(同时充当 Hub 和 Node)
java -jar selenium-server-4.x.y.jar standalone

Standalone 模式会同时启动 Hub 和单个 Node,默认监听 http://localhost:4444

分体式部署(生产推荐)

  1. 启动 Hub
java -jar selenium-server-4.x.y.jar hub
  1. 在其他机器上启动 Node,并注册到 Hub 的地址
java -jar selenium-server-4.x.y.jar node --hub http://<hub-ip>:4444

3. 使用 Docker 快速搭建(强烈推荐)

# 启动一个完整的 Grid (Hub + 多个浏览器节点)
docker run -d -p 4444:4444 --name selenium-hub selenium/hub:latest
docker run -d --link selenium-hub:hub selenium/node-chrome:latest
docker run -d --link selenium-hub:hub selenium/node-firefox:latest

此时访问 http://localhost:4444/grid/console 即可看到在线节点。

配置 Node 的能力与并发数

Node 启动时需要声明可用的浏览器及最大会话数。常见参数如下:

java -jar selenium-server-4.x.y.jar node \
  --hub http://192.168.1.100:4444 \
  --max-sessions 5 \
  --browser "browserName=chrome,maxInstances=3" \
  --browser "browserName=firefox,maxInstances=2"
  • max-sessions:该 Node 允许同时执行的会话总数。
  • maxInstances:该类型浏览器允许的最大并发实例数。

编写分布式测试脚本

使用 Selenium Grid 时,客户端代码变化非常小,仅需将 WebDriver 的创建指向 Hub 的远程地址。

Java 示例

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;

public class GridTest {
    public static void main(String[] args) throws Exception {
        // 配置选项(如浏览器版本、平台)
        ChromeOptions options = new ChromeOptions();
        options.setCapability("browserVersion", "120");
        options.setCapability("platformName", "Windows");

        // 创建 RemoteWebDriver,指向 Hub
        WebDriver driver = new RemoteWebDriver(
            new URL("http://<hub-ip>:4444/"),
            options
        );

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

Python 示例

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

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

driver = webdriver.Remote(
    command_executor='http://<hub-ip>:4444',
    options=options
)

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

关键点:无论何种语言,只需将 WebDriver 实例换成 Remote 模式,并指定 Hub URL 及必要的浏览器配置。

配置并行执行

并行执行依赖测试框架自身的多线程能力。以 Java + TestNG 为例:

import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.net.URL;

public class ParallelGridTest {

    @Test(threadPoolSize = 3, invocationCount = 3)
    public void testInParallel() throws Exception {
        ChromeOptions options = new ChromeOptions();
        WebDriver driver = new RemoteWebDriver(
            new URL("http://localhost:4444/"), options
        );
        // 执行测试步骤
        driver.get("https://www.example.com");
        System.out.println(Thread.currentThread().getName() + " -> " + driver.getTitle());
        driver.quit();
    }
}
  • threadPoolSize 控制并发线程数。
  • 确保 Grid 中有足够节点或足够 maxInstances 来承接这些并发请求。

Python + pytest-xdist 并行

pytest -n 4 --dist=loadscope