跳到主要內容

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

參數

回傳


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#

    逾時時間,以毫秒為單位。