The [PageAssertions] class provides assertion methods that can be used to make assertions about the [Page] state in the tests. A new instance of [PageAssertions] is created by calling [method: PlaywrightAssertions.expectPage
]:
import { test, expect } from '@playwright/test';
test('navigates to login', async ({ page }) => {
// ...
await page.click('#login');
await expect(page).toHaveURL(/.*\/login/);
});
...
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class TestPage {
...
@Test
void navigatesToLoginPage() {
...
page.click("#login");
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.click("#login")
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.click("#login")
expect(page).to_have_url(re.compile(r".*/login"))
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
public class ExampleTests : PageTest
{
[Test]
public async Task NavigatetoLoginPage()
{
// ..
await Page.ClickAsync("#login");
await Expect(Page.Locator("div#foobar")).ToHaveURL(new Regex(".*/login"));
}
}
- langs: java, js, csharp
- returns: <[PageAssertions]>
Makes the assertion check for the opposite condition. 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.ToHaveURL("error");
- langs: python
The opposite of [method: PageAssertions.toHaveTitle
].
titleOrRegExp
<[string]|[RegExp]>
Expected title or RegExp.
- langs: python
- alias-java: hasURL
The opposite of [method: PageAssertions.toHaveURL
].
urlOrRegExp
<[string]|[RegExp]>
Expected substring or RegExp.
- langs: js
Ensures that the page resolves to a given screenshot. This function will re-take screenshots until it matches with the saved expectation.
If there's no expectation yet, it will wait until two consecutive screenshots yield the same result, and save the last one as an expectation.
await expect(page).toHaveScreenshot('image.png');
name
<[string]|[Array]<[string]>>
Snapshot name.
option: PageAssertions.toHaveScreenshot#1.animations = %%-screenshot-option-animations-default-disabled-%%
- langs: js
Ensures that the page resolves to a given screenshot. This function will re-take screenshots until it matches with the saved expectation.
If there's no expectation yet, it will wait until two consecutive screenshots yield the same result, and save the last one as an expectation.
await expect(page).toHaveScreenshot();
option: PageAssertions.toHaveScreenshot#2.animations = %%-screenshot-option-animations-default-disabled-%%
- langs:
- alias-java: hasTitle
Ensures the page has the given title.
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).ToHaveTitle("Playwright");
titleOrRegExp
<[string]|[RegExp]>
Expected title or RegExp.
- langs:
- alias-java: hasURL
Ensures the page is navigated to the given URL.
await expect(page).toHaveURL(/.*checkout/);
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).ToHaveURL(new Regex(".*checkout"));
urlOrRegExp
<[string]|[RegExp]>
Expected substring or RegExp.