Skip to main content

配置

介绍

🌐 Introduction

Playwright 有许多选项可以配置测试的运行方式。你可以在配置文件中指定这些选项。请注意,测试运行器选项是顶层的,不要将它们放入 use 部分。

🌐 Playwright has many options to configure how your tests are run. You can specify these options in the configuration file. Note that test runner options are top-level, do not put them into the use section.

基本配置

🌐 Basic Configuration

以下是一些最常见的配置选项。

🌐 Here are some of the most common configuration options.

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
// Look for test files in the "tests" directory, relative to this configuration file.
testDir: 'tests',

// Run all tests in parallel.
fullyParallel: true,

// Fail the build on CI if you accidentally left test.only in the source code.
forbidOnly: !!process.env.CI,

// Retry on CI only.
retries: process.env.CI ? 2 : 0,

// Opt out of parallel tests on CI.
workers: process.env.CI ? 1 : undefined,

// Reporter to use
reporter: 'html',

use: {
// Base URL to use in actions like `await page.goto('/')`.
baseURL: 'http://localhost:3000',

// Collect trace when retrying the failed test.
trace: 'on-first-retry',
},
// Configure projects for major browsers.
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
// Run your local dev server before starting the tests.
webServer: {
command: 'npm run start',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
},
});

| 选项 | 描述 |

| :- | :- |

| testConfig.forbidOnly | 如果有任何测试被标记为 test.only,是否以错误退出。在持续集成(CI)中很有用。|

| testConfig.fullyParallel | 使所有文件中的所有测试并行运行。有关更多详细信息,请参见 并行性分片。 |

| testConfig.projects | 在多种配置或多个浏览器上运行测试 |

| testConfig.reporter | 要使用的报告器。请参见 测试报告器 以了解可用的报告器。 |

| testConfig.retries | 每个测试的最大重试次数。请参阅 测试重试 以了解有关重试的更多信息。|

| testConfig.testDir | 测试文件所在的目录。 |

| testConfig.use | 带有 use{} 的选项 |

| testConfig.webServer | 在测试期间启动服务器,请使用 webServer 选项 |

| testConfig.workers | 用于并行化测试的最大并发工作进程数。也可以设置为逻辑 CPU 核心的百分比,例如 '50%'.。更多详情请参见 并行化分片。 |

过滤测试

🌐 Filtering Tests

按通配符模式或正则表达式过滤测试。

🌐 Filter tests by glob patterns or regular expressions.

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
// Glob patterns or regular expressions to ignore test files.
testIgnore: '*test-assets',

// Glob patterns or regular expressions that match test files.
testMatch: '*todo-tests/*.spec.ts',
});
选项描述
testConfig.testIgnore当查找测试文件时应忽略的通配符模式或正则表达式。例如,'*test-assets'
testConfig.testMatch匹配测试文件的通配符模式或正则表达式。例如,'*todo-tests/*.spec.ts'。默认情况下,Playwright 会运行 .*(test|spec).(js|ts|mjs) 文件。

高级配置

🌐 Advanced Configuration

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
// Folder for test artifacts such as screenshots, videos, traces, etc.
outputDir: 'test-results',

// path to the global setup files.
globalSetup: require.resolve('./global-setup'),

// path to the global teardown files.
globalTeardown: require.resolve('./global-teardown'),

// Each test is given 30 seconds.
timeout: 30000,

});
选项描述
testConfig.globalSetup全局设置文件的路径。在所有测试之前,此文件将被引入并执行。它必须导出一个单独的函数。
testConfig.globalTeardown全局清理文件的路径。在所有测试之后,此文件将被引入并执行。它必须导出一个单独的函数。
testConfig.outputDir存放测试产物的文件夹,如截图、视频、跟踪等。
testConfig.timeoutPlaywright 为每个测试执行超时限制,默认值为30秒。测试函数、测试夹具以及 beforeEach 钩子所花费的时间都计入测试超时。

期望选项

🌐 Expect Options

期望断言库的配置。

🌐 Configuration for the expect assertion library.

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
expect: {
// Maximum time expect() should wait for the condition to be met.
timeout: 5000,

toHaveScreenshot: {
// An acceptable amount of pixels that could be different, unset by default.
maxDiffPixels: 10,
},

toMatchSnapshot: {
// An acceptable ratio of pixels that are different to the
// total amount of pixels, between 0 and 1.
maxDiffPixelRatio: 0.1,
},
},

});
选项描述
testConfig.expect类似 expect(locator).toHaveText()Web 首次断言 默认有单独的 5 秒超时。这是 expect() 等待条件满足的最长时间。了解有关 测试和断言超时 的更多信息,以及如何为单个测试设置它们。
expect(page).toHaveScreenshot()expect(locator).toHaveScreenshot() 方法的配置。
expect(value).toMatchSnapshot()expect(locator).toMatchSnapshot() 方法的配置。