タイムアウト
Playwright Test には、さまざまなタスクに対して設定可能な複数のタイムアウトがあります。
タイムアウト | デフォルト | 説明 |
---|---|---|
テストタイムアウト | 30,000 ミリ秒 | 各テストのタイムアウト 設定ファイルで設定 { timeout: 60_000 } テストでオーバーライド test.setTimeout(120_000) |
Expect タイムアウト | 5,000 ミリ秒 | 各アサーションのタイムアウト 設定ファイルで設定 { expect: { timeout: 10_000 } } テストでオーバーライド expect(locator).toBeVisible({ timeout: 10_000 }) |
テストタイムアウト
Playwright Test は、各テストにタイムアウトを強制します。デフォルトでは 30 秒です。テスト関数、フィクスチャのセットアップ、および beforeEach
フックによって費やされる時間は、テストタイムアウトに含まれます。
タイムアウトしたテストは、次のエラーを生成します
example.spec.ts:3:1 › basic test ===========================
Timeout of 30000ms exceeded.
テスト関数が終了した後、フィクスチャのティアダウンと afterEach
フックの間で、同じ値の追加の独立したタイムアウトが共有されます。
同じタイムアウト値は beforeAll
および afterAll
フックにも適用されますが、それらはどのテストとも時間を共有しません。
設定ファイルでテストタイムアウトを設定する
import { defineConfig } from '@playwright/test';
export default defineConfig({
timeout: 120_000,
});
API リファレンス: testConfig.timeout。
単一のテストのタイムアウトを設定する
import { test, expect } from '@playwright/test';
test('slow test', async ({ page }) => {
test.slow(); // Easy way to triple the default timeout
// ...
});
test('very slow test', async ({ page }) => {
test.setTimeout(120_000);
// ...
});
API リファレンス: test.setTimeout() および test.slow()。
beforeEach
フックからタイムアウトを変更する
import { test, expect } from '@playwright/test';
test.beforeEach(async ({ page }, testInfo) => {
// Extend timeout for all tests running this hook by 30 seconds.
testInfo.setTimeout(testInfo.timeout + 30_000);
});
API リファレンス: testInfo.setTimeout()。
beforeAll
/afterAll
フックのタイムアウトを変更する
beforeAll
および afterAll
フックには、独立したタイムアウトがあり、デフォルトではテストタイムアウトと同じです。フック内で testInfo.setTimeout() を呼び出すことで、各フックごとに個別に変更できます。
import { test, expect } from '@playwright/test';
test.beforeAll(async () => {
// Set timeout for this hook.
test.setTimeout(60000);
});
API リファレンス: testInfo.setTimeout()。
Expect タイムアウト
expect(locator).toHaveText() のような自動再試行アサーションには、独立したタイムアウトがあり、デフォルトでは 5 秒です。アサーションのタイムアウトは、テストタイムアウトとは無関係です。次のエラーを生成します
example.spec.ts:3:1 › basic test ===========================
Error: expect(received).toHaveText(expected)
Expected string: "my text"
Received string: ""
Call log:
- expect.toHaveText with timeout 5000ms
- waiting for "locator('button')"
設定ファイルで Expect タイムアウトを設定する
import { defineConfig } from '@playwright/test';
export default defineConfig({
expect: {
timeout: 10_000,
},
});
API リファレンス: testConfig.expect。
単一のアサーションの Expect タイムアウトを指定する
import { test, expect } from '@playwright/test';
test('example', async ({ page }) => {
await expect(locator).toHaveText('hello', { timeout: 10_000 });
});
グローバルタイムアウト
Playwright Test は、テスト実行全体のタイムアウトをサポートしています。これにより、すべてがうまくいかなかった場合に、過剰なリソース使用を防ぎます。デフォルトのグローバルタイムアウトはありませんが、設定ファイルで適切なもの(たとえば 1 時間)を設定できます。グローバルタイムアウトは、次のエラーを生成します
Running 1000 tests using 10 workers
514 skipped
486 passed
Timed out waiting 3600s for the entire test run
設定ファイルでグローバルタイムアウトを設定できます。
import { defineConfig } from '@playwright/test';
export default defineConfig({
globalTimeout: 3_600_000,
});
API リファレンス: testConfig.globalTimeout。
詳細設定: 低レベルタイムアウト
これらは、テストランナーによって事前設定されている低レベルのタイムアウトであり、これらを変更する必要はないはずです。テストが不安定なためにこのセクションにいる場合は、別の場所で解決策を探す必要がある可能性が非常に高いです。
タイムアウト | デフォルト | 説明 |
---|---|---|
アクションタイムアウト | タイムアウトなし | 各アクションのタイムアウト 設定ファイルで設定 { use: { actionTimeout: 10_000 } } テストでオーバーライド locator.click({ timeout: 10_000 }) |
ナビゲーションタイムアウト | タイムアウトなし | 各ナビゲーションアクションのタイムアウト 設定ファイルで設定 { use: { navigationTimeout: 30_000 } } テストでオーバーライド page.goto('/', { timeout: 30_000 }) |
グローバルタイムアウト | タイムアウトなし | テスト実行全体のグローバルタイムアウト 設定ファイルで設定 { globalTimeout: 3_600_000 } |
beforeAll /afterAll タイムアウト | 30,000 ミリ秒 | フックのタイムアウト フックで設定 test.setTimeout(60_000) |
フィクスチャタイムアウト | タイムアウトなし | 個々のフィクスチャのタイムアウト フィクスチャで設定 { scope: 'test', timeout: 30_000 } |
設定ファイルでアクションおよびナビゲーションタイムアウトを設定する
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
actionTimeout: 10 * 1000,
navigationTimeout: 30 * 1000,
},
});
API リファレンス: testOptions.actionTimeout および testOptions.navigationTimeout。
単一のアクションのタイムアウトを設定する
import { test, expect } from '@playwright/test';
test('basic test', async ({ page }) => {
await page.goto('https://playwright.dokyumento.jp', { timeout: 30000 });
await page.getByText('Get Started').click({ timeout: 10000 });
});
フィクスチャタイムアウト
デフォルトでは、fixture はテストとタイムアウトを共有します。ただし、低速なフィクスチャ、特に worker-scoped なフィクスチャの場合、独立したタイムアウトを持つと便利です。これにより、全体的なテストタイムアウトを小さく保ち、低速なフィクスチャにより多くの時間を与えることができます。
import { test as base, expect } from '@playwright/test';
const test = base.extend<{ slowFixture: string }>({
slowFixture: [async ({}, use) => {
// ... perform a slow operation ...
await use('hello');
}, { timeout: 60_000 }]
});
test('example test', async ({ slowFixture }) => {
// ...
});
API リファレンス: test.extend()。