项目
简介
项目是使用相同配置运行的测试的逻辑分组。我们使用项目以便可以在不同的浏览器和设备上运行测试。项目在 playwright.config.ts
文件中配置,配置完成后,您可以在所有项目或仅在特定项目上运行测试。您还可以使用项目在不同的配置下运行相同的测试。例如,您可以在登录和退出登录状态下运行相同的测试。
通过设置项目,您还可以运行具有不同超时或重试次数的一组测试,或者针对不同环境(例如预发环境和生产环境)运行一组测试,按包/功能拆分测试等等。
为多个浏览器配置项目
通过使用**项目**,您可以在多个浏览器中运行测试,例如 chromium、webkit 和 firefox,以及品牌浏览器(例如 Google Chrome 和 Microsoft Edge)。Playwright 还可以在模拟的平板电脑和移动设备上运行。请参阅设备参数注册表,了解选定桌面、平板电脑和移动设备的完整列表。
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
/* Test against mobile viewports. */
{
name: 'Mobile Chrome',
use: { ...devices['Pixel 5'] },
},
{
name: 'Mobile Safari',
use: { ...devices['iPhone 12'] },
},
/* Test against branded browsers. */
{
name: 'Microsoft Edge',
use: {
...devices['Desktop Edge'],
channel: 'msedge'
},
},
{
name: 'Google Chrome',
use: {
...devices['Desktop Chrome'],
channel: 'chrome'
},
},
],
});
运行项目
Playwright 默认会运行所有项目。
npx playwright test
Running 7 tests using 5 workers
✓ [chromium] › example.spec.ts:3:1 › basic test (2s)
✓ [firefox] › example.spec.ts:3:1 › basic test (2s)
✓ [webkit] › example.spec.ts:3:1 › basic test (2s)
✓ [Mobile Chrome] › example.spec.ts:3:1 › basic test (2s)
✓ [Mobile Safari] › example.spec.ts:3:1 › basic test (2s)
✓ [Microsoft Edge] › example.spec.ts:3:1 › basic test (2s)
✓ [Google Chrome] › example.spec.ts:3:1 › basic test (2s)
使用 --project
命令行选项来运行单个项目。
npx playwright test --project=firefox
Running 1 test using 1 worker
✓ [firefox] › example.spec.ts:3:1 › basic test (2s)
VS Code 测试运行器在默认的 Chrome 浏览器上运行您的测试。要在其他/多个浏览器上运行,请单击测试侧边栏中运行按钮的下拉菜单,选择另一个配置文件,或者通过单击 Select Default Profile 修改默认配置文件,然后选择您希望在其上运行测试的浏览器。

选择特定配置文件、多个配置文件或所有配置文件来运行测试。

为多个环境配置项目
通过设置项目,我们还可以运行具有不同超时或重试次数的一组测试,或针对不同环境运行一组测试。例如,我们可以针对预发环境运行测试并重试 2 次,也可以针对生产环境运行测试并重试 0 次。
import { defineConfig } from '@playwright/test';
export default defineConfig({
timeout: 60000, // Timeout is shared between all tests.
projects: [
{
name: 'staging',
use: {
baseURL: 'staging.example.com',
},
retries: 2,
},
{
name: 'production',
use: {
baseURL: 'production.example.com',
},
retries: 0,
},
],
});
将测试拆分为项目
我们可以将测试拆分为项目,并使用过滤器来运行测试子集。例如,我们可以创建一个项目,该项目使用匹配特定文件名所有测试的过滤器来运行测试。然后,我们可以有另一组测试忽略特定的测试文件。
下面是一个定义通用超时和两个项目的示例。“Smoke”项目运行一小部分测试且不重试,“Default”项目运行所有其他测试并重试。
import { defineConfig } from '@playwright/test';
export default defineConfig({
timeout: 60000, // Timeout is shared between all tests.
projects: [
{
name: 'Smoke',
testMatch: /.*smoke.spec.ts/,
retries: 0,
},
{
name: 'Default',
testIgnore: /.*smoke.spec.ts/,
retries: 2,
},
],
});
依赖项
依赖项是一个项目列表,它们需要在另一个项目中的测试运行之前运行。它们对于配置全局 setup 操作非常有用,以便某个项目依赖于这些 setup 操作首先运行。当使用项目依赖项时,测试报告器将显示 setup 测试,并且跟踪查看器将记录 setup 的跟踪。您可以使用检查器来检查 setup 测试跟踪的 DOM 快照,并且您还可以在 setup 中使用 fixtures。
在此示例中,chromium、firefox 和 webkit 项目依赖于 setup 项目。
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
projects: [
{
name: 'setup',
testMatch: '**/*.setup.ts',
},
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
dependencies: ['setup'],
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
dependencies: ['setup'],
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
dependencies: ['setup'],
},
],
});
运行顺序
当处理具有依赖项的测试时,依赖项将始终首先运行,并且一旦此项目中的所有测试都通过,其他项目将并行运行。
运行顺序
-
'setup' 项目中的测试运行。一旦此项目中的所有测试都通过,依赖项目中的测试将开始运行。
-
'chromium'、'webkit' 和 'firefox' 项目中的测试一起运行。默认情况下,这些项目会并行运行,但受限于最大 worker 数。

如果存在多个依赖项,则这些项目依赖项将首先并行运行。如果来自依赖项的测试失败,则依赖于此项目的测试将不会运行。
运行顺序
- 'Browser Login' 和 'DataBase' 项目中的测试并行运行
- 'Browser Login' 通过
- ❌ 'DataBase' 失败!
- 'e2e tests' 项目未运行!

Teardown
您还可以通过向 setup 项目添加 testProject.teardown 属性来 teardown 您的 setup。Teardown 将在所有依赖项目运行后运行。有关更多信息,请参阅 teardown 指南。
测试过滤
所有测试过滤选项,例如 --grep
/--grep-invert
、--shard
、直接通过命令行位置过滤,或者使用 test.only()
,都会直接选择要运行的主要测试。如果这些测试属于具有依赖项的项目,则来自这些依赖项的所有测试也将运行。
您可以传递 --no-deps
命令行选项来忽略所有依赖项和 teardowns。只有您直接选择的项目将运行。
自定义项目参数
项目还可以用于使用您的自定义配置来参数化测试 - 请参阅这篇单独的指南。