2021-10-21 17:44:17 -07:00
# class: PlaywrightAssertions
2022-07-05 16:24:50 -08:00
* since: v1.17
2021-10-21 17:44:17 -07:00
2022-02-25 22:58:41 +01:00
Playwright gives you Web-First Assertions with convenience methods for creating assertions that will wait and retry until the expected condition is met.
2021-10-21 17:44:17 -07:00
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
```
2022-02-21 14:01:53 +01:00
```csharp
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
2022-04-19 21:23:26 +03:00
namespace Playwright.TestingHarnessTest.NUnit;
public class ExampleTests : PageTest
2022-02-21 14:01:53 +01:00
{
2022-04-19 21:23:26 +03:00
[Test]
public async Task StatusBecomesSubmitted()
2022-02-21 14:01:53 +01:00
{
2022-04-19 21:23:26 +03:00
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
2022-02-21 14:01:53 +01:00
}
}
```
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-07-05 16:24:50 -08:00
* since: v1.18
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
2022-02-21 14:01:53 +01:00
- alias-csharp: Expect
2021-12-16 11:27:30 -08:00
- returns: < [APIResponseAssertions]>
Creates a [APIResponseAssertions] object for the given [APIResponse].
```java
PlaywrightAssertions.assertThat(response).isOK();
```
### param: PlaywrightAssertions.expectAPIResponse.response
2022-07-05 16:24:50 -08:00
* since: v1.18
2021-12-16 11:27:30 -08:00
- `response` < [APIResponse]>
[APIResponse] object to use for assertions.
2021-11-24 21:58:35 +01:00
## method: PlaywrightAssertions.expectLocator
2022-07-05 16:24:50 -08:00
* since: v1.18
2022-02-21 14:01:53 +01:00
* langs:
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
2022-02-21 14:01:53 +01:00
- alias-csharp: Expect
2021-10-22 16:56:58 -07:00
- returns: < [LocatorAssertions]>
Creates a [LocatorAssertions] object for the given [Locator].
```java
PlaywrightAssertions.assertThat(locator).isVisible();
```
2022-02-21 14:01:53 +01:00
```csharp
await Expect(locator).ToBeVisibleAsync();
```
2021-11-24 21:58:35 +01:00
### param: PlaywrightAssertions.expectLocator.locator
2022-07-05 16:24:50 -08:00
* since: v1.18
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
2022-07-05 16:24:50 -08:00
* since: v1.18
2022-02-21 14:01:53 +01:00
* langs:
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
2022-02-21 14:01:53 +01:00
- alias-csharp: 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");
```
2022-02-21 14:01:53 +01:00
```csharp
await Expect(page).ToHaveTitleAsync("News");
```
2021-11-24 21:58:35 +01:00
### param: PlaywrightAssertions.expectPage.page
2022-07-05 16:24:50 -08:00
* since: v1.18
2021-10-21 17:44:17 -07:00
- `page` < [Page]>
[Page] object to use for assertions.