Playwright

Playwright 为现代 Web 应用提供可靠的端到端测试。

¥Playwright enables reliable end-to-end testing for modern web apps.

我们建议为你想要在 monorepo 中运行的每个测试套件创建一个 Playwright 包。这可能意味着套件会根据你的需求按应用、域或其他方案拆分。如果你不确定,请先为每个应用创建一个 Playwright 包。

¥We recommend creating a Playwright package for each test suite that you'd like to run in your monorepo. This may mean suites broken up per-application, per-domain, or some other scheme, according to your needs. If you are unsure, start with creating a Playwright package per-application.

处理 Playwright 的环境变量

¥Handling Playwright's environment variables

Playwright 需要多个环境变量才能正常运行。为了确保这些变量在 严格模式 中的任务中可用,你需要根据你的作用域偏好,在端到端任务中使用 passThroughEnv 或全局使用 globalPassThroughEnv 将它们添加到传递变量中。

¥Playwright requires several environment variables to run correctly. To ensure that these variables are available within your tasks in Strict Mode, you'll want to add them to your pass through variables using passThroughEnv in your end-to-end task or globalPassThroughEnv globally, depending on your scoping preferences.

以下使用 passThroughEnv 的配置将允许以 PLAYWRIGHT_ 开头的环境变量进入 e2e 任务,并且不会影响哈希值。

¥The configuration below using passThroughEnv will allow environment variables that start with PLAYWRIGHT_ into the e2e task and will not affect hashing.

Turborepo logo
./turbo.json
{
  "tasks": {
    "e2e": {
      "passThroughEnv": ["PLAYWRIGHT_*"]
    }
  }
}

请注意,我们使用传递变量,因为我们不希望在这些 Playwright 内部变量发生变化的情况下丢失缓存。例如,PLAYWRIGHT_BROWSERS_PATH 用于定位 Playwright 使用的浏览器二进制文件,如果此位置发生变化,我们不希望丢失缓存。

¥Note that pass through variables are used since we don't want to miss cache in situations where these Playwright-internal variables change. For example,PLAYWRIGHT_BROWSERS_PATH is used to locate browser binaries used by Playwright, and we don't want to miss cache if this location changes.

设计任务图

¥Designing the task graph

我们希望确保端到端套件拥有正确的缓存行为。具体来说,我们希望在几个关键情况下确保缓存未命中:

¥We want to ensure the proper caching behavior for our end-to-end suites. Specifically, we want to make sure of cache misses in a few key situations:

  • 如果测试套件发生变化,缓存会丢失。

    ¥If test suites change, cache gets missed.

  • 如果套件测试的代码发生变化,缓存将丢失。

    ¥If code tested by the suites changes, cache gets missed.

由于测试代码更改时任务的哈希值也会更改,因此第一个要求自然会得到满足。但是,第二个要求意味着你需要确保端到端测试依赖于应用源代码的更改。

¥The first requirement will be met naturally since the hash for the task will change when test code is changed. However, the second requirement means you need to ensure end-to-end tests depend on changes in application source code.

这种关系可以用 turbo.json 和端到端套件的 package.json 来表达。

¥This relationship can be expressed in turbo.json and the end-to-end suite's package.json.

Turborepo logo
./turbo.json
{
  "tasks": {
    "build": {
      "dependsOn": ["^build"]
    },
    "e2e": {
      "dependsOn": ["^build"] 
    }
  }
}

稍后,当你想要运行端到端测试时,请使用 --only 标志 运行端到端测试,而无需先运行应用的构建。例如,你的命令可能类似于 turbo run e2e --filter=@repo/playwright-myapp --only

¥Later on, when you want to run your end-to-end tests, use the --only flag to run end-to-end tests without running the application's build first. As an example, your command may look like turbo run e2e --filter=@repo/playwright-myapp --only.

共享 Playwright 实用程序

¥Sharing Playwright utilities

你还可以为端到端测试套件中所需的共享实用程序创建一个通用软件包。我们建议在此共享包中使用 peerDependencies,这样你就可以访问用户使用的 Playwright,而无需将 Playwright 安装到共享包本身中。

¥You can also create a common package for shared utilities that you need in your end-to-end test suites. We recommend using peerDependencies in this shared package so that you can get access to Playwright used in consumers without having to install Playwright into the shared package itself.

./packages/playwright-utilities/package.json
{
  "name": "@repo/playwright-utilities",
  "peerDependencies": {
    "playwright": "workspace:*"
}
}