Skip to content

PageAssertions ​

Added in: v1.17

The PageAssertions class provides assertion methods that can be used to make assertions about the Page state in the tests.

java
// ...
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

public class TestPage {
  // ...
  @Test
  void navigatesToLoginPage() {
    // ...
    page.getByText("Sign in").click();
    assertThat(page).hasURL(Pattern.compile(".*/login"));
  }
}

Methods ​

pageAssertions.hasTitle() ​

Added in: v1.20

Ensures the page has the given title.

Usage

java
assertThat(page).hasTitle("Playwright");

Arguments

  • titleOrRegExp string | RegExp Added in: v1.18

    Expected title or RegExp.

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

pageAssertions.hasURL() ​

Added in: v1.20

Ensures the page is navigated to the given URL.

Usage

java
assertThat(page).hasURL(".com");

Arguments

  • urlOrRegExp string | RegExp Added in: v1.18

    Expected URL string or RegExp.

  • options Object (optional)

    • ignoreCase boolean (optional) Added in: v1.44

      Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression parameter if specified. A provided predicate ignores this flag.

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

pageAssertions.matchesAriaSnapshot() ​

Added in: v1.60

Asserts that the page body matches the given accessibility snapshot.

Usage

java
page.navigate("https://demo.playwright.dev/todomvc/");
assertThat(page).matchesAriaSnapshot("""
  - heading "todos"
  - textbox "What needs to be done?"
""");

Arguments

  • expected string

  • options Object (optional)

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

Properties ​

pageAssertions.not() ​

Added in: v1.20

Makes the assertion check for the opposite condition.

Usage

For example, this code tests that the page URL doesn't contain "error":

java
assertThat(page).not().hasURL("error");

Type

  • PageAssertions

Released under the Apache 2.0 License.