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

Azure DevOps中webdriver-manager --detach无法脱离导致流水线阻塞

解决Azure DevOps Ubuntu代理上Selenium测试npm start挂起问题

问题根源

Azure DevOps的任务会阻塞等待当前步骤启动的所有前台进程结束才会进入下一个任务。虽然webdriver-manager start --detach在本地能将进程挂到后台,但在CI环境的shell中,该参数并未正确脱离前台上下文,导致npm start任务一直卡住等待进程退出。

可行解决方案

1. 修改npm start脚本,强制后台运行

直接在package.json的start脚本中使用shell后台运行语法,替代--detach参数:

{
  "scripts": {
    "start": "nohup webdriver-manager start > webdriver.log 2>&1 &"
  }
}
  • nohup:让进程脱离当前shell会话,避免任务结束时被终止
  • > webdriver.log 2>&1:将输出重定向到日志文件,避免占用前台输出流
  • &:将进程放到后台运行

2. 在流水线中直接执行命令并添加启动等待逻辑

跳过npm start,在azure-pipelines.yml的脚本步骤中直接启动webdriver,并加入轮询等待服务就绪的逻辑,确保测试启动前服务已运行:

stages:
- stage: Test
  jobs:
  - job: RunTests
    pool:
      vmImage: 'ubuntu-20.04'
    steps:
    - script: |
        npm install
        npm run update_webdriver
        # 启动webdriver到后台
        nohup webdriver-manager start > webdriver.log 2>&1 &
        # 轮询等待4444端口(webdriver默认端口)就绪
        until curl -s http://localhost:4444/wd/hub/status | grep -q "success"; do
          echo "等待WebDriver服务启动..."
          sleep 2
        done
        npm run test
      displayName: '安装依赖、启动WebDriver并执行测试'

3. 替换为selenium-standalone包(更稳定的CI方案)

webdriver-manager在CI环境的后台运行兼容性较差,推荐使用selenium-standalone包:

  1. 安装依赖:
npm install selenium-standalone --save-dev
  1. 修改package.json脚本:
{
  "scripts": {
    "selenium-install": "npx selenium-standalone install",
    "selenium-start": "npx selenium-standalone start &",
    "test": "jest"
  }
}
  1. 更新azure-pipelines.yml步骤:
- script: |
    npm install
    npm run selenium-install
    npm run selenium-start
    until curl -s http://localhost:4444/wd/hub/status | grep -q "success"; do
      echo "等待Selenium服务启动..."
      sleep 2
    done
    npm run test

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 17:18:25