playwright/docs/src/api/class-playwrightassertions.md
2021-11-30 20:04:44 +01:00

2.6 KiB

class: PlaywrightAssertions

  • langs: java, python, js

The [PlaywrightAssertions] class provides convenience methods for creating assertions that will wait until the expected condition is met.

Consider the following example:

import { test, expect } from '@playwright/test';

test('status becomes submitted', async ({ page }) => {
  // ...
  await page.click('#submit-button')
  await expect(page.locator('.status')).toHaveText('Submitted');
});
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")
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")
...
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

public class TestExample {
  ...
  @Test
  void statusBecomesSubmitted() {
    ...
    page.click("#submit-button");
    assertThat(page.locator(".status")).hasText("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.

langs: javaTo use Playwright assertions add the following dependency into the pom.xml of your Maven project:

<dependency>
  <groupId>com.microsoft.playwright</groupId>
  <artifactId>assertions</artifactId>
  <version>1.17.0</version>
</dependency>

method: PlaywrightAssertions.expectLocator

  • langs: java, python, js
    • alias-java: assertThat
    • alias-python: expect
    • alias-js: expect
  • returns: <[LocatorAssertions]>

Creates a [LocatorAssertions] object for the given [Locator].

PlaywrightAssertions.assertThat(locator).isVisible();

param: PlaywrightAssertions.expectLocator.locator

  • locator <[Locator]>

[Locator] object to use for assertions.

method: PlaywrightAssertions.expectPage

  • langs: java, python, js
    • alias-java: assertThat
    • alias-python: expect
    • alias-js: expect
  • returns: <[PageAssertions]>

Creates a [PageAssertions] object for the given [Page].

PlaywrightAssertions.assertThat(page).hasTitle("News");

param: PlaywrightAssertions.expectPage.page

  • page <[Page]>

[Page] object to use for assertions.