2021-10-21 17:44:17 -07:00
|
|
|
# class: PlaywrightAssertions
|
2021-11-24 21:58:35 +01:00
|
|
|
* langs: java, python, js
|
2021-10-21 17:44:17 -07:00
|
|
|
|
|
|
|
The [PlaywrightAssertions] class provides convenience methods for creating assertions that will wait until the expected condition is met.
|
|
|
|
|
|
|
|
Consider the following example:
|
|
|
|
|
2021-11-24 21:58:35 +01:00
|
|
|
```js
|
|
|
|
import { test, expect } from '@playwright/test';
|
|
|
|
|
|
|
|
test('status becomes submitted', async ({ page }) => {
|
|
|
|
// ...
|
|
|
|
await page.click('#submit-button')
|
|
|
|
await expect(page.locator('.status')).toHaveText('Submitted');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
```python async
|
|
|
|
from playwright.async_api import Page, expect
|
|
|
|
|
2021-11-30 20:04:44 +01:00
|
|
|
async def test_status_becomes_submitted(page: Page) -> None:
|
|
|
|
# ..
|
|
|
|
await page.click("#submit-button")
|
|
|
|
await expect(page.locator(".status")).to_have_text("Submitted")
|
2021-11-24 21:58:35 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
from playwright.sync_api import Page, expect
|
|
|
|
|
2021-11-30 20:04:44 +01:00
|
|
|
def test_status_becomes_submitted(page: Page) -> None:
|
|
|
|
# ..
|
|
|
|
page.click("#submit-button")
|
|
|
|
expect(page.locator(".status")).to_have_text("Submitted")
|
2021-11-24 21:58:35 +01:00
|
|
|
```
|
|
|
|
|
2021-10-21 17:44:17 -07:00
|
|
|
```java
|
2021-10-29 08:33:07 -07:00
|
|
|
...
|
|
|
|
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
|
|
|
|
|
|
|
public class TestExample {
|
|
|
|
...
|
|
|
|
@Test
|
|
|
|
void statusBecomesSubmitted() {
|
|
|
|
...
|
|
|
|
page.click("#submit-button");
|
|
|
|
assertThat(page.locator(".status")).hasText("Submitted");
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 17:44:17 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
Playwright will be re-testing the node with the selector `.status` until fetched Node has the `"Submitted"`
|
|
|
|
text. It will be re-fetching the node and checking it over and over, until the condition is met or until the timeout is
|
|
|
|
reached. You can pass this timeout as an option.
|
|
|
|
|
|
|
|
By default, the timeout for assertions is set to 5 seconds.
|
|
|
|
|
2021-12-16 11:27:30 -08:00
|
|
|
## method: PlaywrightAssertions.expectAPIResponse
|
2022-01-25 18:53:49 +01:00
|
|
|
* langs: js, java, python
|
2021-12-16 11:27:30 -08:00
|
|
|
- alias-java: assertThat
|
|
|
|
- alias-python: expect
|
|
|
|
- alias-js: expect
|
|
|
|
- returns: <[APIResponseAssertions]>
|
|
|
|
|
|
|
|
Creates a [APIResponseAssertions] object for the given [APIResponse].
|
|
|
|
|
|
|
|
```java
|
|
|
|
PlaywrightAssertions.assertThat(response).isOK();
|
|
|
|
```
|
|
|
|
|
|
|
|
### param: PlaywrightAssertions.expectAPIResponse.response
|
|
|
|
- `response` <[APIResponse]>
|
|
|
|
|
|
|
|
[APIResponse] object to use for assertions.
|
|
|
|
|
2021-11-24 21:58:35 +01:00
|
|
|
## method: PlaywrightAssertions.expectLocator
|
|
|
|
* langs: java, python, js
|
2021-10-25 12:03:24 -07:00
|
|
|
- alias-java: assertThat
|
2021-11-18 00:46:30 +01:00
|
|
|
- alias-python: expect
|
2021-11-24 21:58:35 +01:00
|
|
|
- alias-js: expect
|
2021-10-22 16:56:58 -07:00
|
|
|
- returns: <[LocatorAssertions]>
|
|
|
|
|
|
|
|
Creates a [LocatorAssertions] object for the given [Locator].
|
|
|
|
|
|
|
|
```java
|
|
|
|
PlaywrightAssertions.assertThat(locator).isVisible();
|
|
|
|
```
|
|
|
|
|
2021-11-24 21:58:35 +01:00
|
|
|
### param: PlaywrightAssertions.expectLocator.locator
|
2021-10-22 16:56:58 -07:00
|
|
|
- `locator` <[Locator]>
|
|
|
|
|
|
|
|
[Locator] object to use for assertions.
|
|
|
|
|
2021-11-24 21:58:35 +01:00
|
|
|
## method: PlaywrightAssertions.expectPage
|
|
|
|
* langs: java, python, js
|
2021-10-25 12:03:24 -07:00
|
|
|
- alias-java: assertThat
|
2021-11-18 00:46:30 +01:00
|
|
|
- alias-python: expect
|
2021-11-24 21:58:35 +01:00
|
|
|
- alias-js: expect
|
2021-10-21 17:44:17 -07:00
|
|
|
- returns: <[PageAssertions]>
|
|
|
|
|
|
|
|
Creates a [PageAssertions] object for the given [Page].
|
|
|
|
|
|
|
|
```java
|
|
|
|
PlaywrightAssertions.assertThat(page).hasTitle("News");
|
|
|
|
```
|
|
|
|
|
2021-11-24 21:58:35 +01:00
|
|
|
### param: PlaywrightAssertions.expectPage.page
|
2021-10-21 17:44:17 -07:00
|
|
|
- `page` <[Page]>
|
|
|
|
|
|
|
|
[Page] object to use for assertions.
|