docs/src/api/class-pageassertions.md
The [PageAssertions] class provides assertion methods that can be used to make assertions about the [Page] state in the tests.
import { test, expect } from '@playwright/test';
test('navigates to login', async ({ page }) => {
// ...
await page.getByText('Sign in').click();
await expect(page).toHaveURL(/.*\/login/);
});
// ...
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"));
}
}
import re
from playwright.async_api import Page, expect
async def test_navigates_to_login_page(page: Page) -> None:
# ..
await page.get_by_text("Sign in").click()
await expect(page).to_have_url(re.compile(r".*/login"))
import re
from playwright.sync_api import Page, expect
def test_navigates_to_login_page(page: Page) -> None:
# ..
page.get_by_text("Sign in").click()
expect(page).to_have_url(re.compile(r".*/login"))
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
namespace PlaywrightTests;
[TestClass]
public class ExampleTests : PageTest
{
[TestMethod]
public async Task NavigateToLoginPage()
{
await Page.GetByRole(AriaRole.Button, new() { Name = "Sign In" }).ClickAsync();
await Expect(Page).ToHaveURLAsync(new Regex(".*/login"));
}
}
Makes the assertion check for the opposite condition.
Usage
For example, this code tests that the page URL doesn't contain "error":
await expect(page).not.toHaveURL('error');
assertThat(page).not().hasURL("error");
await Expect(Page).Not.ToHaveURLAsync("error");
The opposite of [method: PageAssertions.toHaveTitle].
titleOrRegExp <[string]|[RegExp]>Expected title or RegExp.
The opposite of [method: PageAssertions.toHaveURL].
urlOrRegExp <[string]|[RegExp]>Expected URL string or RegExp.
ignoreCase <[boolean]>Whether to perform case-insensitive match. [option: ignoreCase] option takes precedence over the corresponding regular expression flag if specified.
This function will wait until two consecutive page screenshots yield the same result, and then compare the last screenshot with the expectation.
Usage
await expect(page).toHaveScreenshot('image.png');
Note that screenshot assertions only work with Playwright test runner.
name <[string]|[Array]<[string]>>Snapshot name.
This function will wait until two consecutive page screenshots yield the same result, and then compare the last screenshot with the expectation.
Usage
await expect(page).toHaveScreenshot();
Note that screenshot assertions only work with Playwright test runner.
Ensures the page has the given title.
Usage
await expect(page).toHaveTitle(/.*checkout/);
assertThat(page).hasTitle("Playwright");
import re
from playwright.async_api import expect
# ...
await expect(page).to_have_title(re.compile(r".*checkout"))
import re
from playwright.sync_api import expect
# ...
expect(page).to_have_title(re.compile(r".*checkout"))
await Expect(Page).ToHaveTitleAsync("Playwright");
titleOrRegExp <[string]|[RegExp]>Expected title or RegExp.
Ensures the page is navigated to the given URL.
Usage
// Check for the page URL to be 'https://playwright.dev/docs/intro' (including query string)
await expect(page).toHaveURL('https://playwright.dev/docs/intro');
// Check for the page URL to contain 'doc', followed by an optional 's', followed by '/'
await expect(page).toHaveURL(/docs?\//);
// Check for the page URL to match the URL pattern
await expect(page).toHaveURL(new URLPattern({ pathname: '/docs/*' }));
// Check for the predicate to be satisfied
// For example: verify query strings
await expect(page).toHaveURL(url => {
const params = url.searchParams;
return params.has('search') && params.has('options') && params.get('id') === '5';
});
assertThat(page).hasURL(".com");
import re
from playwright.async_api import expect
# ...
await expect(page).to_have_url(re.compile(".*checkout"))
import re
from playwright.sync_api import expect
# ...
expect(page).to_have_url(re.compile(".*checkout"))
await Expect(Page).ToHaveURLAsync(new Regex(".*checkout"));
url <[string]|[RegExp]|[URLPattern]|[function]([URL]):[boolean]>Expected URL string, RegExp, or predicate receiving [URL] to match.
When [option: Browser.newContext.baseURL] is provided via the context options and the url argument is a string, the two values are merged via the new URL() constructor and used for the comparison against the current browser URL.
urlOrRegExp <[string]|[RegExp]>Expected URL string or RegExp.
ignoreCase <[boolean]>Whether to perform case-insensitive match. [option: ignoreCase] option takes precedence over the corresponding regular expression parameter if specified. A provided predicate ignores this flag.