Skip to main content

设置 CI

介绍

¥Introduction

Playwright 测试可以在任何 CI 提供商上运行。在本节中,我们将介绍如何使用 GitHub Actions 在 GitHub 上运行测试。如果你想了解如何配置其他 CI 提供商,请查看我们关于持续集成的详细文档。

¥Playwright tests can be run on any CI provider. In this section we will 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 will run on each push and pull request for the main/master branch.

.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
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
- name: Build & Install
run: mvn -B install -D skipTests --no-transfer-progress
- name: Ensure browsers are installed
run: mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --with-deps"
- name: Run tests
run: mvn test

要了解更多信息,请参阅 "了解 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:

  1. 克隆你的代码库

    ¥Clone your repository

  2. 安装语言依赖

    ¥Install language dependencies

  3. 安装项目依赖并构建

    ¥Install project dependencies and build

  4. 安装 Playwright 浏览器

    ¥Install Playwright Browsers

  5. 运行测试

    ¥Run tests

创建 Repo 并推送到 GitHub

¥Create a Repo and Push to GitHub

一旦你完成了 GitHub actions 工作流程 设置,那么你需要做的就是 在 GitHub 上创建存储库 或将你的代码推送到现有的存储库。按照 GitHub 上的说明进行操作,不要忘记使用 git init 命令进行 初始化 git 存储库,这样你就可以对代码进行 addcommitpush

¥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.

Create a Repo and Push to GitHub

打开工作流程

¥Opening the Workflows

单击“操作”选项卡可查看工作流程。在这里你将看到你的测试是通过还是失败。

¥Click on the Actions tab to see the workflows. Here you will see if your tests have passed or failed.

opening the workflow

查看测试日志

¥Viewing Test Logs

单击工作流程运行将显示 GitHub 执行的所有操作,单击运行 Playwright 测试将显示错误消息、预期内容和收到的内容以及调用日志。

¥Clicking on the workflow run will show you the all the actions that GitHub performed and clicking on Run Playwright tests will show the error messages, what was expected and what was received as well as the call log.

Viewing Test Logs

查看踪迹

¥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.

playwright trace viewer

下一步是什么

¥What's Next