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

GitLab CI Ubuntu容器中Testcafe报ECONNREFUSED错误求助

问题分析

报错ECONNREFUSED说明Testcafe无法连接到浏览器实例,结合你的配置,核心问题集中在浏览器参数传递错误、手动启动浏览器导致冲突、服务未就绪就执行测试这三点。

解决方案

1. 修正package.json的Testcafe脚本

你当前test-ci脚本中的单引号会被npm解析异常,导致Testcafe无法正确识别浏览器启动参数。修改为:

{
  "scripts": {
    "test": "testcafe chrome",
    "test-ci": "testcafe chromium --headless --no-sandbox"
  },
  "dependencies": {
    "testcafe": "^2.2.0"
  }
}

也可以使用Testcafe的简写格式:

"test-ci": "testcafe chromium:headless:no-sandbox"

2. 移除手动启动Chromium的命令

Testcafe会自动管理浏览器实例,手动启动的Chromium会占用资源甚至引发端口冲突,直接删除这一行:

- chromium --headless --no-sandbox --remote-debugging-port=34135 http://localhost:3000 &

3. 添加服务就绪等待逻辑

后端PHP内置服务器和前端npm dev服务启动后需要时间初始化,直接执行测试会因服务未就绪失败。在启动服务后添加轮询等待:

script:
  # ... 其他步骤 ...
  - php -d variables_order=EGPCS -S $BACKEND_SERVICE_URL public/index.php &
  # 等待后端服务就绪
  - while ! curl -s $BACKEND_SERVICE_URL > /dev/null; do sleep 1; echo "Waiting for backend..."; done
  # ... 前端安装步骤 ...
  - npm run dev &
  # 等待前端服务就绪
  - while ! curl -s http://localhost:3000 > /dev/null; do sleep 1; echo "Waiting for frontend..."; done
  # ... 其他步骤 ...

如果镜像没有curl,可用wget替代:

- while ! wget --spider $BACKEND_SERVICE_URL > /dev/null 2>&1; do sleep 1; done

4. 优化CI配置(可选)

  • 移除before_script里的x11-apps安装,无头浏览器不需要X11环境:
    before_script:
      # - apt update
      # - apt install -y x11-apps
    
  • 关闭Testcafe配置里的qrCode,CI环境下无需该功能:
    {
      "disableScreenshots": false,
      "screenshots": {
        "path": "./screenshots/",
        "takeOnFails": true
      },
      "qrCode": false,
      "color": true
    }
    

5. 验证Chromium路径(排查用)

若仍报错,在CI脚本中添加which chromium确认Testcafe能找到浏览器:

- which chromium

如果输出有效路径,说明浏览器可访问;若没有,需用绝对路径指定给Testcafe:

"test-ci": "testcafe /usr/bin/chromium --headless --no-sandbox"
最终CI脚本示例
variables:
  DOCKER_DRIVER: overlay2
  JOB_IMAGE: git.var-lab.com:5050/dev-tools/glubuntu-php:latest-php-8.1
  GIT_STRATEGY: clone

e2e_tests:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: ($CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH)
  image: $JOB_IMAGE
  variables:
    BACKEND_SERVICE_URL: http://localhost
    PUBLIC_BACKEND_SERVICE_URL: http://localhost
  script:
    - mysql -h database -u root -proot <<< "SET GLOBAL sql_mode = '';"
    - mysql -h database -u root -proot symfony < data/sql_dump.sql
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@git.xxx.de/xxx/xxx-software.git
    - cd xxx-software
    - composer install
    - php -d variables_order=EGPCS -S $BACKEND_SERVICE_URL public/index.php &
    - while ! curl -s $BACKEND_SERVICE_URL > /dev/null; do sleep 1; echo "Waiting for backend..."; done
    - cd ../
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@git.xxx.de/xxx/xxx-frontend.git
    - cd xxx-frontend
    - npm install
    - npm run dev &
    - while ! curl -s http://localhost:3000 > /dev/null; do sleep 1; echo "Waiting for frontend..."; done
    - cd ../
    - npm install
    - ps aux
    - npm run test-ci -- -b
    - npm run test-ci -- --debug-on-fail

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 03:15:46