持续集成
介绍
¥Introduction
Playwright 测试可以在 CI 环境中执行。我们为常见 CI 提供商创建了示例配置。
¥Playwright tests can be executed in CI environments. We have created sample configurations for common CI providers.
在 CI 上运行测试的 3 个步骤:
¥3 steps to get your tests running on CI:
-
确保 CI 代理可以运行浏览器:在 Linux 代理中使用 我们的 Docker 镜像 或使用 CLI 安装依赖。
¥Ensure CI agent can run browsers: Use our Docker image in Linux agents or install your dependencies using the CLI.
-
安装 Playwright:
¥Install Playwright:
pip install playwright
playwright install --with-deps -
运行你的测试:
¥Run your tests:
pytest
CI 配置
¥CI configurations
命令行工具 可用于在 CI 中安装所有操作系统依赖。
¥The Command line tools can be used to install all operating system dependencies in CI.
GitHub Actions
在推/拉请求时
¥On push/pull_request
测试将在主/主分支上的推送或拉取请求上运行。workflow 将安装所有依赖,安装 Playwright,然后运行测试。
¥Tests will run on push or pull request on branches main/master. The workflow will install all dependencies, install Playwright and then run the tests.
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/
通过容器
¥Via Containers
GitHub Actions 通过使用 jobs.<job_id>.container
选项支持 在容器中运行作业。这对于不通过依赖污染主机环境以及拥有一致的环境(例如)很有用。 跨不同操作系统的屏幕截图/视觉回归测试。
¥GitHub Actions support running jobs in a container by using the jobs.<job_id>.container
option. This is useful to not pollute the host environment with dependencies and to have a consistent environment for e.g. screenshots/visual regression testing across different operating systems.
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
部署中
¥On deployment
这将在 GitHub 部署 进入 success
状态后开始测试。Vercel 等服务使用此模式,因此你可以在其部署的环境中运行端到端测试。
¥This will start the tests after a GitHub Deployment went into the success
state. Services like Vercel use this pattern so you can run your end-to-end tests on their deployed environment.
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 配置 以确保最佳性能。
¥We have a pre-built Docker image which can either be used directly or as a reference to update your existing Docker definitions. Make sure to follow the Recommended Docker Configuration to ensure the best performance.
Azure 管道
¥Azure Pipelines
对于 Windows 或 macOS 代理,无需额外配置,只需安装 Playwright 并运行测试即可。
¥For Windows or macOS agents, no additional configuration is required, just install Playwright and run your tests.
对于 Linux 代理,可以使用 我们的 Docker 容器 和 Azure Pipelines 支持 运行容器化作业。或者,你可以使用 命令行工具 安装所有必需的依赖。
¥For Linux agents, you can use our Docker container with Azure Pipelines support running containerized jobs. Alternatively, you can use Command line tools to install all necessary dependencies.
要运行 Playwright 测试,请使用此管道任务:
¥For running the Playwright tests use this pipeline task:
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(容器化)
¥Azure Pipelines (containerized)
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:
修改代理定义,如下所示:
¥Running Playwright on CircleCI is very similar to running on GitHub Actions. In order to specify the pre-built Playwright Docker image, simply modify the agent definition with docker:
in your config like so:
executors:
pw-noble-development:
docker:
- image: mcr.microsoft.com/playwright/python:v1.51.0-noble
注意:使用 docker 代理定义时,你将 playwright 运行的资源类指定为 'medium' 层 此处。Playwright 的默认行为是将工作线程数量设置为检测到的核心数量(在中等层的情况下为 2)。将工作线程数量覆盖到大于此数量将导致不必要的超时和失败。
¥Note: When using the docker agent definition, you are specifying the resource class of where playwright runs to the 'medium' tier here. The default behavior of Playwright is to set the number of workers to the detected core count (2 in the case of the medium tier). Overriding the number of workers to greater than this number will cause unnecessary timeouts and failures.
Jenkins
Jenkins 支持管道的 Docker 代理。使用 Playwright Docker 镜像 在 Jenkins 上运行测试。
¥Jenkins supports Docker agents for pipelines. Use the Playwright Docker image to run tests on 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 管道
¥Bitbucket Pipelines
Bitbucket Pipelines 可以使用公共 Docker 镜像作为构建环境。要在 Bitbucket 上运行 Playwright 测试,请使用我们的公共 Docker 映像 (参见 Dockerfile)。
¥Bitbucket Pipelines can use public Docker images as build environments. To run Playwright tests on Bitbucket, use our public Docker image (see Dockerfile).
image: mcr.microsoft.com/playwright/python:v1.51.0-noble
GitLab CI
要在 GitLab 上运行 Playwright 测试,请使用我们的公共 Docker 映像 (参见 Dockerfile)。
¥To run Playwright tests on GitLab, use our public Docker image (see Dockerfile).
stages:
- test
tests:
stage: test
image: mcr.microsoft.com/playwright/python:v1.51.0-noble
script:
...
缓存浏览器
¥Caching browsers
不建议缓存浏览器二进制文件,因为恢复缓存所需的时间与下载二进制文件所需的时间相当。特别是在 Linux 下,需要安装 操作系统依赖,它是不可缓存的。
¥Caching browser binaries is not recommended, since the amount of time it takes to restore the cache is comparable to the time it takes to download the binaries. Especially under Linux, operating system dependencies need to be installed, which are not cacheable.
如果你仍想在 CI 运行之间缓存浏览器二进制文件,请根据 Playwright 版本的哈希在 CI 配置中缓存 这些目录。
¥If you still want to cache the browser binaries between CI runs, cache these directories in your CI configuration, against a hash of the Playwright version.
调试浏览器启动
¥Debugging browser launches
Playwright 支持 DEBUG
环境变量在执行过程中输出调试日志。将其设置为 pw:browser
在调试 Error: Failed to launch browser
错误时很有帮助。
¥Playwright supports the DEBUG
environment variable to output debug logs during execution. Setting it to pw:browser
is helpful while debugging Error: Failed to launch browser
errors.
DEBUG=pw:browser pytest
运行有头
¥Running headed
默认情况下,Playwright 以无头模式启动浏览器。请参阅我们的 运行测试 指南,了解如何在有头模式下运行测试。
¥By default, Playwright launches browsers in headless mode. See in our Running tests guide how to run tests in headed mode.
在 Linux 代理上,引导执行需要安装 Xvfb。我们的 Docker 镜像 和 GitHub Action 预装了 Xvfb。要使用 Xvfb 在 head 模式下运行浏览器,请在实际命令之前添加 xvfb-run
。
¥On Linux agents, headed execution requires Xvfb to be installed. Our Docker image and GitHub Action have Xvfb pre-installed. To run browsers in headed mode with Xvfb, add xvfb-run
before the actual command.
xvfb-run pytest