发布于 2026-01-06 0 阅读
0

将 Playwright 与 CI/CD 流水线集成

将 Playwright 与 CI/CD 流水线集成

目录

介绍

持续集成和持续部署 (CI/CD) 流水线对于高效交付高质量软件至关重要。将 Playwright 与 CI/CD 集成,可以实现端到端测试的自动化,确保每次代码变更在部署到生产环境之前都经过测试。本指南将引导您完成 Playwright 与 GitHub Actions、GitLab CI 和 Jenkins 等常用 CI/CD 工具的集成过程。

为什么要将 Playwright 与 CI/CD 集成?

  • 自动化测试:确保代码更改不会破坏功能。
  • 一致性:在受控且可重复的环境中进行测试。
  • 可扩展性:同时在多个浏览器和设备上执行测试。
  • 反馈循环:立即获得测试结果反馈,以便更快地进行调试。

第一步:准备你的剧作家测试

在将 Playwright 集成到 CI/CD 流水线之前,请确保您的测试符合以下要求:

  1. 功能性验证:在本地运行npx playwright test以验证其是否有效。
  2. 可配置:使用环境变量设置基本 URL、凭据和其他配置。
  3. 可靠:利用 Playwright 的自动等待功能,最大限度地减少不稳定的测试。

步骤 2:将 Playwright 与 GitHub Actions 集成

GitHub Actions 提供了一种简单的方法来设置 CI/CD 流水线。

创建工作流文件

保存以下内容.github/workflows/playwright.yml

name: Playwright Tests

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Set up Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '16'

    - name: Install dependencies
      run: npm ci

    - name: Install Playwright Browsers
      run: npx playwright install --with-deps

    - name: Run Playwright tests
      run: npx playwright test

    - name: Upload Playwright Test Report
      if: failure()
      uses: actions/upload-artifact@v3
      with:
        name: playwright-report
        path: playwright-report
Enter fullscreen mode Exit fullscreen mode

主要特点

  • 在推送或拉取请求事件发生时自动触发。
  • 上传剧作家HTML报告,用于记录运行失败的情况。

步骤 3:将 Playwright 与 GitLab CI/CD 集成

GitLab CI/CD 流水线是使用该.gitlab-ci.yml文件进行配置的。

创建管道配置

将以下内容添加到您的.gitlab-ci.yml

stages:
  - test

test_playwright:
  stage: test
  image: mcr.microsoft.com/playwright:v1.38.0-focal
  script:
    - npm ci
    - npx playwright install --with-deps
    - npx playwright test
  artifacts:
    when: always
    paths:
      - playwright-report/
    expire_in: 1 week
Enter fullscreen mode Exit fullscreen mode

主要特点

  • 为了保持一致性,使用官方的 Playwright Docker 镜像。
  • 保存测试工件,包括 Playwright 报告,以便于调试。

第四步:将剧作家与詹金斯整合

Jenkins 需要Jenkinsfile定义您的流水线。

创建 Jenkinsfile

pipeline {
    agent any

    stages {
        stage('Install Dependencies') {
            steps {
                sh 'npm ci'
                sh 'npx playwright install --with-deps'
            }
        }

        stage('Run Tests') {
            steps {
                sh 'npx playwright test'
            }
        }
    }

    post {
        always {
            archiveArtifacts artifacts: 'playwright-report/**', allowEmptyArchive: true
        }
        failure {
            echo 'Tests failed! Check the report.'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

主要特点

  • 存档剧作家报告的所有演出记录。
  • 为失败和成功提供自定义运行后操作。

步骤五:并行运行测试

为了加快测试执行速度,请将 Playwright 配置为并行运行测试。

配置workers添加playwright.config.ts

import { defineConfig } from '@playwright/test';

export default defineConfig({
  workers: process.env.CI ? 4 : 1,
});
Enter fullscreen mode Exit fullscreen mode

在 CI 流水线中,确保分配足够的资源来支持并行处理。

步骤六:处理工件和报告

为了有效分析测试结果:

  1. 生成 HTML 报告:确保playwright.config.ts已启用 HTML 报告功能:
reporter: [['html', { outputFolder: 'playwright-report' }]]
Enter fullscreen mode Exit fullscreen mode
  1. 上传报告:使用 CI/CD 特定命令将报告作为工件上传(例如,actions/upload-artifact上传到 GitHub 或artifactsGitLab)。
  2. 可视化结果:将报告托管在 S3 存储桶或静态服务器上,以便轻松访问。

Playwright CI/CD 集成最佳实践

  • 使用 Docker 镜像:利用官方 Playwright Docker 镜像来获得一致的环境。
  • 跨浏览器测试:确保测试配置为在多个浏览器(Chromium、WebKit、Firefox)上运行。
  • 对每次提交运行冒烟测试:执行一小部分关键测试以获得更快的反馈。
  • 夜间构建的完整测试套件:在非高峰时段运行完整的测试套件。
  • 监控不稳定测试:跟踪并解决不稳定测试,以保持管道稳定性。

结论

将 Playwright 与 CI/CD 流水线集成,可确保可靠的自动化测试,从而增强您的开发工作流程。按照本指南,您可以使用 GitHub Actions、GitLab CI 和 Jenkins 等工具搭建强大的流水线。立即开始将 Playwright 集成到您的 CI/CD 流程中,自信地交付高质量软件!

有任何问题或建议想分享吗?请在下方评论区留言!

文章来源:https://dev.to/aswani25/integrating-playwright-with-cicd-pipelines-1g1m