跳转到主要内容

持续集成

简介

Playwright 测试可以在 CI 环境中执行。我们为常见的 CI 提供商创建了示例配置。

在 CI 中运行测试的 3 个步骤

  1. 确保 CI 代理可以运行浏览器:在 Linux 代理中使用我们的 Docker 镜像,或者使用CLI安装您的依赖项。

  2. 安装 Playwright:

    dotnet build
    pwsh bin/Debug/netX/playwright.ps1 install --with-deps
  3. 运行您的测试:

    dotnet test

CI 配置

命令行工具可用于在 CI 中安装所有操作系统依赖项。

GitHub Actions

在 push/pull_request 上

测试将在主分支/master 分支上的推送或拉取请求时运行。工作流将安装所有依赖项,安装 Playwright,然后运行测试。

.github/workflows/playwright.yml
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Build & Install
run: dotnet build
- name: Ensure browsers are installed
run: pwsh bin/Debug/net8.0/playwright.ps1 install --with-deps
- name: Run your tests
run: dotnet test

通过容器

GitHub Actions 支持通过使用jobs.<job_id>.container选项在容器中运行作业。这对于避免使用依赖项污染主机环境以及为例如跨不同操作系统的屏幕截图/视觉回归测试提供一致的环境非常有用。

.github/workflows/playwright.yml
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
playwright:
name: 'Playwright Tests'
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright/dotnet:v1.54.0-noble
options: --user 1001
steps:
- uses: actions/checkout@v4
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- run: dotnet build
- name: Run your tests
run: dotnet test

在部署上

这将在GitHub 部署进入success状态后启动测试。Vercel 等服务使用此模式,因此您可以在其部署环境中运行端到端测试。

.github/workflows/playwright.yml
name: Playwright Tests
on:
deployment_status:
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
if: github.event.deployment_status.state == 'success'
steps:
- uses: actions/checkout@v4
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- run: dotnet build
- name: Ensure browsers are installed
run: pwsh bin/Debug/net8.0/playwright.ps1 install --with-deps
- name: Run tests
run: dotnet test
env:
# This might depend on your test-runner
PLAYWRIGHT_TEST_BASE_URL: ${{ github.event.deployment_status.target_url }}

Docker

我们有一个预构建的 Docker 镜像,可以直接使用或作为更新现有 Docker 定义的参考。请务必遵循推荐的 Docker 配置以确保最佳性能。

Azure Pipelines

对于 Windows 或 macOS 代理,无需额外配置,只需安装 Playwright 并运行测试即可。

对于 Linux 代理,您可以使用我们的 Docker 容器,Azure Pipelines 支持运行容器化作业。或者,您可以使用命令行工具安装所有必要的依赖项。

要运行 Playwright 测试,请使用此管道任务

trigger:
- main

pool:
vmImage: ubuntu-latest

steps:
- task: UseDotNet@2
inputs:
packageType: sdk
version: '8.0.x'
displayName: 'Use .NET SDK'
- script: dotnet build --configuration Release
displayName: 'Build'
- script: pwsh bin/Release/net8.0/playwright.ps1 install --with-deps
displayName: 'Install Playwright browsers'
- script: dotnet test --configuration Release
displayName: 'Run tests'

Azure Pipelines(容器化)

trigger:
- main

pool:
vmImage: ubuntu-latest
container: mcr.microsoft.com/playwright/dotnet:v1.54.0-noble

steps:
- task: UseDotNet@2
inputs:
packageType: sdk
version: '8.0.x'
displayName: 'Use .NET SDK'

- script: dotnet build --configuration Release
displayName: 'Build'
- script: dotnet test --configuration Release
displayName: 'Run tests'

CircleCI

在 CircleCI 上运行 Playwright 与在 GitHub Actions 上运行非常相似。要指定预构建的 Playwright Docker 镜像,只需在配置中通过docker:修改代理定义,如下所示:

executors:
pw-noble-development:
docker:
- image: mcr.microsoft.com/playwright/dotnet:v1.54.0-noble

注意:当使用 docker 代理定义时,您将 Playwright 运行的资源类指定为“medium”层此处。Playwright 的默认行为是将 worker 数量设置为检测到的核心数(在 medium 层中为 2)。将 worker 数量覆盖到大于此数字将导致不必要的超时和失败。

Jenkins

Jenkins 支持用于管道的 Docker 代理。使用Playwright Docker 镜像在 Jenkins 上运行测试。

pipeline {
agent { docker { image 'mcr.microsoft.com/playwright/dotnet:v1.54.0-noble' } }
stages {
stage('e2e-tests') {
steps {
sh 'dotnet build'
sh 'dotnet test'
}
}
}
}

Bitbucket Pipelines

Bitbucket Pipelines 可以使用公共Docker 镜像作为构建环境。要在 Bitbucket 上运行 Playwright 测试,请使用我们的公共 Docker 镜像(参见 Dockerfile)。

image: mcr.microsoft.com/playwright/dotnet:v1.54.0-noble

GitLab CI

要在 GitLab 上运行 Playwright 测试,请使用我们的公共 Docker 镜像(参见 Dockerfile)。

缓存浏览器

不建议缓存浏览器二进制文件,因为恢复缓存所需的时间与下载二进制文件所需的时间相当。尤其是在 Linux 下,需要安装操作系统依赖项,这些依赖项是不可缓存的。

如果您仍然想在 CI 运行之间缓存浏览器二进制文件,请根据 Playwright 版本的哈希,在您的 CI 配置中缓存这些目录

调试浏览器启动

Playwright 支持 DEBUG 环境变量以在执行期间输出调试日志。将其设置为 pw:browser 在调试 Error: Failed to launch browser 错误时很有帮助。

DEBUG=pw:browser dotnet test

有头模式运行

默认情况下,Playwright 在无头模式下启动浏览器。请参阅我们的运行测试指南,了解如何在有头模式下运行测试。

在 Linux 代理上,有头执行需要安装Xvfb。我们的Docker 镜像和 GitHub Action 都预装了 Xvfb。要在有头模式下使用 Xvfb 运行浏览器,请在实际命令之前添加xvfb-run

xvfb-run dotnet test