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)

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

相关推荐
unfeeling_几秒前
Nginx实验
运维·nginx
xiaoginshuo2 分钟前
流程自动化从传统RPA升级到AI Agent,如何匹配合适的自动化方案
人工智能·自动化·rpa
悠闲蜗牛�11 分钟前
边缘AI推理实战:从服务器到嵌入式设备的模型部署与优化
运维·服务器·人工智能
小鸡吃米…12 分钟前
TensorFlow - 构建计算图
人工智能·python·tensorflow
cipher34 分钟前
crawl4ai:AI时代的数据采集利器——从入门到实战
后端·爬虫·python
shawnyz1 小时前
Nginx的源码编译
运维·nginx
南 阳1 小时前
Python从入门到精通day37
数据库·python·oracle
开发者小天1 小时前
python安装 Matplotlib 库 安装 Seaborn 库
开发语言·python·matplotlib
with-the-flow1 小时前
从数学底层的底层原理来讲 random 的函数是怎么实现的
c语言·python·算法
The️1 小时前
Linux驱动开发之Read_Write函数
linux·运维·服务器·驱动开发·ubuntu·交互