Skip to main content
Version: 1.23

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.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.

assertThat(locator).not()

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])

  • options <LocatorAssertions.IsCheckedOptions>
  • returns: <void>#

Ensures the Locator points to a checked input.

assertThat(page.locator(".subscribe")).isChecked();

assertThat(locator).isDisabled([options])

  • options <LocatorAssertions.IsDisabledOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • 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])

  • options <LocatorAssertions.IsEditableOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • returns: <void>#

Ensures the Locator points to an editable element.

assertThat(page.locator("input")).isEditable();

assertThat(locator).isEmpty([options])

  • options <LocatorAssertions.IsEmptyOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • 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])

  • options <LocatorAssertions.IsEnabledOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • returns: <void>#

Ensures the Locator points to an enabled element.

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

assertThat(locator).isFocused([options])

  • options <LocatorAssertions.IsFocusedOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • returns: <void>#

Ensures the Locator points to a focused DOM node.

assertThat(page.locator("input")).isFocused();

assertThat(locator).isHidden([options])

  • options <LocatorAssertions.IsHiddenOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • returns: <void>#

Ensures the Locator points to a hidden DOM node, which is the opposite of visible.

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

assertThat(locator).isVisible([options])

  • options <LocatorAssertions.IsVisibleOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • returns: <void>#

Ensures the Locator points to a visible DOM node.

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

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

  • expected <String|Pattern|String[]|Pattern[]> Expected substring or RegExp or a list of those.#
  • options <LocatorAssertions.ContainsTextOptions>
    • setIgnoreCase <boolean> Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified.#
    • setTimeout <double> Time to retry the assertion for.#
    • setUseInnerText <boolean> Whether to use element.innerText instead of element.textContent when retrieving DOM node text.#
  • 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");

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

assertThat(page.locator("list > .list-item")).containsText(new String[] {"Text 1", "Text 4", "Text 5"});

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

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

Ensures the Locator points to an element with given attribute.

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

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

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

Ensures the Locator points to an element with given CSS class.

assertThat(page.locator("#component")).hasClass(Pattern.compile("selected"));

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])

  • count <int> Expected count.#
  • options <LocatorAssertions.HasCountOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • 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])

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

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

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

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

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

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

assertThat(page.locator("input")).hasId("lastname");

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

  • name <String> Property name.#
  • value <Object> Property value.#
  • options <LocatorAssertions.HasJSPropertyOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • 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])

  • expected <String|Pattern|String[]|Pattern[]> Expected substring or RegExp or a list of those.#
  • options <LocatorAssertions.HasTextOptions>
    • setIgnoreCase <boolean> Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified.#
    • setTimeout <double> Time to retry the assertion for.#
    • setUseInnerText <boolean> Whether to use element.innerText instead of element.textContent when retrieving DOM node text.#
  • 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, .*"));

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

assertThat(page.locator("list > .component")).hasText(new String[] {"Text 1", "Text 2", "Text 3"});

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

  • value <String|Pattern> Expected value.#
  • options <LocatorAssertions.HasValueOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • 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])

  • 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()

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])

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

Ensures the page has the given title.

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

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

  • urlOrRegExp <String|Pattern> Expected substring or RegExp.#
  • options <PageAssertions.HasURLOptions>
    • setTimeout <double> Time to retry the assertion for.#
  • returns: <void>#

Ensures the page is navigated to the given URL.

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

assertThat(apiResponse).not()

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()

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

assertThat(response).isOK();