Selenium Grid
Selenium Grid 完全指南(2025 最新版 · Selenium 4+)
Selenium Grid = 分布式自动化测试神器
实现 多浏览器、跨平台、并行执行,10 倍提升测试效率!
一、Selenium Grid 是什么?
| 名称 | 说明 |
|---|---|
| Selenium Grid | 分布式测试运行环境 |
| 核心功能 | 将测试脚本分发到多个 Node(节点) 上并行执行 |
| 适用场景 | 跨浏览器测试、CI/CD 集成、性能测试、云端执行 |
Hub (指挥中心)
│
├─ Node 1 (Chrome + Windows)
├─ Node 2 (Firefox + Linux)
└─ Node 3 (Safari + macOS)
二、Selenium Grid 4 架构(重大升级)
| 组件 | 作用 | 变化(vs Grid 3) |
|---|---|---|
| Hub | 接收请求,分发任务 | 合并为 Server |
| Node | 执行测试 | 支持 独立模式 |
| Router | 路由请求 | 新增 |
| Distributor | 任务分配 | 新增 |
| Session Map | 会话管理 | 新增 |
| Event Bus | 事件通信 | 新增 |
Selenium 4 推荐模式:全分布式模式(Fully Distributed)
三、快速上手:单机 Grid(开发调试)
1. 下载 Selenium Server(Grid)
# 官方下载(最新版)
wget https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.25.0/selenium-server-4.25.0.jar
或使用 Docker(推荐!)
2. 启动方式一:Docker Compose(最简单)
# docker-compose.yml
version: "3.8"
services:
selenium-hub:
image: selenium/hub:4.25.0
ports:
- "4444:4444"
- "4442:4442" # Event Bus
- "4443:4443" # Session Map
chrome:
image: selenium/node-chrome:4.25.0
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- NODE_MAX_INSTANCES=5
- NODE_MAX_SESSIONS=5
firefox:
image: selenium/node-firefox:4.25.0
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- NODE_MAX_INSTANCES=5
docker-compose up -d
访问控制台:http://localhost:4444
3. 启动方式二:独立 JAR 模式
# 启动 Hub
java -jar selenium-server-4.25.0.jar hub
# 启动 Node(另一终端)
java -jar selenium-server-4.25.0.jar node --hub http://localhost:4444
四、Python 连接 Grid
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# 连接 Grid Hub
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME
)
try:
driver.get("https://www.baidu.com")
print("标题:", driver.title)
finally:
driver.quit()
推荐:使用
options(Selenium 4+)
from selenium.webdriver.chrome.options import Options
options = Options()
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=options
)
五、并行测试(pytest + Grid)
pip install pytest pytest-xdist
# test_baidu.py
import pytest
from selenium import webdriver
@pytest.mark.parametrize("browser", ["chrome", "firefox"])
def test_baidu(browser):
caps = {
"chrome": webdriver.ChromeOptions(),
"firefox": webdriver.FirefoxOptions()
}
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=caps[browser]
)
try:
driver.get("https://www.baidu.com")
assert "百度" in driver.title
finally:
driver.quit()
pytest -n 2 test_baidu.py # 并行运行 2 个
六、生产级配置(Docker Swarm / Kubernetes)
1. Docker Swarm
# swarm-stack.yml
version: "3.8"
services:
hub:
image: selenium/hub:4.25.0
deploy:
replicas: 1
ports:
- "4444:4444"
chrome:
image: selenium/node-chrome:4.25.0
deploy:
replicas: 3
environment:
- HUB_HOST=hub
docker stack deploy -c swarm-stack.yml selenium
2. Kubernetes
# k8s-grid.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: selenium-hub
spec:
replicas: 1
template:
spec:
containers:
- name: hub
image: selenium/hub:4.25.0
ports:
- containerPort: 4444
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: chrome-node
spec:
replicas: 5
template:
spec:
containers:
- name: chrome
image: selenium/node-chrome:4.25.0
env:
- name: HUB_HOST
value: "selenium-hub"
kubectl apply -f k8s-grid.yaml
七、Grid 控制台(http://localhost:4444)
| 功能 | 说明 |
|---|---|
| Sessions | 查看当前运行的会话 |
| Nodes | 节点状态、浏览器版本 |
| Health Check | /wd/hub/status |
| GraphQL API | 高级查询 |
curl http://localhost:4444/status
八、高级功能
1. 浏览器版本指定
options = webdriver.ChromeOptions()
options.browser_version = "120"
options.platform_name = "Windows 11"
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=options
)
2. 视频录制(Node 配置)
chrome:
image: selenium/node-chrome:4.25.0
environment:
- SE_ENABLE_TRACING=true
- SE_SCREEN_RECORDING=true
下载视频:http://node:9000/download
3. 日志与截图
# 自动截图(失败时)
import base64
if not test_passed:
driver.save_screenshot("error.png")
九、常见问题 & 解决方案
| 问题 | 原因 | 解决 |
|---|---|---|
SessionNotCreatedException | Node 无匹配浏览器 | 检查 (browserName, version, platform) |
| 连接超时 | Hub 未启动 | docker ps 检查容器 |
| Node 未注册 | 网络问题 | 确保 HUB_HOST 正确 |
| 内存溢出 | 并发太多 | 限制 NODE_MAX_SESSIONS |
environment:
- NODE_MAX_SESSIONS=3
- NODE_MAX_INSTANCES=3
十、最佳实践总结
| 项目 | 推荐 |
|---|---|
| 启动 | Docker Compose > JAR |
| 并行 | pytest-xdist + Grid |
| CI/CD | GitHub Actions + Self-hosted Runner |
| 监控 | Prometheus + Grafana |
| 清理 | 每次测试后 driver.quit() |
# GitHub Actions 示例
- name: Run Selenium Tests
run: |
docker-compose up -d
pytest -n 4
docker-compose down
十一、一句话总结
Selenium Grid = 1 个 Hub + N 个 Node + 并行测试 = 自动化测试提速 10 倍
十二、完整启动脚本(一键部署)
#!/bin/bash
# start_grid.sh
cat > docker-compose.yml << EOF
version: "3.8"
services:
hub:
image: selenium/hub:4.25.0
ports: ["4444:4444"]
chrome:
image: selenium/node-chrome:4.25.0
depends_on: [hub]
environment: [HUB_HOST=hub, NODE_MAX_INSTANCES=5]
firefox:
image: selenium/node-firefox:4.25.0
depends_on: [hub]
environment: [HUB_HOST=hub, NODE_MAX_INSTANCES=5]
EOF
docker-compose up -d
echo "Grid 启动成功!访问 http://localhost:4444"
chmod +x start_grid.sh && ./start_grid.sh
下一步:
想看 企业级 Grid 集群模板(K8s + Helm + 监控 + 自动扩缩容)?
回复 grid_pro 我发你一个 生产级分布式测试平台!
当前最佳组合:
Docker + Selenium Grid 4 + pytest-xdist = 分布式测试无敌!
现在,你已掌握 Selenium 分布式测试!