跳至主要内容

PlaywrightAssertions

Playwright为您提供了Web优先断言功能,包含便捷方法用于创建断言,这些断言会等待并重试,直到满足预期条件。

考虑以下示例:

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

test('status becomes submitted', async ({ page }) => {
// ...
await page.locator('#submit-button').click();
await expect(page.locator('.status')).toHaveText('Submitted');
});

Playwright 将重新测试带有选择器 .status 的节点,直到获取的节点包含 "Submitted" 文本。它会不断重新获取节点并进行检查,直到满足条件或达到超时时间。您可以将此超时时间作为选项传递。

默认情况下,断言超时时间设置为5秒。


方法

expect(response)

Added in: v1.18 playwrightAssertions.expect(response)

为给定的APIResponse创建一个APIResponseAssertions对象。

用法

参数

返回


expect(value)

Added in: v1.9 playwrightAssertions.expect(value)

为给定值创建一个GenericAssertions对象。

用法

expect(value);

参数

  • value Object#

    将被断言的值。

返回


expect(locator)

Added in: v1.18 playwrightAssertions.expect(locator)

为给定的Locator创建一个LocatorAssertions对象。

用法

参数

返回


expect(page)

Added in: v1.18 playwrightAssertions.expect(page)

为给定的Page创建一个PageAssertions对象。

用法

参数

返回