playwright/docs/src/api/class-playwrightassertions.md

144 lines
3.4 KiB
Markdown
Raw Normal View History

# class: PlaywrightAssertions
2022-07-05 16:24:50 -08:00
* since: v1.17
Playwright gives you Web-First Assertions with convenience methods for creating assertions that will wait and retry until the expected condition is met.
Consider the following example:
```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
async def test_status_becomes_submitted(page: Page) -> None:
# ..
await page.click("#submit-button")
await expect(page.locator(".status")).to_have_text("Submitted")
```
```python sync
from playwright.sync_api import Page, expect
def test_status_becomes_submitted(page: Page) -> None:
# ..
page.click("#submit-button")
expect(page.locator(".status")).to_have_text("Submitted")
```
```java
...
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class TestExample {
...
@Test
void statusBecomesSubmitted() {
...
page.click("#submit-button");
assertThat(page.locator(".status")).hasText("Submitted");
}
}
```
```csharp
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace Playwright.TestingHarnessTest.NUnit;
public class ExampleTests : PageTest
{
[Test]
public async Task StatusBecomesSubmitted()
{
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
}
```
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.
## method: PlaywrightAssertions.expectAPIResponse
2022-07-05 16:24:50 -08:00
* since: v1.18
* langs: js, java, python
- alias-java: assertThat
- alias-python: expect
- alias-js: expect
- alias-csharp: Expect
- 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
- `response` <[APIResponse]>
[APIResponse] object to use for assertions.
## method: PlaywrightAssertions.expectLocator
2022-07-05 16:24:50 -08:00
* since: v1.18
* langs:
- alias-java: assertThat
- alias-python: expect
- alias-js: expect
- alias-csharp: Expect
- returns: <[LocatorAssertions]>
Creates a [LocatorAssertions] object for the given [Locator].
```java
PlaywrightAssertions.assertThat(locator).isVisible();
```
```csharp
await Expect(locator).ToBeVisibleAsync();
```
### param: PlaywrightAssertions.expectLocator.locator
2022-07-05 16:24:50 -08:00
* since: v1.18
- `locator` <[Locator]>
[Locator] object to use for assertions.
## method: PlaywrightAssertions.expectPage
2022-07-05 16:24:50 -08:00
* since: v1.18
* langs:
- alias-java: assertThat
- alias-python: expect
- alias-js: expect
- alias-csharp: Expect
- returns: <[PageAssertions]>
Creates a [PageAssertions] object for the given [Page].
```java
PlaywrightAssertions.assertThat(page).hasTitle("News");
```
```csharp
await Expect(page).ToHaveTitleAsync("News");
```
### param: PlaywrightAssertions.expectPage.page
2022-07-05 16:24:50 -08:00
* since: v1.18
- `page` <[Page]>
[Page] object to use for assertions.