跳转到主要内容

持续集成

简介

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

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

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

  2. 安装 Playwright:

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

    pytest

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: 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.54.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 部署进入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 代理,您可以将我们的 Docker 容器与支持运行容器化作业的 Azure Pipelines 结合使用。或者,您可以使用命令行工具安装所有必要的依赖项。

要运行 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.54.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.54.0-noble

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

Jenkins

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

pipeline {
agent { docker { image 'mcr.microsoft.com/playwright/python:v1.54.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.54.0-noble

GitLab CI

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

stages:
- test

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

缓存浏览器

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

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

调试浏览器启动

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