UI自动化-Grid分布式运行

实现 Selenium Grid 分布式运行测试用例,核心是通过 Hub(中心节点) 统一调度、 Node(代理节点) 执行用例,实现多浏览器 / 多机器并行测试

  • Hub:Grid 的 "调度中心",负责接收测试请求、管理所有 Node 节点、分发用例到可用的 Node;
  • Node:连接到 Hub 的 "执行节点",提供浏览器运行环境(Chrome/Firefox 等),执行 Hub 分发的用例;
  • 核心价值:多机器 / 多浏览器并行执行用例,提升测试效率;支持跨平台(Windows/Linux/Mac)、跨浏览器测试。

1. 安装运行环境

1.1 下载 JDK(推荐 8/11 版本,兼容 Selenium 4.x):

2. 访问启动的地址:http://192.168.1.14:4444/ui/----能访问成功则说明启动成功

3. 连接Grid-单个用例

python 复制代码
import time
from selenium.webdriver import Remote, ChromeOptions

driver = Remote(
    command_executor="http://127.0.0.1:4444",
    options=ChromeOptions()
)
driver.get("https://www.baidu.com")

time.sleep(3)
driver.quit() # 在grid浏览器不会随着Python自动结束,务必要手动关闭!不然影响资源占用

4. 分布式执行

  • 执行命令: pytest -n 4(执行并行数) / pytest test_grid.py -n auto (自动根据 CPU 核心数并行)
  • 注意需要安装pytest-xdist插件: pip install pytest-xdist
python 复制代码
import pytest
import time
from selenium.webdriver import Remote, ChromeOptions


@pytest.fixture()
def driver():
    d = Remote(
        command_executor="http://127.0.0.1:4444",
        options=ChromeOptions()
    )
    yield d
    d.quit()  # 在grid 浏览器不会随着Python自动结束,务必要手动关闭!


@pytest.mark.parametrize(
    "url",
    [
        "https://www.baidu.com",
        "https://www.bilibili.com/",
        "https://www.csdn.net/",
    ]
)
def test_ces(driver, url):
    time.sleep(2)
    driver.get(url)

执行结果:三个用例同时执行

相关推荐
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时2 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
AI职业加油站2 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
奇思妙想聪明勤奋的小羊2 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1232 天前
2026年第38周GitHub趋势周报
python·科技·github