设置 CI
介绍
¥Introduction
Playwright 测试可以在任何 CI 提供商上运行。本节将介绍如何使用 GitHub Actions 在 GitHub 上运行测试。如果你想了解如何配置其他 CI 提供商,请查看我们关于持续集成的详细文档。
¥Playwright tests can be run on any CI provider. In this section we cover running tests on GitHub using GitHub Actions. If you would like to see how to configure other CI providers, check out our detailed doc on Continuous Integration.
你将学习
¥You will learn
设置 GitHub Actions
¥Setting up GitHub Actions
要添加 GitHub Actions 文件,首先创建 .github/workflows
文件夹,并在其中添加包含以下示例代码的 playwright.yml
文件,以便你的测试在主/master 分支的每个推送和拉取请求上运行。
¥To add a GitHub Actions file, first create .github/workflows
folder and inside it add a playwright.yml
file containing the example code below so that your tests run on each push and pull request for the main/master branch.
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"。
¥To learn more about this, see "Understanding GitHub Actions".
查看 jobs.test.steps
中的步骤列表,你可以看到工作流执行以下步骤:
¥Looking at the list of steps in jobs.test.steps
, you can see that the workflow performs these steps:
-
克隆你的代码库
¥Clone your repository
-
安装语言依赖
¥Install language dependencies
-
安装项目依赖并构建
¥Install project dependencies and build
-
安装 Playwright 浏览器
¥Install Playwright Browsers
-
运行测试
¥Run tests
创建 Repo 并推送到 GitHub
¥Create a Repo and Push to GitHub
设置好 GitHub Actions 工作流程 后,你只需运行 在 GitHub 上创建存储库 或将代码推送到现有代码库即可。请按照 GitHub 上的说明操作,并不要忘记使用 git init
命令执行 初始化 git 存储库,这样你就可以对代码执行 add、commit 和 push 操作。
¥Once you have your GitHub Actions workflow setup, then all you need to do is Create a repo on GitHub or push your code to an existing repository. Follow the instructions on GitHub and don't forget to initialize a git repository using the git init
command so you can add, commit, and push your code.

打开工作流程
¥Opening the Workflows
单击“操作”选项卡可查看工作流程。在这里你可以查看你的测试是通过还是失败。
¥Click on the Actions tab to see the workflows. Here you see if your tests have passed or failed.
查看测试日志
¥Viewing Test Logs
点击“工作流运行”会显示 GitHub 执行的所有操作;点击“运行 Playwright 测试”会显示错误消息、预期结果、收到的结果以及调用日志。
¥Clicking on the workflow run shows you all the actions that GitHub performed and clicking on Run Playwright tests shows the error messages, what was expected and what was received as well as the call log.
查看踪迹
¥Viewing the Trace
trace.playwright.dev 是跟踪查看器的静态托管变体。你可以使用拖放操作上传跟踪文件。
¥trace.playwright.dev is a statically hosted variant of the Trace Viewer. You can upload trace files using drag and drop.
正确处理密钥
¥Properly handling Secrets
跟踪文件或控制台日志等工件包含有关测试执行的信息。它们可能包含敏感数据,例如测试用户的用户凭据、访问暂存后端的令牌、测试源代码,有时甚至是应用源代码。请像处理敏感数据一样谨慎处理这些文件。如果你在 CI 工作流程中上传报告和跟踪,请确保仅将它们上传到受信任的工件存储,或者在上传前对文件进行加密。与团队成员共享工件也是如此:使用受信任的文件共享或在共享前加密文件。
¥Artifacts like trace files or console logs contain information about your test execution. They can contain sensitive data like user credentials for a test user, access tokens to a staging backend, testing source code, or sometimes even your application source code. Treat these files just as carefully as you treat that sensitive data. If you upload reports and traces as part of your CI workflow, make sure that you only upload them to trusted artifact stores, or that you encrypt the files before upload. The same is true for sharing artifacts with team members: Use a trusted file share or encrypt the files before sharing.
下一步是什么
¥What's Next