跳至主要内容

项目

简介

项目是指使用相同配置运行的测试的逻辑分组。我们使用项目是为了能在不同的浏览器和设备上运行测试。项目在playwright.config.ts文件中进行配置,配置完成后,您可以选择在所有项目上运行测试,或仅在特定项目上运行。您还可以利用项目在不同的配置下运行相同的测试。例如,您可以在登录状态和未登录状态下运行相同的测试。

通过设置项目,您还可以运行一组具有不同超时或重试次数的测试,或者针对不同环境(如预发布和生产环境)运行一组测试,按包/功能拆分测试等等。

为多浏览器配置项目

通过使用projects功能,您可以在多种浏览器中运行测试,包括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 浏览器上运行您的测试。要在其他/多个浏览器上运行,请点击测试侧边栏中播放按钮的下拉菜单,选择其他配置文件,或通过点击选择默认配置文件来修改默认配置,并选择您希望运行测试的浏览器。

selecting browsers

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

choosing default profiles

为多环境配置项目

通过设置项目,我们还可以运行一组具有不同超时或重试次数的测试,或者针对不同环境运行一组测试。例如,我们可以针对暂存环境运行测试,设置2次重试,同时针对生产环境运行测试,设置0次重试。

playwright.config.ts
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"项目运行所有其他测试并带有重试机制。

playwright.config.ts
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,
},
],
});

依赖项

依赖项是指在另一个项目的测试运行之前需要先运行的项目列表。它们可用于配置全局设置操作,使一个项目依赖于首先运行的这些设置。当使用项目依赖项时,测试报告器将显示设置测试,追踪查看器将记录设置的追踪信息。您可以使用检查器来检查设置测试追踪的DOM快照,也可以在设置中使用fixtures

在这个示例中,chromium、firefox和webkit项目都依赖于setup项目。

playwright.config.ts
import { defineConfig } 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'],
},
],
});

运行序列

当处理具有依赖关系的测试时,依赖项将始终首先运行,一旦该项目中的所有测试都通过,其他项目将并行运行。

运行顺序:

  1. 'setup'项目中的测试会先运行。一旦该项目中的所有测试通过,依赖项目的测试才会开始运行。

  2. 'chromium'、'webkit' 和 'firefox' 项目中的测试会一起运行。默认情况下,这些项目将并行运行,受限于最大工作线程数限制。

chromium, webkit and firefox projects depend on setup project

如果有多个依赖项,那么这些项目依赖项将首先并行运行。如果某个依赖项的测试失败,那么依赖该项目的测试将不会运行。

运行顺序:

  1. Tests in the 'Browser Login' and 'DataBase' projects run in parallel:
    • '浏览器登录' 通过
    • ❌ 'DataBase' 失败!
  2. 'e2e tests'项目没有运行!
Browser login project is blue, database is red and e2e tests relies on both

拆卸

您还可以通过向设置项目添加testProject.teardown属性来拆除您的设置。拆除操作将在所有依赖项目运行后执行。更多信息请参阅teardown指南

global setup and teardown

测试筛选

如果使用了--grep/--grep-invert--shard option选项,在command line中指定了测试文件名过滤器,或者使用了test.only(),这些过滤条件将仅应用于项目依赖链中最深层项目中的测试。换句话说,如果匹配的测试属于一个有项目依赖关系的项目,Playwright将忽略这些过滤器,运行来自项目依赖关系的所有测试。

自定义项目参数

项目也可以用于根据您的自定义配置参数化测试 - 请查看这份独立指南