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

タイムアウト

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フックの間で、同じ値の追加の個別のタイムアウトが共有されます。

同じタイムアウト値はbeforeAllafterAllフックにも適用されますが、これらのフックはどのテストとも時間を共有しません。

設定ファイルでテストタイムアウトを設定する

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

export default defineConfig({
timeout: 120_000,
});

APIリファレンス: testConfig.timeout.

単一のテストにタイムアウトを設定する

example.spec.ts
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フックからタイムアウトを変更する

example.spec.ts
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フックのタイムアウトを変更する

beforeAllafterAllフックは個別のタイムアウトを持ち、デフォルトではテストタイムアウトと同じです。各フック内でtestInfo.setTimeout()を呼び出すことで、個別に変更できます。

example.spec.ts
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タイムアウトを設定する

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

export default defineConfig({
expect: {
timeout: 10_000,
},
});

APIリファレンス: testConfig.expect.

単一のアサーションにExpectタイムアウトを指定する

example.spec.ts
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

設定ファイルでグローバルタイムアウトを設定できます。

playwright.config.ts
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 }

設定ファイルでアクションとナビゲーションのタイムアウトを設定する

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

export default defineConfig({
use: {
actionTimeout: 10 * 1000,
navigationTimeout: 30 * 1000,
},
});

APIリファレンス: testOptions.actionTimeout および testOptions.navigationTimeout.

単一のアクションにタイムアウトを設定する

example.spec.ts
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 });
});

フィクスチャタイムアウト

デフォルトでは、フィクスチャはテストとタイムアウトを共有します。しかし、特にworker-scopedのような遅いフィクスチャの場合、個別のタイムアウトを持つのが便利です。これにより、全体のテストタイムアウトを短く保ちつつ、遅いフィクスチャに十分な時間を与えることができます。

example.spec.ts
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().