Skip to main content
Version: 1.18

LocatorAssertions

The LocatorAssertions class provides assertion methods that can be used to make assertions about the Locator state in the tests. A new instance of LocatorAssertions is created by calling PlaywrightAssertions.assertThat(locator):

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

public class TestLocator {
...
@Test
void statusBecomesSubmitted() {
...
page.click("#submit-button");
assertThat(page.locator(".status")).hasText("Submitted");
}
}

LocatorAssertions.containsText(expected[, options])

  • expected <String|Pattern|String[]|Pattern[]> Expected substring or RegExp or a list of those.#
  • options <LocatorAssertions.ContainsTextOptions>
    • 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"});

LocatorAssertions.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");

LocatorAssertions.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"});

LocatorAssertions.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);

LocatorAssertions.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");

LocatorAssertions.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");

LocatorAssertions.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);

LocatorAssertions.hasText(expected[, options])

  • expected <String|Pattern|String[]|Pattern[]> Expected substring or RegExp or a list of those.#
  • options <LocatorAssertions.HasTextOptions>
    • 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"});

LocatorAssertions.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]"));

LocatorAssertions.isChecked([options])

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

Ensures the Locator points to a checked input.

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

LocatorAssertions.isDisabled([options])

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

Ensures the Locator points to a disabled element.

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

LocatorAssertions.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();

LocatorAssertions.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();

LocatorAssertions.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();

LocatorAssertions.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();

LocatorAssertions.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();

LocatorAssertions.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")).toBeVisible();

LocatorAssertions.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");