测试配置

简介

Playwright 提供了许多配置选项,用于定制测试的运行方式。你可以在配置文件中指定这些选项。需要注意的是,测试运行器选项是顶层配置,不能放入 use 部分。

基本配置

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

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://127.0.0.1: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://127.0.0.1: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

启动测试期间需要的服务器。

testConfig.workers

用于并行化测试的最大并发工作进程数。也可以设置为逻辑 CPU 核心的百分比,例如 '50%'。详细信息见并行性和分片。

过滤测试

可以通过 glob 模式或正则表达式过滤测试。

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

在查找测试文件时应忽略的 glob 模式或正则表达式。例如,*test-assets

testConfig.testMatch

匹配测试文件的 glob 模式或正则表达式。例如,*todo-tests/*.spec.ts。默认情况下,Playwright 会运行 .*(test|spec).(js|ts|mjs) 文件。

高级配置

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.timeout

Playwright 强制为每个测试设置超时,默认为 30 秒。测试函数、测试装置和 beforeEach 钩子花费的时间会包括在测试超时内。

期望选项

配置 expect 断言库。

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

export default defineConfig({
  expect: {
    // `expect()` 等待条件满足的最大时间。
    timeout: 5000,

    toHaveScreenshot: {
      // 可接受的像素差异量,默认未设置。
      maxDiffPixels: 10,
    },

    toMatchSnapshot: {
      // 可接受的像素差异比例,
      // 与总像素的比率,范围从 0 到 1。
      maxDiffPixelRatio: 0.1,
    },
  },
});

配置选项说明:

选项 描述

testConfig.expect

expect(locator).toHaveText() 等 Web 断言有一个单独的超时时间,默认是 5 秒。这是 expect() 等待条件满足的最大时间。了解更多关于测试和 expect 超时以及如何为单个测试设置它们的信息。

expect(page).toHaveScreenshot()

配置 expect(locator).toHaveScreenshot() 方法。

expect(value).toMatchSnapshot()

配置 expect(locator).toMatchSnapshot() 方法。