2021-10-21 17:44:17 -07:00
# class: PageAssertions
2021-12-16 11:27:30 -08:00
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` ]:
2021-11-09 12:44:02 -08:00
2021-11-30 20:04:44 +01:00
```js
import { test, expect } from '@playwright/test ';
test('navigates to login', async ({ page }) => {
// ...
await page.click('#login ');
await expect(page).toHaveURL(/.*\/login/);
});
```
2021-11-09 12:44:02 -08:00
```java
...
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class TestPage {
...
@Test
void navigatesToLoginPage() {
...
page.click("#login ");
assertThat(page).hasURL(Pattern.compile(".*/login"));
}
}
```
2021-10-21 17:44:17 -07:00
2021-11-30 20:04:44 +01:00
```python async
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"))
```
```python sync
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"))
```
2022-02-21 14:01:53 +01:00
```csharp
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
using static Microsoft.Playwright.Assertions;
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"));
}
}
}
```
2021-11-18 00:46:30 +01:00
## method: PageAssertions.not
2022-02-21 14:01:53 +01:00
* langs: java, js, csharp
2021-11-18 00:46:30 +01:00
- returns: < [PageAssertions]>
Makes the assertion check for the opposite condition. For example, this code tests that the page URL doesn't contain `"error"` :
2021-11-30 20:04:44 +01:00
```js
await expect(page).not.toHaveURL('error');
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page).not().hasURL("error");
```
2022-02-21 14:01:53 +01:00
```csharp
await Expect(page).Not.ToHaveURL("error");
```
## async method: PageAssertions.NotToHaveTitle
2021-11-18 00:46:30 +01:00
* langs: python
The opposite of [`method: PageAssertions.toHaveTitle` ].
### param: PageAssertions.NotToHaveTitle.titleOrRegExp
- `titleOrRegExp` < [string]|[RegExp]>
Expected title or RegExp.
2022-03-02 12:43:16 -08:00
### option: PageAssertions.NotToHaveTitle.timeout = %%-js-assertions-timeout-%%
### option: PageAssertions.NotToHaveTitle.timeout = %%-csharp-java-python-assertions-timeout-%%
2021-11-18 00:46:30 +01:00
2022-02-21 14:01:53 +01:00
## async method: PageAssertions.NotToHaveURL
2021-11-18 00:46:30 +01:00
* langs: python
- alias-java: hasURL
The opposite of [`method: PageAssertions.toHaveURL` ].
### param: PageAssertions.NotToHaveURL.urlOrRegExp
- `urlOrRegExp` < [string]|[RegExp]>
Expected substring or RegExp.
2022-03-02 12:43:16 -08:00
### option: PageAssertions.NotToHaveURL.timeout = %%-js-assertions-timeout-%%
### option: PageAssertions.NotToHaveURL.timeout = %%-csharp-java-python-assertions-timeout-%%
2021-11-18 00:46:30 +01:00
2022-02-28 13:25:59 -07:00
## async method: PageAssertions.toHaveScreenshot
* 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.
```js
await expect(page).toHaveScreenshot();
```
2022-03-02 12:43:16 -08:00
### option: PageAssertions.toHaveScreenshot.timeout = %%-js-assertions-timeout-%%
### option: PageAssertions.toHaveScreenshot.timeout = %%-csharp-java-python-assertions-timeout-%%
2022-02-28 13:25:59 -07:00
### option: PageAssertions.toHaveScreenshot.disableAnimations = %%-screenshot-option-disable-animations-%%
### option: PageAssertions.toHaveScreenshot.omitBackground = %%-screenshot-option-omit-background-%%
### option: PageAssertions.toHaveScreenshot.fullPage = %%-screenshot-option-full-page-%%
### option: PageAssertions.toHaveScreenshot.clip = %%-screenshot-option-clip-%%
### option: PageAssertions.toHaveScreenshot.mask = %%-screenshot-option-mask-%%
### option: PageAssertions.toHaveScreenshot.pixelCount = %%-assertions-pixel-count-%%
### option: PageAssertions.toHaveScreenshot.pixelRatio = %%-assertions-pixel-ratio-%%
### option: PageAssertions.toHaveScreenshot.threshold = %%-assertions-threshold-%%
2022-02-21 14:01:53 +01:00
## async method: PageAssertions.toHaveTitle
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: hasTitle
2021-10-21 17:44:17 -07:00
2021-10-22 16:56:58 -07:00
Ensures the page has the given title.
2021-10-21 17:44:17 -07:00
2021-11-24 21:58:35 +01:00
```js
await expect(page).toHaveTitle(/.*checkout/);
```
2021-10-21 17:44:17 -07:00
```java
assertThat(page).hasTitle("Playwright");
```
2021-11-30 20:04:44 +01:00
```python async
import re
from playwright.async_api import expect
# ...
await expect(page).to_have_title(re.compile(r".*checkout"))
```
```python sync
import re
from playwright.sync_api import expect
# ...
expect(page).to_have_title(re.compile(r".*checkout"))
```
2022-02-21 14:01:53 +01:00
```csharp
await Expect(page).ToHaveTitle("Playwright");
```
2021-11-18 00:46:30 +01:00
### param: PageAssertions.toHaveTitle.titleOrRegExp
2021-10-21 17:44:17 -07:00
- `titleOrRegExp` < [string]|[RegExp]>
Expected title or RegExp.
2022-03-02 12:43:16 -08:00
### option: PageAssertions.toHaveTitle.timeout = %%-js-assertions-timeout-%%
### option: PageAssertions.toHaveTitle.timeout = %%-csharp-java-python-assertions-timeout-%%
2021-10-21 17:44:17 -07:00
2022-02-21 14:01:53 +01:00
## async method: PageAssertions.toHaveURL
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: hasURL
2021-10-21 17:44:17 -07:00
Ensures the page is navigated to the given URL.
2021-11-24 21:58:35 +01:00
```js
await expect(page).toHaveURL(/.*checkout/);
```
2021-10-21 17:44:17 -07:00
```java
2021-11-08 11:31:11 -08:00
assertThat(page).hasURL(".com");
2021-10-21 17:44:17 -07:00
```
2021-11-30 20:04:44 +01:00
```python async
import re
from playwright.async_api import expect
# ...
await expect(page).to_have_url(re.compile(".*checkout"))
```
```python sync
import re
from playwright.sync_api import expect
# ...
expect(page).to_have_url(re.compile(".*checkout"))
```
2022-02-21 14:01:53 +01:00
```csharp
await Expect(page).ToHaveURL(new Regex(".*checkout"));
```
2021-11-18 00:46:30 +01:00
### param: PageAssertions.toHaveURL.urlOrRegExp
2021-10-21 17:44:17 -07:00
- `urlOrRegExp` < [string]|[RegExp]>
Expected substring or RegExp.
2022-03-02 12:43:16 -08:00
### option: PageAssertions.toHaveURL.timeout = %%-js-assertions-timeout-%%
### option: PageAssertions.toHaveURL.timeout = %%-csharp-java-python-assertions-timeout-%%