Skip to main content

PlaywrightAssertions

Playwright gives you Web-First Assertions with convenience methods for creating assertions that will wait and retry until the expected condition is met.

Consider the following example:

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 will be re-testing the node with the selector .status until fetched Node has the "Submitted" text. It will be re-fetching the node and checking it over and over, until the condition is met or until the timeout is reached. You can pass this timeout as an option.

By default, the timeout for assertions is set to 5 seconds.


Methods

assertThat(response)

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

Creates a APIResponseAssertions object for the given APIResponse.

Usage

PlaywrightAssertions.assertThat(response).isOK();

Arguments

Returns


assertThat(locator)

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

Creates a LocatorAssertions object for the given Locator.

Usage

PlaywrightAssertions.assertThat(locator).isVisible();

Arguments

Returns


assertThat(page)

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

Creates a PageAssertions object for the given Page.

Usage

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

Arguments

  • page Page#

    Page object to use for assertions.

Returns


setDefaultAssertionTimeout

Added in: v1.25 playwrightAssertions.setDefaultAssertionTimeout

Changes default timeout for Playwright assertions from 5 seconds to the specified value.

Usage

PlaywrightAssertions.setDefaultAssertionTimeout(30_000);

Arguments

  • timeout double#

    Timeout in milliseconds.