PageAssertions
Added in: v1.17The 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
pageAssertions.hasTitle()
Added in: v1.20Ensures the page has the given title.
Usage
assertThat(page).hasTitle("Playwright");Arguments
pageAssertions.hasURL()
Added in: v1.20Ensures the page is navigated to the given URL.
Usage
assertThat(page).hasURL(".com");Arguments
urlOrRegExpstring | RegExpAdded in: v1.18Expected URL string or RegExp.
ignoreCaseboolean(optional) Added in: v1.44Whether to perform case-insensitive match.
ignoreCaseoption takes precedence over the corresponding regular expression parameter if specified. A provided predicate ignores this flag.timeoutfloat(optional) Added in: v1.18Time to retry the assertion for in milliseconds. Defaults to
5000.
pageAssertions.matchesAriaSnapshot()
Added in: v1.60Asserts that the page body matches the given accessibility snapshot.
Usage
page.navigate("https://demo.playwright.dev/todomvc/");
assertThat(page).matchesAriaSnapshot("""
- heading "todos"
- textbox "What needs to be done?"
""");Arguments
Properties
pageAssertions.not()
Added in: v1.20Makes the assertion check for the opposite condition.
Usage
For example, this code tests that the page URL doesn't contain "error":
assertThat(page).not().hasURL("error");Type
PageAssertions