跳到主要内容

持续集成

介绍

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

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

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

  2. 安装 Playwright:

    pip install playwright
    playwright install --with-deps
  3. 运行测试:

    pytest

CI 配置

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

GitHub Actions

在 push/pull_request 时

测试将在 main/master 分支上的 push 或 pull request 时运行。工作流将安装所有依赖项,安装 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: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Ensure browsers are installed
run: python -m playwright install --with-deps
- name: Run your tests
run: pytest --tracing=retain-on-failure
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-traces
path: test-results/

通过容器

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/python:v1.51.0-noble
options: --user 1001
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r local-requirements.txt
pip install -e .
- name: Run your tests
run: pytest

在部署时

这将在 GitHub Deployment 进入 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
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Ensure browsers are installed
run: python -m playwright install --with-deps
- name: Run tests
run: pytest
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 代理,您可以使用支持运行容器化作业的 Azure Pipelines 的我们的 Docker 容器。另外,您可以使用命令行工具安装所有必要的依赖项。

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

trigger:
- main

pool:
vmImage: ubuntu-latest

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.11'
displayName: 'Use Python'
- script: |
python -m pip install --upgrade pip
pip install -r requirements.txt
displayName: 'Install dependencies'
- script: playwright install --with-deps
displayName: 'Install Playwright browsers'
- script: pytest
displayName: 'Run Playwright tests'

Azure Pipelines (容器化)

trigger:
- main

pool:
vmImage: ubuntu-latest
container: mcr.microsoft.com/playwright/python:v1.51.0-noble

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.11'
displayName: 'Use Python'

- script: |
python -m pip install --upgrade pip
pip install -r requirements.txt
displayName: 'Install dependencies'
- script: pytest
displayName: 'Run tests'

CircleCI

在 CircleCI 上运行 Playwright 与在 GitHub Actions 上运行非常相似。为了指定预构建的 Playwright Docker 镜像,只需像这样修改配置中的 docker: 代理定义即可

executors:
pw-noble-development:
docker:
- image: mcr.microsoft.com/playwright/python:v1.51.0-noble

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

Jenkins

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

pipeline {
agent { docker { image 'mcr.microsoft.com/playwright/python:v1.51.0-noble' } }
stages {
stage('e2e-tests') {
steps {
sh 'pip install -r requirements.txt'
sh 'pytest'
}
}
}
}

Bitbucket Pipelines

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

image: mcr.microsoft.com/playwright/python:v1.51.0-noble

GitLab CI

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

stages:
- test

tests:
stage: test
image: mcr.microsoft.com/playwright/python:v1.51.0-noble
script:
...

缓存浏览器

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

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

调试浏览器启动

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

DEBUG=pw:browser pytest

运行有头模式

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

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

xvfb-run pytest