跳至主要内容

持续集成

简介

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

在推送/拉取请求时

测试将在main/master分支的推送或拉取请求时运行。workflow将安装所有依赖项,安装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..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.50.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定义。

建议配置

  1. 在使用Chromium时,也建议使用--ipc=host。没有这个参数,Chromium可能会耗尽内存并崩溃。更多关于此选项的信息请参阅Docker文档
  2. 在启动Chromium时遇到其他奇怪错误?在本地开发时,尝试使用docker run --cap-add=SYS_ADMIN运行您的容器。
  3. 建议使用--init Docker标志或dumb-init来避免对PID=1进程的特殊处理。这是导致僵尸进程的常见原因。

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.50.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.50.0-noble

注意:当使用docker代理定义时,您将Playwright运行的资源类别指定为'medium'层级此处。Playwright的默认行为是将工作线程数设置为检测到的核心数(在medium层级情况下为2)。将工作线程数覆盖为超过此数值会导致不必要的超时和故障。

Jenkins

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

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

GitLab CI

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

stages:
- test

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

浏览器缓存

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

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

调试浏览器启动

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

DEBUG=pw:browser pytest

运行带界面的版本

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

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

xvfb-run pytest