跳到主要内容

PlaywrightAssertions

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

考虑以下示例:

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

public class TestExample {
// ...
@Test
void statusBecomesSubmitted() {
// ...
page.locator("#submit-button").click();
assertThat(page.locator(".status")).hasText("Submitted");
}
}

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

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


方法

assertThat(response)

添加于:v1.18 playwrightAssertions.assertThat(response)

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

用法

PlaywrightAssertions.assertThat(response).isOK();

参数

返回值


assertThat(locator)

新增于:v1.18 playwrightAssertions.assertThat(locator)

为给定的 [Locator(定位器)] 创建一个 [LocatorAssertions(定位器断言)] 对象。

用法

PlaywrightAssertions.assertThat(locator).isVisible();

参数

  • locator [Locator(定位器)]#

    用于断言的 [Locator(定位器)] 对象。

返回值

  • [LocatorAssertions(定位器断言)]#

assertThat(page)

新增于:v1.18 playwrightAssertions.assertThat(page)

为给定的 [Page(页面)] 创建一个 [PageAssertions(页面断言)] 对象。

用法

PlaywrightAssertions.assertThat(page).hasTitle("News");

参数

  • page [Page(页面)]#

    用于断言的 [Page(页面)] 对象。

返回值

  • [PageAssertions(页面断言)]#

setDefaultAssertionTimeout

新增于:v1.25 playwrightAssertions.setDefaultAssertionTimeout

将 Playwright 断言的默认超时时间从 5 秒更改为指定值。

用法

PlaywrightAssertions.setDefaultAssertionTimeout(30_000);

参数

  • timeout double#

    超时时间(毫秒)。