跳至主要内容

超时设置

Playwright Test 为各种任务提供了多个可配置的超时设置。

TimeoutDefaultDescription
Test timeout30_000 msTimeout for each test
Set in config
{ timeout: 60_000 }
Override in test
test.setTimeout(120_000)
Expect timeout5_000 msTimeout for each assertion
Set in config
{ expect: { timeout: 10_000 } }
Override in test
expect(locator).toBeVisible({ timeout: 10_000 })

测试超时

Playwright Test 为每个测试强制执行一个超时时间,默认为30秒。测试函数、fixture设置以及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(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

为单个断言指定预期超时时间

example.spec.ts
import { test, expect } from '@playwright/test';

test('example', async ({ page }) => {
await expect(locator).toHaveText('hello', { timeout: 10_000 });
});

全局超时设置

Playwright Test 支持为整个测试运行设置超时。这可以防止在一切出错时资源被过度占用。默认情况下没有全局超时设置,但你可以在配置中设置一个合理的值,例如一小时。全局超时会产生以下错误:

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

高级:底层超时设置

这些是由测试运行器预先配置的低级超时设置,通常不需要修改。如果您因为测试不稳定而查看此部分,很可能应该在其他地方寻找解决方案。

TimeoutDefaultDescription
Action timeoutno timeoutTimeout for each action
Set in config
{ use: { actionTimeout: 10_000 } }
Override in test
locator.click({ timeout: 10_000 })
Navigation timeoutno timeoutTimeout for each navigation action
Set in config
{ use: { navigationTimeout: 30_000 } }
Override in test
page.goto('/', { timeout: 30_000 })
Global timeoutno timeoutGlobal timeout for the whole test run
Set in config
{ globalTimeout: 3_600_000 }
beforeAll/afterAll timeout30_000 msTimeout for the hook
Set in hook
test.setTimeout(60_000)
Fixture timeoutno timeoutTimeout for an individual fixture
Set in fixture
{ 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.actionTimeouttestOptions.navigationTimeout

为单个操作设置超时

example.spec.ts
import { test, expect } from '@playwright/test';

test('basic test', async ({ page }) => {
await page.goto('https://playwright.dev', { timeout: 30000 });
await page.getByText('Get Started').click({ timeout: 10000 });
});

Fixture超时

默认情况下,fixture与测试共享超时设置。但对于耗时较长的fixture,尤其是worker-scoped这种作用域范围的,单独设置超时会更方便。这样您可以保持整体测试的超时时间较短,同时为耗时的fixture分配更多时间。

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()