You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Jenkins借助Selenium Grid在不同远程服务器执行测试

实现Jenkins触发Selenium Grid跨机器测试方案

整体架构说明

Jenkins(Linux)、Selenium Hub(独立机器)、Windows测试节点(多台)三者完全独立,通过网络通信完成测试触发与执行。

步骤1:部署Selenium Hub

在独立的机器(支持Linux/Windows,只要能和Jenkins、Windows节点连通)上启动Hub:

  • 下载与测试用Selenium版本匹配的Selenium Server JAR包
  • 执行启动命令:
    java -jar selenium-server-<version>.jar hub
    
  • 验证启动:访问http://<hub-ip>:4444,能看到Grid控制台页面即为成功。

步骤2:配置Windows测试节点

在每台Windows远程桌面机器上配置节点并连接到Hub:

  • 下载与Hub同版本的Selenium Server JAR包,以及对应浏览器的驱动(ChromeDriver、GeckoDriver等,需和浏览器版本匹配)
  • 将驱动放入系统PATH路径,或在启动命令中指定驱动路径
  • 执行节点启动命令(以Chrome为例):
    java -jar selenium-server-<version>.jar node --hub http://<hub-ip>:4444/grid/register --browserName chrome --browserVersion <chrome-version> --maxInstances 5
    
  • 验证注册:在Hub控制台页面能看到该Windows节点的信息即为成功。

步骤3:在Jenkins中配置测试任务

因禁止安装Selenium Grid插件,直接通过构建步骤执行测试脚本:

  • 确保Jenkins所在Linux服务器安装了与Selenium、测试脚本兼容的Java环境
  • 将测试代码(如Java TestNG、Python pytest项目)上传至Jenkins工作目录,或通过Git拉取
  • 修改测试代码中的WebDriver配置,指定连接到Selenium Hub:
    Java示例:
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setBrowserName("chrome");
    WebDriver driver = new RemoteWebDriver(new URL("http://<hub-ip>:4444/wd/hub"), caps);
    
    Python示例:
    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    driver = webdriver.Remote(
        command_executor='http://<hub-ip>:4444/wd/hub',
        desired_capabilities=DesiredCapabilities.CHROME
    )
    
  • 在Jenkins任务中添加构建步骤,执行对应命令:
    # Java项目示例
    mvn test
    # Python项目示例
    pytest test_case.py -v
    
  • 保存任务后触发构建,Jenkins会将测试请求发送至Selenium Hub,由Hub分配至空闲Windows节点执行测试。

关键注意事项

  • 确保Jenkins服务器、Selenium Hub、Windows节点三者之间网络互通,尤其是Hub的4444端口、节点与Hub通信的端口需在防火墙中开放
  • Windows节点需保持远程桌面登录状态(否则浏览器无法正常启动),可设置系统自动登录或借助工具维持会话
  • 测试代码中指定的浏览器版本、驱动版本必须与Windows节点上的完全匹配,避免兼容性报错
  • 可通过Hub的控制台页面(http://<hub-ip>:4444)实时监控节点状态与测试执行进度

内容的提问来源于stack exchange,提问作者suvi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:02:56