跳转到主要内容

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

参数

返回


assertThat(page)

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

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

用法

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

参数

返回


setDefaultAssertionTimeout

新增于: v1.25 playwrightAssertions.setDefaultAssertionTimeout

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

用法

PlaywrightAssertions.setDefaultAssertionTimeout(30_000);

参数

  • timeout double#

    超时时间,单位为毫秒。