Skip to main content

超时

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

超时默认值描述
测试超时30000 ms每个测试的超时,包括测试、钩子和 fixtures:
设置默认值
config = { timeout: 60000 }
覆盖
test.setTimeout(120000)
Expect 超时5000 ms每个断言的超时:
设置默认值
config = { expect: { timeout: 10000 } }
覆盖
expect(locator).toBeVisible({ timeout: 10000 })
操作超时无超时每个操作的超时:
设置默认值
config = { use: { actionTimeout: 10000 } }
覆盖
locator.click({ timeout: 10000 })
导航超时无超时每个导航操作的超时:
设置默认值
config = { use: { navigationTimeout: 30000 } }
覆盖
page.goto('/', { timeout: 30000 })
全局超时无超时整个测试运行的全局超时:
在配置中设置
config = { globalTimeout: 60*60*1000 }
beforeAll/afterAll 超时30000 ms钩子的超时:
在钩子中设置
test.setTimeout(60000)
Fixture 超时无超时单个 fixture 的超时:
在 fixture 中设置
{ scope: 'test', timeout: 30000 }

测试超时

Playwright Test 为每个测试强制执行超时,默认为 30 秒。测试函数、fixtures、beforeEachafterEach 钩子所花费的时间都包含在测试超时中。

超时的测试会产生以下错误:

example.spec.ts:3:1 › basic test ===========================

Timeout of 30000ms exceeded.

相同的超时值也适用于 beforeAllafterAll 钩子,但它们不与任何测试共享时间。

在配置中设置测试超时

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

const config: PlaywrightTestConfig = {
timeout: 5 * 60 * 1000,
};
export default config;

API 参考: testConfig.timeout

为单个测试设置超时

import { test, expect } from '@playwright/test';

test('slow test', async ({ page }) => {
test.slow(); // 将默认超时增加三倍的简单方法
// ...
});

test('very slow test', async ({ page }) => {
test.setTimeout(120000);
// ...
});

API 参考: test.setTimeout(timeout)test.slow()

从慢钩子更改超时

import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }, testInfo) => {
// 为运行此钩子的所有测试延长 30 秒超时。
testInfo.setTimeout(testInfo.timeout + 30000);
});

API 参考: testInfo.setTimeout(timeout)

更改 beforeAll/afterAll 钩子的超时

beforeAllafterAll 钩子有单独的超时,默认等于测试超时。您可以通过在钩子内部调用 testInfo.setTimeout(timeout) 为每个钩子单独更改它。

import { test, expect } from '@playwright/test';

test.beforeAll(async () => {
// 为此钩子设置超时。
test.setTimeout(60000);
});

API 参考: testInfo.setTimeout(timeout)

Expect 超时

expect(locator).toHaveText() 这样的 Web-first 断言有单独的超时,默认为 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 type { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
expect: {
timeout: 10 * 1000,
},
};
export default config;

API 参考: testConfig.expect

为单个断言设置超时

import { test, expect } from '@playwright/test';

test('basic test', async ({ page }) => {
await expect(page.getByRole('button')).toHaveText('Sign in', { timeout: 10000 });
});

操作和导航超时

测试通常通过调用 Playwright API 执行一些操作,例如 locator.click()。这些操作默认没有超时,但您可以设置一个。超时的操作会产生以下错误:

example.spec.ts:3:1 › basic test ===========================

locator.click: Timeout 1000ms exceeded.
=========================== logs ===========================
waiting for "locator('button')"
============================================================

Playwright 还允许为导航操作(如 page.goto())设置单独的超时,因为加载页面通常较慢。

在配置中设置操作和导航超时

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

const config: PlaywrightTestConfig = {
use: {
actionTimeout: 10 * 1000,
navigationTimeout: 30 * 1000,
},
};
export default config;

API 参考: testOptions.actionTimeouttestOptions.navigationTimeout

为单个操作设置超时

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 });
});

全局超时

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 type { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
globalTimeout: 60 * 60 * 1000,
};
export default config;

API 参考: testConfig.globalTimeout

Fixture 超时

默认情况下,fixture 与测试共享超时。但是,对于慢 fixtures,尤其是 worker-scoped 的 fixtures,使用单独的超时很方便。这样您可以保持整体测试超时较小,并给慢 fixture 更多时间。

import { test as base, expect } from '@playwright/test';

const test = base.extend<{ slowFixture: string }>({
slowFixture: [async ({}, use) => {
// ... 执行慢操作 ...
await use('hello');
}, { timeout: 60000 }]
});

test('example test', async ({ slowFixture }) => {
// ...
});

API 参考: test.extend(fixtures)