タイムアウト
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 });
});
フィクスチャタイムアウト
デフォルトでは、フィクスチャはテストとタイムアウトを共有します。ただし、遅いフィクスチャ、特にワーカーースコープのフィクスチャの場合、個別のタイムアウトを設定すると便利です。これにより、全体のテストタイムアウトを短く保ち、遅いフィクスチャにより多くの時間を与えることができます。
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()。