跳到主要内容

PlaywrightAssertions

Playwright 提供了 Web-First 断言,其便利的方法可用于创建会等待并重试直到满足预期条件的断言。

请看以下示例

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

参数

  • response APIResponse#

    用于断言的 APIResponse 对象。

返回值


assertThat(locator)

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

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

用法

PlaywrightAssertions.assertThat(locator).isVisible();

参数

  • locator Locator#

    用于断言的 Locator 对象。

返回值


assertThat(page)

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

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

用法

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

参数

  • page Page#

    用于断言的 Page 对象。

返回值


setDefaultAssertionTimeout

添加于: v1.25 playwrightAssertions.setDefaultAssertionTimeout

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

用法

PlaywrightAssertions.setDefaultAssertionTimeout(30_000);

参数

  • timeout double#

    超时,单位为毫秒。