Skip to main content

PageAssertions

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

...
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

hasTitle

Added in: v1.20 pageAssertions.hasTitle

Ensures the page has the given title.

Usage

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

Arguments

  • titleOrRegExp String | Pattern Added in: v1.18#

    Expected title or RegExp.

  • options PageAssertions.HasTitleOptions (optional)

    • setTimeout double (optional) Added in: v1.18#

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

Returns


hasURL

Added in: v1.20 pageAssertions.hasURL

Ensures the page is navigated to the given URL.

Usage

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

Arguments

  • urlOrRegExp String | Pattern Added in: v1.18#

    Expected URL string or RegExp.

  • options PageAssertions.HasURLOptions (optional)

    • setIgnoreCase boolean (optional) Added in: v1.44#

      Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified.

    • setTimeout double (optional) Added in: v1.18#

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

Returns


Properties

not()

Added in: v1.20 pageAssertions.not()

Makes the assertion check for the opposite condition. For example, this code tests that the page URL doesn't contain "error":

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

Usage

assertThat(page).not()

Returns