Skip to main content

Assertions

Introduction

Playwright includes web-specific assertions that automatically retry until the expected condition is met. Consider the following example:

assertThat(page.getByTestId("status")).hasText("Submitted");

Playwright will re-test the element with the test ID of status until it has the "Submitted" text. It will re-fetch the element and check it repeatedly until the condition is met or the timeout is reached.

By default, the timeout for assertions is 5 seconds.

Auto-retrying assertions

The following assertions will retry until the assertion passes or the assertion timeout is reached.

AssertionDescription
assertThat(locator).isAttached()Element is attached
assertThat(locator).isChecked()Checkbox is checked
assertThat(locator).isDisabled()Element is disabled
assertThat(locator).isEditable()Element is editable
assertThat(locator).isEmpty()Container is empty
assertThat(locator).isEnabled()Element is enabled
assertThat(locator).isFocused()Element is focused
assertThat(locator).isHidden()Element is not visible
assertThat(locator).isInViewport()Element intersects viewport
assertThat(locator).isVisible()Element is visible
assertThat(locator).containsClass()Element has specified CSS classes
assertThat(locator).containsText()Element contains text
assertThat(locator).hasAccessibleDescription()Element has a matching accessible description
assertThat(locator).hasAccessibleName()Element has a matching accessible name
assertThat(locator).hasAttribute()Element has a DOM attribute
assertThat(locator).hasClass()Element has a class property
assertThat(locator).hasCount()List has exact number of children
assertThat(locator).hasCSS()Element has CSS property
assertThat(locator).hasId()Element has an ID
assertThat(locator).hasJSProperty()Element has a JavaScript property
assertThat(locator).hasRole()Element has a specific ARIA role
assertThat(locator).hasText()Element matches text
assertThat(locator).hasValue()Input has a value
assertThat(locator).hasValues()Select has options selected
assertThat(locator).matchesAriaSnapshot()Element matches provided Aria snapshot
assertThat(page).hasTitle()Page has a title
assertThat(page).hasURL()Page has a URL
assertThat(response).isOK()Response has an OK status

Setting a custom timeout

You can specify a custom timeout for assertions either globally or per assertion. The default timeout is 5 seconds.

Global timeout

import com.microsoft.playwright.assertions.PlaywrightAssertions;

PlaywrightAssertions.setDefaultAssertionTimeout(10_000);

Per assertion timeout

import com.microsoft.playwright.assertions.LocatorAssertions;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

assertThat(page.getByText("Name")).isVisible(
new LocatorAssertions.IsVisibleOptions().setTimeout(10_000));