Skip to main content

Assertions

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:

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

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

assertThat(locator).not()

Added in: v1.20

Makes the assertion check for the opposite condition. For example, this code tests that the Locator doesn't contain text "error":

assertThat(locator).not().containsText("error");

assertThat(locator).isChecked([options])

Added in: v1.20
  • options <LocatorAssertions.IsCheckedOptions>
    • setChecked <boolean> Added in: v1.18#
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to a checked input.

assertThat(page.getByLabel("Subscribe to newsletter")).isChecked();

assertThat(locator).isDisabled([options])

Added in: v1.20
  • options <LocatorAssertions.IsDisabledOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to a disabled element. Element is disabled if it has "disabled" attribute or is disabled via 'aria-disabled'. Note that only native control elements such as HTML button, input, select, textarea, option, optgroup can be disabled by setting "disabled" attribute. "disabled" attribute on other elements is ignored by the browser.

assertThat(page.locator("button.submit")).isDisabled();

assertThat(locator).isEditable([options])

Added in: v1.20
  • options <LocatorAssertions.IsEditableOptions>
    • setEditable <boolean> Added in: v1.26#
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an editable element.

assertThat(page.getByRole("textbox")).isEditable();

assertThat(locator).isEmpty([options])

Added in: v1.20
  • options <LocatorAssertions.IsEmptyOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an empty editable element or to a DOM node that has no text.

assertThat(page.locator("div.warning")).isEmpty();

assertThat(locator).isEnabled([options])

Added in: v1.20
  • options <LocatorAssertions.IsEnabledOptions>
    • setEnabled <boolean> Added in: v1.26#
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an enabled element.

assertThat(page.locator("button.submit")).isEnabled();

assertThat(locator).isFocused([options])

Added in: v1.20
  • options <LocatorAssertions.IsFocusedOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to a focused DOM node.

assertThat(page.getByRole("textbox")).isFocused();

assertThat(locator).isHidden([options])

Added in: v1.20
  • options <LocatorAssertions.IsHiddenOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures that Locator either does not resolve to any DOM node, or resolves to a non-visible one.

assertThat(page.locator(".my-element")).isHidden();

assertThat(locator).isVisible([options])

Added in: v1.20
  • options <LocatorAssertions.IsVisibleOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
    • setVisible <boolean> Added in: v1.26#
  • returns: <void>#

Ensures that Locator points to an attached and visible DOM node.

assertThat(page.locator(".my-element")).isVisible();

assertThat(locator).containsText(expected[, options])

Added in: v1.20
  • expected <String|Pattern|String[]|Pattern[]> Expected substring or RegExp or a list of those. Added in: v1.18#
  • options <LocatorAssertions.ContainsTextOptions>
    • setIgnoreCase <boolean> Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified. Added in: v1.23#
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
    • setUseInnerText <boolean> Whether to use element.innerText instead of element.textContent when retrieving DOM node text. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an element that contains the given text. You can use regular expressions for the value as well.

assertThat(page.locator(".title")).containsText("substring");

If you pass an array as an expected value, the expectations are:

  1. Locator resolves to a list of elements.
  2. Elements from a subset of this list contain text from the expected array, respectively.
  3. The matching subset of elements has the same order as the expected array.
  4. Each text value from the expected array is matched by some element from the list.

For example, consider the following list:

<ul>
<li>Item Text 1</li>
<li>Item Text 2</li>
<li>Item Text 3</li>
</ul>

Let's see how we can use the assertion:

// ✓ Contains the right items in the right order
assertThat(page.locator("ul > li")).containsText(new String[] {"Text 1", "Text 3", "Text 4"});

// ✖ Wrong order
assertThat(page.locator("ul > li")).containsText(new String[] {"Text 3", "Text 2"});

// ✖ No item contains this text
assertThat(page.locator("ul > li")).containsText(new String[] {"Some 33"});

// ✖ Locator points to the outer list element, not to the list items
assertThat(page.locator("ul")).containsText(new String[] {"Text 3"});

assertThat(locator).hasAttribute(name, value[, options])

Added in: v1.20
  • name <String> Attribute name. Added in: v1.18#
  • value <String|Pattern> Expected attribute value. Added in: v1.18#
  • options <LocatorAssertions.HasAttributeOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an element with given attribute.

assertThat(page.locator("input")).hasAttribute("type", "text");

assertThat(locator).hasClass(expected[, options])

Added in: v1.20
  • expected <String|Pattern|String[]|Pattern[]> Expected class or RegExp or a list of those. Added in: v1.18#
  • options <LocatorAssertions.HasClassOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an element with given CSS classes. This needs to be a full match or using a relaxed regular expression.

<div class='selected row' id='component'></div>
assertThat(page.locator("#component")).hasClass(Pattern.compile("selected"));
assertThat(page.locator("#component")).hasClass("selected row");

Note that if array is passed as an expected value, entire lists of elements can be asserted:

assertThat(page.locator("list > .component")).hasClass(new String[] {"component", "component selected", "component"});

assertThat(locator).hasCount(count[, options])

Added in: v1.20
  • count <int> Expected count. Added in: v1.18#
  • options <LocatorAssertions.HasCountOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator resolves to an exact number of DOM nodes.

assertThat(page.locator("list > .component")).hasCount(3);

assertThat(locator).hasCSS(name, value[, options])

Added in: v1.20
  • name <String> CSS property name. Added in: v1.18#
  • value <String|Pattern> CSS property value. Added in: v1.18#
  • options <LocatorAssertions.HasCSSOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator resolves to an element with the given computed CSS style.

assertThat(page.getByRole("button")).hasCSS("display", "flex");

assertThat(locator).hasId(id[, options])

Added in: v1.20
  • id <String|Pattern> Element id. Added in: v1.18#
  • options <LocatorAssertions.HasIdOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an element with the given DOM Node ID.

assertThat(page.getByRole("textbox")).hasId("lastname");

assertThat(locator).hasJSProperty(name, value[, options])

Added in: v1.20
  • name <String> Property name. Added in: v1.18#
  • value <Object> Property value. Added in: v1.18#
  • options <LocatorAssertions.HasJSPropertyOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an element with given JavaScript property. Note that this property can be of a primitive type as well as a plain serializable JavaScript object.

assertThat(page.locator("input")).hasJSProperty("loaded", true);

assertThat(locator).hasText(expected[, options])

Added in: v1.20
  • expected <String|Pattern|String[]|Pattern[]> Expected substring or RegExp or a list of those. Added in: v1.18#
  • options <LocatorAssertions.HasTextOptions>
    • setIgnoreCase <boolean> Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified. Added in: v1.23#
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
    • setUseInnerText <boolean> Whether to use element.innerText instead of element.textContent when retrieving DOM node text. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an element with the given text. You can use regular expressions for the value as well.

assertThat(page.locator(".title")).hasText("Welcome, Test User");
assertThat(page.locator(".title")).hasText(Pattern.compile("Welcome, .*"));

If you pass an array as an expected value, the expectations are:

  1. Locator resolves to a list of elements.
  2. The number of elements equals the number of expected values in the array.
  3. Elements from the list have text matching expected array values, one by one, in order.

For example, consider the following list:

<ul>
<li>Text 1</li>
<li>Text 2</li>
<li>Text 3</li>
</ul>

Let's see how we can use the assertion:

// ✓ Has the right items in the right order
assertThat(page.locator("ul > li")).hasText(new String[] {"Text 1", "Text 2", "Text 3"});

// ✖ Wrong order
assertThat(page.locator("ul > li")).hasText(new String[] {"Text 3", "Text 2", "Text 1"});

// ✖ Last item does not match
assertThat(page.locator("ul > li")).hasText(new String[] {"Text 1", "Text 2", "Text"});

// ✖ Locator points to the outer list element, not to the list items
assertThat(page.locator("ul")).hasText(new String[] {"Text 1", "Text 2", "Text 3"});

assertThat(locator).hasValue(value[, options])

Added in: v1.20
  • value <String|Pattern> Expected value. Added in: v1.18#
  • options <LocatorAssertions.HasValueOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an element with the given input value. You can use regular expressions for the value as well.

assertThat(page.locator("input[type=number]")).hasValue(Pattern.compile("[0-9]"));

assertThat(locator).hasValues(values[, options])

Added in: v1.23
  • values <String[]|Pattern[]> Expected options currently selected.#
  • options <LocatorAssertions.HasValuesOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • returns: <void>#

Ensures the Locator points to multi-select/combobox (i.e. a select with the multiple attribute) and the specified values are selected.

For example, given the following element:

<select id="favorite-colors" multiple>
<option value="R">Red</option>
<option value="G">Green</option>
<option value="B">Blue</option>
</select>
page.locator("id=favorite-colors").selectOption(["R", "G"]);
assertThat(page.locator("id=favorite-colors")).hasValues(new Pattern[] { Pattern.compile("R"), Pattern.compile("G") });

assertThat(page).not()

Added in: v1.20

Makes the assertion check for the opposite condition. For example, this code tests that the page URL doesn't contain "error":

assertThat(page).not().hasURL("error");

assertThat(page).hasTitle(titleOrRegExp[, options])

Added in: v1.20
  • titleOrRegExp <String|Pattern> Expected title or RegExp. Added in: v1.18#
  • options <PageAssertions.HasTitleOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the page has the given title.

assertThat(page).hasTitle("Playwright");

assertThat(page).hasURL(urlOrRegExp[, options])

Added in: v1.20
  • urlOrRegExp <String|Pattern> Expected URL string or RegExp. Added in: v1.18#
  • options <PageAssertions.HasURLOptions>
    • setTimeout <double> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the page is navigated to the given URL.

assertThat(page).hasURL(".com");

assertThat(apiResponse).not()

Added in: v1.20

Makes the assertion check for the opposite condition. For example, this code tests that the response status is not successful:

assertThat(response).not().isOK();

assertThat(apiResponse).isOK()

Added in: v1.18

Ensures the response status code is within 200..299 range.

assertThat(response).isOK();

setDefaultAssertionTimeout(timeout)

Added in: v1.25

Changes default timeout for Playwright assertions from 5 seconds to the specified value.

PlaywrightAssertions.setDefaultAssertionTimeout(30_000);