TestProject
Playwright Test 支持同时运行多个测试项目。这对于在多种配置下运行测试非常有用。例如,可以针对多个浏览器运行测试。
TestProject 封装了单个项目的特定配置。项目在配置文件中通过 testConfig.projects 进行配置。需要注意的是,TestProject 的所有属性都可以在顶层 TestConfig 中使用,这种情况下它们会在所有项目之间共享。
以下是一个配置示例,在 Chromium、Firefox 和 WebKit 的桌面版和移动版中运行所有测试。
- TypeScript
- JavaScript
// playwright.config.ts
import { type PlaywrightTestConfig, devices } from '@playwright/test';
const config: PlaywrightTestConfig = {
// 所有项目共享的选项
timeout: 30000,
use: {
ignoreHTTPSErrors: true,
},
// 每个项目特定的选项
projects: [
{
name: 'Desktop Chromium',
use: {
browserName: 'chromium',
viewport: { width: 1280, height: 720 },
},
},
{
name: 'Desktop Safari',
use: {
browserName: 'webkit',
viewport: { width: 1280, height: 720 },
}
},
{
name: 'Desktop Firefox',
use: {
browserName: 'firefox',
viewport: { width: 1280, height: 720 },
}
},
{
name: 'Mobile Chrome',
use: devices['Pixel 5'],
},
{
name: 'Mobile Safari',
use: devices['iPhone 12'],
},
],
};
export default config;
// playwright.config.js
// @ts-check
const { devices } = require('@playwright/test');
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
// 所有项目共享的选项
timeout: 30000,
use: {
ignoreHTTPSErrors: true,
},
// 每个项目特定的选项
projects: [
{
name: 'Desktop Chromium',
use: {
browserName: 'chromium',
viewport: { width: 1280, height: 720 },
},
},
{
name: 'Desktop Safari',
use: {
browserName: 'webkit',
viewport: { width: 1280, height: 720 },
}
},
{
name: 'Desktop Firefox',
use: {
browserName: 'firefox',
viewport: { width: 1280, height: 720 },
}
},
{
name: 'Mobile Chrome',
use: devices['Pixel 5'],
},
{
name: 'Mobile Safari',
use: devices['iPhone 12'],
},
],
};
module.exports = config;
- testProject.expect
- testProject.fullyParallel
- testProject.grep
- testProject.grepInvert
- testProject.metadata
- testProject.name
- testProject.outputDir
- testProject.repeatEach
- testProject.retries
- testProject.snapshotDir
- testProject.testDir
- testProject.testIgnore
- testProject.testMatch
- testProject.timeout
- testProject.use
testProject.expect
Added in: v1.10- type: <Object>
timeout?<number> 异步 expect 匹配器的默认超时时间,以毫秒为单位,默认为 5000 毫秒。toHaveScreenshot?<Object> expect(page).toHaveScreenshot(name[, options]) 方法的配置。threshold?<number> 在 YIQ 色彩空间中,对比图像中相同像素之间可接受的感知色差,取值范围从 0(严格)到 1(宽松)。默认为0.2。maxDiffPixels?<number> 可接受的不同像素数量,默认未设置。maxDiffPixelRatio?<number> 不同像素占总像素的可接受比例,取值范围为0到1,默认未设置。animations?<"allow"|"disabled"> 参见 page.screenshot([options]) 中的animations。默认为"disabled"。caret?<"hide"|"initial"> 参见 page.screenshot([options]) 中的caret。默认为"hide"。scale?<"css"|"device"> 参见 page.screenshot([options]) 中的scale。默认为"css"。
toMatchSnapshot?<Object> expect(screenshot).toMatchSnapshot(name[, options]) 方法的配置。
expect 断言库的配置。
使用 testConfig.expect 可为所有项目更改此选项。
testProject.fullyParallel
Added in: v1.10- type: <boolean>
Playwright Test 并行运行测试。为了实现这一点,它会同时运行多个工作进程。默认情况下,测试文件并行运行。单个文件中的测试在同一个工作进程中按顺序运行。
您可以使用此选项将整个测试项目配置为并发运行所有文件中的所有测试。
testProject.grep
Added in: v1.10仅运行标题与其中一个模式匹配的测试。例如,传递 grep: /cart/ 将只运行标题中包含 "cart" 的测试。也可以在全局和命令行中使用 -g 选项。
grep 选项对于标记测试也很有用。
testProject.grepInvert
Added in: v1.10仅运行标题不匹配其中一个模式的测试。这与 testProject.grep 相反。也可以在全局和命令行中使用 --grep-invert 选项。
grepInvert 选项对于标记测试也很有用。
testProject.metadata
Added in: v1.10- type: <[Metadata]>
将以 JSON 格式序列化后直接放入测试报告的元数据。
testProject.name
Added in: v1.10- type: <string>
项目名称在报告和测试执行期间可见。
testProject.outputDir
Added in: v1.10- type: <string>
测试执行期间创建的文件的输出目录。默认为 <package.json-directory>/test-results。
此目录在开始时会被清空。运行测试时,会在 testProject.outputDir 内创建一个唯一的子目录,确保并行运行的测试不会冲突。可以通过 testInfo.outputDir 和 testInfo.outputPath(...pathSegments) 访问此目录。
以下是使用 testInfo.outputPath(...pathSegments) 创建临时文件的示例。
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
import fs from 'fs';
test('example test', async ({}, testInfo) => {
const file = testInfo.outputPath('temporary-file.txt');
await fs.promises.writeFile(file, 'Put some data to the file', 'utf8');
});
const { test, expect } = require('@playwright/test');
const fs = require('fs');
test('example test', async ({}, testInfo) => {
const file = testInfo.outputPath('temporary-file.txt');
await fs.promises.writeFile(file, 'Put some data to the file', 'utf8');
});
使用 testConfig.outputDir 可为所有项目更改此选项。
testProject.repeatEach
Added in: v1.10- type: <number>
每个测试重复运行的次数,对于调试不稳定的测试很有用。
使用 testConfig.repeatEach 可为所有项目更改此选项。
testProject.retries
Added in: v1.10- type: <number>
失败测试的最大重试次数。了解更多关于测试重试的信息。
使用 testConfig.retries 可为所有项目更改此选项。
testProject.snapshotDir
Added in: v1.10- type: <string>
使用 toMatchSnapshot 创建的快照文件的基础目录,相对于配置文件。默认为 testProject.testDir。
每个测试的目录可以通过 testInfo.snapshotDir 和 testInfo.snapshotPath(...pathSegments) 访问。
此路径将作为每个测试文件快照目录的基础目录。将 snapshotDir 设置为 'snapshots',testInfo.snapshotDir 将解析为 snapshots/a.spec.js-snapshots。
testProject.testDir
Added in: v1.10- type: <string>
将递归扫描测试文件的目录。默认为配置文件所在的目录。
每个项目可以使用不同的目录。以下是一个在三个浏览器中运行冒烟测试,并在稳定版 Chrome 浏览器中运行所有其他测试的示例。
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
projects: [
{
name: 'Smoke Chromium',
testDir: './smoke-tests',
use: {
browserName: 'chromium',
}
},
{
name: 'Smoke WebKit',
testDir: './smoke-tests',
use: {
browserName: 'webkit',
}
},
{
name: 'Smoke Firefox',
testDir: './smoke-tests',
use: {
browserName: 'firefox',
}
},
{
name: 'Chrome Stable',
testDir: './',
use: {
browserName: 'chromium',
channel: 'chrome',
}
},
],
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
projects: [
{
name: 'Smoke Chromium',
testDir: './smoke-tests',
use: {
browserName: 'chromium',
}
},
{
name: 'Smoke WebKit',
testDir: './smoke-tests',
use: {
browserName: 'webkit',
}
},
{
name: 'Smoke Firefox',
testDir: './smoke-tests',
use: {
browserName: 'firefox',
}
},
{
name: 'Chrome Stable',
testDir: './',
use: {
browserName: 'chromium',
channel: 'chrome',
}
},
],
};
module.exports = config;
使用 testConfig.testDir 可为所有项目更改此选项。
testProject.testIgnore
Added in: v1.10匹配这些模式之一的文件不会作为测试文件执行。匹配针对绝对文件路径进行。字符串被视为 glob 模式。
例如,'**/test-assets/**' 将忽略 test-assets 目录中的所有文件。
使用 testConfig.testIgnore 可为所有项目更改此选项。
testProject.testMatch
Added in: v1.10只有匹配这些模式之一的文件才会作为测试文件执行。匹配针对绝对文件路径进行。字符串被视为 glob 模式。
默认情况下,Playwright Test 查找匹配 .*(test|spec)\.(js|ts|mjs) 的文件。
使用 testConfig.testMatch 可为所有项目更改此选项。
testProject.timeout
Added in: v1.10- type: <number>
每个测试的超时时间,以毫秒为单位。默认为 30 秒。
这是所有测试的基础超时时间。此外,每个测试可以使用 test.setTimeout(timeout) 配置自己的超时时间。
使用 testConfig.timeout 可为所有项目更改此选项。
testProject.use
Added in: v1.10- type: <Fixtures>
此项目中所有测试的选项,例如 testOptions.browserName。了解更多关于配置的信息,并查看可用选项。
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
projects: [
{
name: 'Chromium',
use: {
browserName: 'chromium',
},
},
],
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
projects: [
{
name: 'Chromium',
use: {
browserName: 'chromium',
},
},
],
};
module.exports = config;
使用 testConfig.use 可为所有项目更改此选项。