A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://playwright.dev/java/docs/api/class-locatorassertions below:

LocatorAssertions | Playwright Java

LocatorAssertions

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


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 containsClassAdded in: v1.52 locatorAssertions.containsClass

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

<div class='middle selected row' id='component'></div>
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:

<div class='list'>
<div class='component inactive'></div>
<div class='component active'></div>
<div class='component inactive'></div>
</div>
assertThat(page.locator(".list > .component")).containsClass(new String[] {"inactive", "active", "inactive"});

Arguments

Returns

containsTextAdded in: v1.20 locatorAssertions.containsText

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.

Usage

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:


assertThat(page.locator("ul > li")).containsText(new String[] {"Text 1", "Text 3", "Text 4"});


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


assertThat(page.locator("ul > li")).containsText(new String[] {"Some 33"});


assertThat(page.locator("ul")).containsText(new String[] {"Text 3"});

Arguments

Returns

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.

hasAccessibleDescriptionAdded in: v1.44 locatorAssertions.hasAccessibleDescription

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

Usage

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

Arguments

Returns

hasAccessibleErrorMessageAdded in: v1.50 locatorAssertions.hasAccessibleErrorMessage

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

Usage

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

Arguments

Returns

hasAccessibleNameAdded in: v1.44 locatorAssertions.hasAccessibleName

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

Usage

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

Arguments

Returns

hasAttributeAdded in: v1.20 locatorAssertions.hasAttribute

Ensures the Locator points to an element with given attribute.

Usage

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

Arguments

Returns

hasClassAdded in: v1.20 locatorAssertions.hasClass

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 assertThat(locator).containsClass().

Usage

<div class='middle selected row' id='component'></div>
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:

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

Arguments

Returns

hasCountAdded in: v1.20 locatorAssertions.hasCount

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

Usage

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

Arguments

Returns

hasCSSAdded in: v1.20 locatorAssertions.hasCSS

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

Usage

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

Arguments

Returns

hasIdAdded in: v1.20 locatorAssertions.hasId

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

Usage

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

Arguments

Returns

hasJSPropertyAdded in: v1.20 locatorAssertions.hasJSProperty

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

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

Arguments

Returns

hasRoleAdded in: v1.44 locatorAssertions.hasRole

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

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

Arguments

Returns

hasTextAdded in: v1.20 locatorAssertions.hasText

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.

Usage

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:


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


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


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


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

Arguments

Returns

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.

hasValueAdded in: v1.20 locatorAssertions.hasValue

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

Usage

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

Arguments

Returns

hasValuesAdded in: v1.23 locatorAssertions.hasValues

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:

<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(new String[]{"R", "G"});
assertThat(page.locator("id=favorite-colors")).hasValues(new Pattern[] { Pattern.compile("R"), Pattern.compile("G") });

Arguments

Returns

isAttachedAdded in: v1.33 locatorAssertions.isAttached

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

Usage

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

Arguments

Returns

isCheckedAdded in: v1.20 locatorAssertions.isChecked

Ensures the Locator points to a checked input.

Usage

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

Arguments

Returns

isDisabledAdded in: v1.20 locatorAssertions.isDisabled

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

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

Arguments

Returns

isEditableAdded in: v1.20 locatorAssertions.isEditable

Ensures the Locator points to an editable element.

Usage

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

Arguments

Returns

isEmptyAdded in: v1.20 locatorAssertions.isEmpty

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

Usage

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

Arguments

Returns

isEnabledAdded in: v1.20 locatorAssertions.isEnabled

Ensures the Locator points to an enabled element.

Usage

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

Arguments

Returns

isFocusedAdded in: v1.20 locatorAssertions.isFocused

Ensures the Locator points to a focused DOM node.

Usage

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

Arguments

Returns

isHiddenAdded in: v1.20 locatorAssertions.isHidden

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

Usage

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

Arguments

Returns

isInViewportAdded in: v1.31 locatorAssertions.isInViewport

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

Usage

Locator locator = page.getByRole(AriaRole.BUTTON);

assertThat(locator).isInViewport();

assertThat(locator).not().isInViewport();

assertThat(locator).isInViewport(new LocatorAssertions.IsInViewportOptions().setRatio(0.5));

Arguments

Returns

isVisibleAdded in: v1.20 locatorAssertions.isVisible

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


assertThat(page.getByText("Welcome")).isVisible();


assertThat(page.getByTestId("todo-item").first()).isVisible();


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

Returns

matchesAriaSnapshotAdded in: v1.49 locatorAssertions.matchesAriaSnapshot

Asserts that the target element matches the given accessibility snapshot.

Usage

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

Arguments

Returns

Properties not()Added in: v1.20 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");

Usage

assertThat(locator).not()

Returns


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4