2021-10-21 17:44:17 -07:00
|
|
|
# class: PageAssertions
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.17
|
2021-10-21 17:44:17 -07:00
|
|
|
|
2023-02-16 11:48:38 -08:00
|
|
|
The [PageAssertions] class provides assertion methods that can be used to make assertions about the [Page] state in the tests.
|
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 }) => {
|
|
|
|
// ...
|
2022-10-03 17:02:46 -07:00
|
|
|
await page.getByText('Sign in').click();
|
2021-11-30 20:04:44 +01:00
|
|
|
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() {
|
|
|
|
...
|
2022-10-03 17:02:46 -07:00
|
|
|
page.getByText("Sign in").click();
|
2021-11-09 12:44:02 -08:00
|
|
|
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:
|
|
|
|
# ..
|
2022-10-03 17:02:46 -07:00
|
|
|
await page.get_by_text("Sign in").click()
|
2021-11-30 20:04:44 +01:00
|
|
|
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:
|
|
|
|
# ..
|
2022-10-03 17:02:46 -07:00
|
|
|
page.get_by_text("Sign in").click()
|
2021-11-30 20:04:44 +01:00
|
|
|
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;
|
|
|
|
|
2022-04-19 21:23:26 +03:00
|
|
|
namespace PlaywrightTests;
|
|
|
|
|
2022-09-08 01:44:58 +02:00
|
|
|
[TestFixture]
|
2022-04-19 21:23:26 +03:00
|
|
|
public class ExampleTests : PageTest
|
2022-02-21 14:01:53 +01:00
|
|
|
{
|
2022-09-14 22:44:38 +02:00
|
|
|
[Test]
|
2022-04-19 21:23:26 +03:00
|
|
|
public async Task NavigatetoLoginPage()
|
2022-02-21 14:01:53 +01:00
|
|
|
{
|
2022-04-19 21:23:26 +03:00
|
|
|
// ..
|
2022-10-03 17:02:46 -07:00
|
|
|
await Page.GetByText("Sing in").ClickAsync();
|
2022-04-19 21:23:26 +03:00
|
|
|
await Expect(Page.Locator("div#foobar")).ToHaveURL(new Regex(".*/login"));
|
2022-02-21 14:01:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2021-11-18 00:46:30 +01:00
|
|
|
|
2022-03-02 15:03:33 -08:00
|
|
|
## property: PageAssertions.not
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.20
|
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
|
2023-11-07 09:28:18 +01:00
|
|
|
await Expect(Page).Not.ToHaveURL("error");
|
2022-02-21 14:01:53 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
## async method: PageAssertions.NotToHaveTitle
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.20
|
2021-11-18 00:46:30 +01:00
|
|
|
* langs: python
|
|
|
|
|
|
|
|
The opposite of [`method: PageAssertions.toHaveTitle`].
|
|
|
|
|
|
|
|
### param: PageAssertions.NotToHaveTitle.titleOrRegExp
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2021-11-18 00:46:30 +01:00
|
|
|
- `titleOrRegExp` <[string]|[RegExp]>
|
|
|
|
|
|
|
|
Expected title or RegExp.
|
|
|
|
|
2022-03-02 12:43:16 -08:00
|
|
|
### option: PageAssertions.NotToHaveTitle.timeout = %%-csharp-java-python-assertions-timeout-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2021-11-18 00:46:30 +01:00
|
|
|
|
2022-02-21 14:01:53 +01:00
|
|
|
## async method: PageAssertions.NotToHaveURL
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.20
|
2021-11-18 00:46:30 +01:00
|
|
|
* langs: python
|
|
|
|
- alias-java: hasURL
|
|
|
|
|
|
|
|
The opposite of [`method: PageAssertions.toHaveURL`].
|
|
|
|
|
|
|
|
### param: PageAssertions.NotToHaveURL.urlOrRegExp
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2021-11-18 00:46:30 +01:00
|
|
|
- `urlOrRegExp` <[string]|[RegExp]>
|
|
|
|
|
2022-07-15 11:09:20 -07:00
|
|
|
Expected URL string or RegExp.
|
2021-11-18 00:46:30 +01:00
|
|
|
|
2022-03-02 12:43:16 -08:00
|
|
|
### option: PageAssertions.NotToHaveURL.timeout = %%-csharp-java-python-assertions-timeout-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2021-11-18 00:46:30 +01:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
## async method: PageAssertions.toHaveScreenshot#1
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-04-14 13:22:42 -07:00
|
|
|
* langs: js
|
|
|
|
|
2022-05-24 11:54:32 -06:00
|
|
|
This function will wait until two consecutive page screenshots
|
|
|
|
yield the same result, and then compare the last screenshot with the expectation.
|
2022-04-14 13:22:42 -07:00
|
|
|
|
2022-11-21 10:40:21 -08:00
|
|
|
**Usage**
|
|
|
|
|
2022-04-14 13:22:42 -07:00
|
|
|
```js
|
2022-05-16 06:53:46 -08:00
|
|
|
await expect(page).toHaveScreenshot('image.png');
|
2022-04-14 13:22:42 -07:00
|
|
|
```
|
|
|
|
|
2022-11-28 10:32:48 -08:00
|
|
|
Note that screenshot assertions only work with Playwright test runner.
|
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### param: PageAssertions.toHaveScreenshot#1.name
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-05-16 06:53:46 -08:00
|
|
|
- `name` <[string]|[Array]<[string]>>
|
2022-04-14 13:22:42 -07:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
Snapshot name.
|
2022-04-14 13:22:42 -07:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.timeout = %%-js-assertions-timeout-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 22:26:23 +03:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.animations = %%-screenshot-option-animations-default-disabled-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.caret = %%-screenshot-option-caret-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.clip = %%-screenshot-option-clip-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.fullPage = %%-screenshot-option-full-page-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.mask = %%-screenshot-option-mask-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2023-06-06 17:15:55 -07:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.maskColor = %%-screenshot-option-mask-color-%%
|
|
|
|
* since: v1.35
|
|
|
|
|
2023-11-30 17:42:45 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.style = %%-screenshot-option-style-%%
|
|
|
|
* since: v1.41
|
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.omitBackground = %%-screenshot-option-omit-background-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 22:26:23 +03:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.scale = %%-screenshot-option-scale-default-css-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.maxDiffPixels = %%-assertions-max-diff-pixels-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.maxDiffPixelRatio = %%-assertions-max-diff-pixel-ratio-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#1.threshold = %%-assertions-threshold-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-04-14 13:22:42 -07:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
## async method: PageAssertions.toHaveScreenshot#2
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-05-16 06:53:46 -08:00
|
|
|
* langs: js
|
2022-04-14 13:22:42 -07:00
|
|
|
|
2022-05-24 11:54:32 -06:00
|
|
|
This function will wait until two consecutive page screenshots
|
|
|
|
yield the same result, and then compare the last screenshot with the expectation.
|
2022-04-14 13:22:42 -07:00
|
|
|
|
2022-11-21 10:40:21 -08:00
|
|
|
**Usage**
|
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
```js
|
|
|
|
await expect(page).toHaveScreenshot();
|
|
|
|
```
|
2022-04-14 13:22:42 -07:00
|
|
|
|
2022-11-28 10:32:48 -08:00
|
|
|
Note that screenshot assertions only work with Playwright test runner.
|
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.timeout = %%-js-assertions-timeout-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 22:26:23 +03:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.animations = %%-screenshot-option-animations-default-disabled-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.caret = %%-screenshot-option-caret-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.clip = %%-screenshot-option-clip-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.fullPage = %%-screenshot-option-full-page-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.mask = %%-screenshot-option-mask-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2023-06-06 17:15:55 -07:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.maskColor = %%-screenshot-option-mask-color-%%
|
|
|
|
* since: v1.35
|
|
|
|
|
2023-11-30 17:42:45 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.style = %%-screenshot-option-style-%%
|
|
|
|
* since: v1.41
|
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.omitBackground = %%-screenshot-option-omit-background-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 22:26:23 +03:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.scale = %%-screenshot-option-scale-default-css-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.maxDiffPixels = %%-assertions-max-diff-pixels-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.maxDiffPixelRatio = %%-assertions-max-diff-pixel-ratio-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-05-16 06:53:46 -08:00
|
|
|
### option: PageAssertions.toHaveScreenshot#2.threshold = %%-assertions-threshold-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.23
|
2022-04-14 13:22:42 -07:00
|
|
|
|
2022-02-21 14:01:53 +01:00
|
|
|
## async method: PageAssertions.toHaveTitle
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.20
|
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
|
|
|
|
2022-11-21 10:40:21 -08:00
|
|
|
**Usage**
|
|
|
|
|
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
|
2023-11-07 09:28:18 +01:00
|
|
|
await Expect(Page).ToHaveTitle("Playwright");
|
2022-02-21 14:01:53 +01:00
|
|
|
```
|
|
|
|
|
2021-11-18 00:46:30 +01:00
|
|
|
### param: PageAssertions.toHaveTitle.titleOrRegExp
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
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-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-03-02 12:43:16 -08:00
|
|
|
### option: PageAssertions.toHaveTitle.timeout = %%-csharp-java-python-assertions-timeout-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2021-10-21 17:44:17 -07:00
|
|
|
|
2022-02-21 14:01:53 +01:00
|
|
|
## async method: PageAssertions.toHaveURL
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.20
|
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.
|
|
|
|
|
2022-11-21 10:40:21 -08:00
|
|
|
**Usage**
|
|
|
|
|
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
|
2023-11-07 09:28:18 +01:00
|
|
|
await Expect(Page).ToHaveURL(new Regex(".*checkout"));
|
2022-02-21 14:01:53 +01:00
|
|
|
```
|
|
|
|
|
2021-11-18 00:46:30 +01:00
|
|
|
### param: PageAssertions.toHaveURL.urlOrRegExp
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2021-10-21 17:44:17 -07:00
|
|
|
- `urlOrRegExp` <[string]|[RegExp]>
|
|
|
|
|
2022-07-15 11:09:20 -07:00
|
|
|
Expected URL string or RegExp.
|
2021-10-21 17:44:17 -07:00
|
|
|
|
2022-03-02 12:43:16 -08:00
|
|
|
### option: PageAssertions.toHaveURL.timeout = %%-js-assertions-timeout-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2022-11-21 09:30:32 -08:00
|
|
|
|
2022-03-02 12:43:16 -08:00
|
|
|
### option: PageAssertions.toHaveURL.timeout = %%-csharp-java-python-assertions-timeout-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|