Skip to content

LocatorAssertions ​

Added in: v1.17

The LocatorAssertions class provides assertion methods that can be used to make assertions about the Locator state in the tests.

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

public class TestLocator {
  // ...
  @Test
  void statusBecomesSubmitted() {
    // ...
    page.getByRole(AriaRole.BUTTON).click();
    assertThat(page.locator(".status")).hasText("Submitted");
  }
}

Methods ​

locatorAssertions.containsClass() ​

Added in: v1.52

Ensures the Locator points to an element with given CSS classes. All classes from the asserted value, separated by spaces, must be present in the Element.classList in any order.

Usage

html
<div class='middle selected row' id='component'></div>
java
assertThat(page.locator("#component")).containsClass("middle selected row");
assertThat(page.locator("#component")).containsClass("selected");
assertThat(page.locator("#component")).containsClass("row middle");

When an array is passed, the method asserts that the list of elements located matches the corresponding list of expected class lists. Each element's class attribute is matched against the corresponding class in the array:

html
<div class='list'>
  <div class='component inactive'></div>
  <div class='component active'></div>
  <div class='component inactive'></div>
</div>
java
assertThat(page.locator(".list > .component")).containsClass(Arrays.asList("inactive", "active", "inactive"));

Arguments

  • expected string | Array<string>

    A string containing expected class names, separated by spaces, or a list of such strings to assert multiple elements.

  • options Object (optional)

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.containsText() ​

Added in: v1.20

Ensures the Locator points to an element that contains the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

Details

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

Usage

java
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:

html
<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:

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

// ✖ 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"});

Arguments

  • expected string | RegExp | Array<string> | Array<RegExp> Added in: v1.18

    Expected substring or RegExp or a list of those.

  • options Object (optional)

    • ignoreCase boolean (optional) Added in: v1.23

      Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified.

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

    • useInnerText boolean (optional) Added in: v1.18

      Whether to use element.innerText instead of element.textContent when retrieving DOM node text.

locatorAssertions.hasAccessibleDescription() ​

Added in: v1.44

Ensures the Locator points to an element with a given accessible description.

Usage

java
Locator locator = page.getByTestId("save-button");
assertThat(locator).hasAccessibleDescription("Save results to disk");

Arguments

  • description string | RegExp

    Expected accessible description.

  • options Object (optional)

    • ignoreCase boolean (optional)

      Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified.

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasAccessibleErrorMessage() ​

Added in: v1.50

Ensures the Locator points to an element with a given aria errormessage.

Usage

java
Locator locator = page.getByTestId("username-input");
assertThat(locator).hasAccessibleErrorMessage("Username is required.");

Arguments

  • errorMessage string | RegExp

    Expected accessible error message.

  • options Object (optional)

    • ignoreCase boolean (optional)

      Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified.

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasAccessibleName() ​

Added in: v1.44

Ensures the Locator points to an element with a given accessible name.

Usage

java
Locator locator = page.getByTestId("save-button");
assertThat(locator).hasAccessibleName("Save to disk");

Arguments

  • name string | RegExp

    Expected accessible name.

  • options Object (optional)

    • ignoreCase boolean (optional)

      Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified.

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasAttribute() ​

Added in: v1.20

Ensures the Locator points to an element with given attribute.

Usage

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

Arguments

  • name string Added in: v1.18

    Attribute name.

  • value string | RegExp Added in: v1.18

    Expected attribute value.

  • options Object (optional)

    • ignoreCase boolean (optional) Added in: v1.40

      Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified.

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasClass() ​

Added in: v1.20

Ensures the Locator points to an element with given CSS classes. When a string is provided, it must fully match the element's class attribute. To match individual classes use locatorAssertions.containsClass().

Usage

html
<div class='middle selected row' id='component'></div>
java
assertThat(page.locator("#component")).hasClass("middle selected row");
assertThat(page.locator("#component")).hasClass(Pattern.compile("(^|\\s)selected(\\s|$)"));

When an array is passed, the method asserts that the list of elements located matches the corresponding list of expected class values. Each element's class attribute is matched against the corresponding string or regular expression in the array:

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

Arguments

  • expected string | RegExp | Array<string> | Array<RegExp> Added in: v1.18

    Expected class or RegExp or a list of those.

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasCount() ​

Added in: v1.20

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

Usage

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

Arguments

  • count int Added in: v1.18

    Expected count.

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasCSS() ​

Added in: v1.20

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

Usage

java
assertThat(page.getByRole(AriaRole.BUTTON)).hasCSS("display", "flex");

Arguments

  • name string Added in: v1.18

    CSS property name.

  • value string | RegExp Added in: v1.18

    CSS property value.

  • options Object (optional)

    • pseudo "before" | "after" (optional) Added in: v1.60

      Pseudo-element to read computed styles from.

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasId() ​

Added in: v1.20

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

Usage

java
assertThat(page.getByRole(AriaRole.TEXTBOX)).hasId("lastname");

Arguments

  • id string | RegExp Added in: v1.18

    Element id.

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasJSProperty() ​

Added in: v1.20

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.

Usage

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

Arguments

  • name string Added in: v1.18

    Property name.

  • value any Added in: v1.18

    Property value.

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasRole() ​

Added in: v1.44

Ensures the Locator points to an element with a given ARIA role.

Note that role is matched as a string, disregarding the ARIA role hierarchy. For example, asserting a superclass role "checkbox" on an element with a subclass role "switch" will fail.

Usage

java
Locator locator = page.getByTestId("save-button");
assertThat(locator).hasRole(AriaRole.BUTTON);

Arguments

  • role "alert" | "alertdialog" | "application" | "article" | "banner" | "blockquote" | "button" | "caption" | "cell" | "checkbox" | "code" | "columnheader" | "combobox" | "complementary" | "contentinfo" | "definition" | "deletion" | "dialog" | "directory" | "document" | "emphasis" | "feed" | "figure" | "form" | "generic" | "grid" | "gridcell" | "group" | "heading" | "img" | "insertion" | "link" | "list" | "listbox" | "listitem" | "log" | "main" | "marquee" | "math" | "meter" | "menu" | "menubar" | "menuitem" | "menuitemcheckbox" | "menuitemradio" | "navigation" | "none" | "note" | "option" | "paragraph" | "presentation" | "progressbar" | "radio" | "radiogroup" | "region" | "row" | "rowgroup" | "rowheader" | "scrollbar" | "search" | "searchbox" | "separator" | "slider" | "spinbutton" | "status" | "strong" | "subscript" | "superscript" | "switch" | "tab" | "table" | "tablist" | "tabpanel" | "term" | "textbox" | "time" | "timer" | "toolbar" | "tooltip" | "tree" | "treegrid" | "treeitem"

    Required aria role.

  • options Object (optional)

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasText() ​

Added in: v1.20

Ensures the Locator points to an element with the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

Details

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

Usage

java
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:

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

Let's see how we can use the assertion:

java
// ✓ 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"});

Arguments

  • expected string | RegExp | Array<string> | Array<RegExp> Added in: v1.18

    Expected string or RegExp or a list of those.

  • options Object (optional)

    • ignoreCase boolean (optional) Added in: v1.23

      Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified.

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

    • useInnerText boolean (optional) Added in: v1.18

      Whether to use element.innerText instead of element.textContent when retrieving DOM node text.

locatorAssertions.hasValue() ​

Added in: v1.20

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

Usage

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

Arguments

  • value string | RegExp Added in: v1.18

    Expected value.

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.hasValues() ​

Added in: v1.23

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

Usage

For example, given the following element:

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

Arguments

  • values Array<string> | Array<RegExp>

    Expected options currently selected.

  • options Object (optional)

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isAttached() ​

Added in: v1.33

Ensures that Locator points to an element that is connected to a Document or a ShadowRoot.

Usage

java
assertThat(page.getByText("Hidden text")).isAttached();

Arguments

  • options Object (optional)

    • attached boolean (optional)

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isChecked() ​

Added in: v1.20

Ensures the Locator points to a checked input.

Usage

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

Arguments

  • options Object (optional)

    • checked boolean (optional) Added in: v1.18

      Provides state to assert for. Asserts for input to be checked by default. This option can't be used when indeterminate is set to true.

    • indeterminate boolean (optional) Added in: v1.50

      Asserts that the element is in the indeterminate (mixed) state. Only supported for checkboxes and radio buttons. This option can't be true when checked is provided.

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isDisabled() ​

Added in: v1.20

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.

Usage

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

Arguments

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isEditable() ​

Added in: v1.20

Ensures the Locator points to an editable element.

Usage

java
assertThat(page.getByRole(AriaRole.TEXTBOX)).isEditable();

Arguments

  • options Object (optional)

    • editable boolean (optional) Added in: v1.26

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isEmpty() ​

Added in: v1.20

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

Usage

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

Arguments

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isEnabled() ​

Added in: v1.20

Ensures the Locator points to an enabled element.

Usage

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

Arguments

  • options Object (optional)

    • enabled boolean (optional) Added in: v1.26

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isFocused() ​

Added in: v1.20

Ensures the Locator points to a focused DOM node.

Usage

java
assertThat(page.getByRole(AriaRole.TEXTBOX)).isFocused();

Arguments

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isHidden() ​

Added in: v1.20

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

Usage

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

Arguments

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isInViewport() ​

Added in: v1.31

Ensures the Locator points to an element that intersects viewport, according to the intersection observer API.

Usage

java
Locator locator = page.getByRole(AriaRole.BUTTON);
// Make sure at least some part of element intersects viewport.
assertThat(locator).isInViewport();
// Make sure element is fully outside of viewport.
assertThat(locator).not().isInViewport();
// Make sure that at least half of the element intersects viewport.
assertThat(locator).isInViewport(new LocatorAssertions.IsInViewportOptions().setRatio(0.5));

Arguments

  • options Object (optional)

    • ratio float (optional)

      The minimal ratio of the element to intersect viewport. If equals to 0, then element should intersect viewport at any positive ratio. Defaults to 0.

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

locatorAssertions.isVisible() ​

Added in: v1.20

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

To check that at least one element from the list is visible, use locator.first().

Usage

java
// A specific element is visible.
assertThat(page.getByText("Welcome")).isVisible();

// At least one item in the list is visible.
assertThat(page.getByTestId("todo-item").first()).isVisible();

// At least one of the two elements is visible, possibly both.
assertThat(
  page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Sign in"))
    .or(page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Sign up")))
    .first()
).isVisible();

Arguments

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

      Time to retry the assertion for in milliseconds. Defaults to 5000.

    • visible boolean (optional) Added in: v1.26

locatorAssertions.matchesAriaSnapshot() ​

Added in: v1.49

Asserts that the target element matches the given accessibility snapshot.

Usage

java
page.navigate("https://demo.playwright.dev/todomvc/");
assertThat(page.locator("body")).matchesAriaSnapshot("""
  - heading "todos"
  - textbox "What needs to be done?"
""");

Arguments

  • expected string

  • options Object (optional)

    • timeout float (optional)

      Time to retry the assertion for in milliseconds. Defaults to 5000.

Properties ​

locatorAssertions.not() ​

Added in: v1.20

Makes the assertion check for the opposite condition.

Usage

For example, this code tests that the Locator doesn't contain text "error":

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

Type

  • LocatorAssertions

Released under the Apache 2.0 License.