跳至主要内容

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)

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

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

用法

PlaywrightAssertions.assertThat(response).isOK();

参数

返回


assertThat(定位器)

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

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

用法

PlaywrightAssertions.assertThat(locator).isVisible();

参数

返回


assertThat(page)

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

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

用法

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

参数

返回


setDefaultAssertionTimeout

Added in: v1.25 playwrightAssertions.setDefaultAssertionTimeout

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

用法

PlaywrightAssertions.setDefaultAssertionTimeout(30_000);

参数

  • timeout double#

    超时时间(毫秒)。