メインコンテンツにスキップ

テスト構成

はじめに

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: 'https://: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: 'https://:3000',
reuseExistingServer: !process.env.CI,
},
});
オプション説明
testConfig.forbidOnlyいずれかのテストが test.only とマークされている場合にエラーで終了するかどうか。CI で役立ちます。
testConfig.fullyParallelすべてのファイルのすべてのテストを並列で実行します。詳細については、並列実行シャーディングを参照してください。
testConfig.projects複数の構成または複数のブラウザでテストを実行します。
testConfig.reporter使用するレポーター。利用可能なレポーターの詳細については、テストレポーターを参照してください。
testConfig.retriesテストごとの最大再試行回数。リトライの詳細については、テストリトライを参照してください。
testConfig.testDirテストファイルが格納されているディレクトリ。
testConfig.useuse{} を使用するオプション
testConfig.webServerテスト中にサーバーを起動するには、webServer オプションを使用します。
testConfig.workersテストを並列化するために使用する同時ワーカプロセスの最大数。論理 CPU コアの割合として設定することもできます (例: '50%')。詳細については、並列実行シャーディングを参照してください。

テストのフィルタリング

グロブパターンまたは正規表現でテストをフィルタリングします。

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) ファイルを実行します。

高度な構成

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 オプション

expect アサーションライブラリの設定。

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.expectexpect(locator).toHaveText() のようなWeb ファーストアサーションは、デフォルトで 5 秒の個別のタイムアウトを持ちます。これは、expect() が条件が満たされるまで待機すべき最大時間です。テストとexpectのタイムアウト、および個別のテストに設定する方法について詳しく学びます。
expect(page).toHaveScreenshot()expect(locator).toHaveScreenshot() メソッドの設定。
expect(value).toMatchSnapshot()expect(locator).toMatchSnapshot() メソッドの設定。