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

如何在GitLab CI中拆分Playwright自动化测试为两个独立阶段?

拆分GitLab CI中Playwright测试任务的实现方案

不需要新增stage块,核心是把原来的单个tests任务拆成两个独立的job即可——你可以把它们放在同一个stage下(默认并行执行),也可以指定不同stage(按需串行执行)。

以下是修改后的完整配置示例,包含独立环境变量和任务拆分:

stages:
  - automation_tests

# 定义复用模板,减少重复配置
.template-playwright:
  stage: automation_tests
  image:
    name: mcr.microsoft.com/playwright:v1.33.0-focal
    entrypoint: ["/bin/bash", "-c", "ln -snf /bin/bash /bin/sh && /bin/bash -c $0"]
  script:
    - npm ci
    - npx playwright install chromium
  artifacts:
    when: always
    expire_in: 5 days

# QA环境测试任务
tests-qa:
  extends: .template-playwright
  variables:
    # QA环境专属变量
    QA_HIDDEN_VARIABLE: "qa-environment-value"
  script:
    - !reference [.template-playwright, script]
    - npx playwright test --project=qa
  artifacts:
    paths:
      - ./playwright-report-qa/ # 单独路径避免报告覆盖
    when: always
    expire_in: 5 days

# Stage环境测试任务
tests-stage:
  extends: .template-playwright
  variables:
    # Stage环境专属变量
    STAGE_HIDDEN_VARIABLE: "stage-environment-value"
  script:
    - !reference [.template-playwright, script]
    - npx playwright test --project=stage
  artifacts:
    paths:
      - ./playwright-report-stage/ # 单独路径避免报告覆盖
    when: always
    expire_in: 5 days

关键改动说明

  • 拆分独立job:将原tests任务拆分为tests-qa和tests-stage,各自对应Playwright的qa和stage项目
  • 独立环境变量:通过每个job的variables块配置专属环境变量,也可以直接在script命令中临时设置(比如QA_VAR="xxx" npx playwright test...)
  • 复用配置:用.template-playwright模板抽离重复的镜像、依赖安装等步骤,减少冗余代码
  • 避免报告冲突:修改artifacts的路径,确保两个任务的测试报告不会互相覆盖
  • 执行方式灵活:两个job放在同一个stage下会默认并行执行;如果需要串行,只需新增不同stage并调整stages顺序即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 14:18:16