class Playwright::LocatorAssertions
“‘
expect(page.locator(“.status”)).to_have_text(“Submitted”)
page.get_by_role(“button”).click()
# ..
def test_status_becomes_submitted(page: Page) -> None:
from playwright.sync_api import Page, expect
“`python sync
The `LocatorAssertions` class provides assertion methods that can be used to make assertions about the `Locator` state in the tests.
def not_to_be_attached(attached: nil, timeout: nil)
def not_to_be_attached(attached: nil, timeout: nil) wrap_impl(@impl.not_to_be_attached(attached: unwrap_impl(attached), timeout: unwrap_impl(timeout))) end
def not_to_be_checked(timeout: nil)
def not_to_be_checked(timeout: nil) wrap_impl(@impl.not_to_be_checked(timeout: unwrap_impl(timeout))) end
def not_to_be_disabled(timeout: nil)
def not_to_be_disabled(timeout: nil) wrap_impl(@impl.not_to_be_disabled(timeout: unwrap_impl(timeout))) end
def not_to_be_editable(editable: nil, timeout: nil)
def not_to_be_editable(editable: nil, timeout: nil) wrap_impl(@impl.not_to_be_editable(editable: unwrap_impl(editable), timeout: unwrap_impl(timeout))) end
def not_to_be_empty(timeout: nil)
def not_to_be_empty(timeout: nil) wrap_impl(@impl.not_to_be_empty(timeout: unwrap_impl(timeout))) end
def not_to_be_enabled(enabled: nil, timeout: nil)
def not_to_be_enabled(enabled: nil, timeout: nil) wrap_impl(@impl.not_to_be_enabled(enabled: unwrap_impl(enabled), timeout: unwrap_impl(timeout))) end
def not_to_be_focused(timeout: nil)
def not_to_be_focused(timeout: nil) wrap_impl(@impl.not_to_be_focused(timeout: unwrap_impl(timeout))) end
def not_to_be_hidden(timeout: nil)
def not_to_be_hidden(timeout: nil) wrap_impl(@impl.not_to_be_hidden(timeout: unwrap_impl(timeout))) end
def not_to_be_in_viewport(ratio: nil, timeout: nil)
def not_to_be_in_viewport(ratio: nil, timeout: nil) wrap_impl(@impl.not_to_be_in_viewport(ratio: unwrap_impl(ratio), timeout: unwrap_impl(timeout))) end
def not_to_be_visible(timeout: nil, visible: nil)
def not_to_be_visible(timeout: nil, visible: nil) wrap_impl(@impl.not_to_be_visible(timeout: unwrap_impl(timeout), visible: unwrap_impl(visible))) end
def not_to_contain_class(expected, timeout: nil)
def not_to_contain_class(expected, timeout: nil) wrap_impl(@impl.not_to_contain_class(unwrap_impl(expected), timeout: unwrap_impl(timeout))) end
def not_to_contain_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil)
def not_to_contain_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) wrap_impl(@impl.not_to_contain_text(unwrap_impl(expected), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout), useInnerText: unwrap_impl(useInnerText))) end
def not_to_have_accessible_description(name, ignoreCase: nil, timeout: nil)
def not_to_have_accessible_description(name, ignoreCase: nil, timeout: nil) wrap_impl(@impl.not_to_have_accessible_description(unwrap_impl(name), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end
def not_to_have_accessible_error_message(errorMessage, ignoreCase: nil, timeout: nil)
def not_to_have_accessible_error_message(errorMessage, ignoreCase: nil, timeout: nil) wrap_impl(@impl.not_to_have_accessible_error_message(unwrap_impl(errorMessage), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end
def not_to_have_accessible_name(name, ignoreCase: nil, timeout: nil)
def not_to_have_accessible_name(name, ignoreCase: nil, timeout: nil) wrap_impl(@impl.not_to_have_accessible_name(unwrap_impl(name), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end
def not_to_have_attribute(name, value, ignoreCase: nil, timeout: nil)
def not_to_have_attribute(name, value, ignoreCase: nil, timeout: nil) wrap_impl(@impl.not_to_have_attribute(unwrap_impl(name), unwrap_impl(value), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end
def not_to_have_class(expected, timeout: nil)
def not_to_have_class(expected, timeout: nil) wrap_impl(@impl.not_to_have_class(unwrap_impl(expected), timeout: unwrap_impl(timeout))) end
def not_to_have_count(count, timeout: nil)
def not_to_have_count(count, timeout: nil) wrap_impl(@impl.not_to_have_count(unwrap_impl(count), timeout: unwrap_impl(timeout))) end
def not_to_have_css(name, value, timeout: nil)
def not_to_have_css(name, value, timeout: nil) wrap_impl(@impl.not_to_have_css(unwrap_impl(name), unwrap_impl(value), timeout: unwrap_impl(timeout))) end
def not_to_have_id(id, timeout: nil)
def not_to_have_id(id, timeout: nil) wrap_impl(@impl.not_to_have_id(unwrap_impl(id), timeout: unwrap_impl(timeout))) end
def not_to_have_js_property(name, value, timeout: nil)
def not_to_have_js_property(name, value, timeout: nil) wrap_impl(@impl.not_to_have_js_property(unwrap_impl(name), unwrap_impl(value), timeout: unwrap_impl(timeout))) end
def not_to_have_role(role, timeout: nil)
def not_to_have_role(role, timeout: nil) wrap_impl(@impl.not_to_have_role(unwrap_impl(role), timeout: unwrap_impl(timeout))) end
def not_to_have_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil)
def not_to_have_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) wrap_impl(@impl.not_to_have_text(unwrap_impl(expected), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout), useInnerText: unwrap_impl(useInnerText))) end
def not_to_have_value(value, timeout: nil)
def not_to_have_value(value, timeout: nil) wrap_impl(@impl.not_to_have_value(unwrap_impl(value), timeout: unwrap_impl(timeout))) end
def not_to_have_values(values, timeout: nil)
def not_to_have_values(values, timeout: nil) wrap_impl(@impl.not_to_have_values(unwrap_impl(values), timeout: unwrap_impl(timeout))) end
def not_to_match_aria_snapshot(expected, timeout: nil)
def not_to_match_aria_snapshot(expected, timeout: nil) wrap_impl(@impl.not_to_match_aria_snapshot(unwrap_impl(expected), timeout: unwrap_impl(timeout))) end
def to_be_attached(attached: nil, timeout: nil)
expect(page.get_by_text("Hidden text")).to_be_attached()
```python sync
**Usage**
Ensures that `Locator` points to an element that is [connected](https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected) to a Document or a ShadowRoot.
def to_be_attached(attached: nil, timeout: nil) wrap_impl(@impl.to_be_attached(attached: unwrap_impl(attached), timeout: unwrap_impl(timeout))) end
def to_be_checked(checked: nil, indeterminate: nil, timeout: nil)
expect(locator).to_be_checked()
locator = page.get_by_label("Subscribe to newsletter")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` points to a checked input.
def to_be_checked(checked: nil, indeterminate: nil, timeout: nil) wrap_impl(@impl.to_be_checked(checked: unwrap_impl(checked), indeterminate: unwrap_impl(indeterminate), timeout: unwrap_impl(timeout))) end
def to_be_disabled(timeout: nil)
expect(locator).to_be_disabled()
locator = page.locator("button.submit")
from playwright.sync_api import expect
```python sync
**Usage**
by the browser.
can be disabled by setting "disabled" attribute. "disabled" attribute on other elements is ignored
Note that only native control elements such as HTML `button`, `input`, `select`, `textarea`, `option`, `optgroup`
or is disabled via ['aria-disabled'](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-disabled).
Ensures the `Locator` points to a disabled element. Element is disabled if it has "disabled" attribute
def to_be_disabled(timeout: nil) wrap_impl(@impl.to_be_disabled(timeout: unwrap_impl(timeout))) end
def to_be_editable(editable: nil, timeout: nil)
expect(locator).to_be_editable()
locator = page.get_by_role("textbox")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` points to an editable element.
def to_be_editable(editable: nil, timeout: nil) wrap_impl(@impl.to_be_editable(editable: unwrap_impl(editable), timeout: unwrap_impl(timeout))) end
def to_be_empty(timeout: nil)
expect(locator).to_be_empty()
locator = page.locator("div.warning")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` points to an empty editable element or to a DOM node that has no text.
def to_be_empty(timeout: nil) wrap_impl(@impl.to_be_empty(timeout: unwrap_impl(timeout))) end
def to_be_enabled(enabled: nil, timeout: nil)
expect(locator).to_be_enabled()
locator = page.locator("button.submit")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` points to an enabled element.
def to_be_enabled(enabled: nil, timeout: nil) wrap_impl(@impl.to_be_enabled(enabled: unwrap_impl(enabled), timeout: unwrap_impl(timeout))) end
def to_be_focused(timeout: nil)
expect(locator).to_be_focused()
locator = page.get_by_role("textbox")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` points to a focused DOM node.
def to_be_focused(timeout: nil) wrap_impl(@impl.to_be_focused(timeout: unwrap_impl(timeout))) end
def to_be_hidden(timeout: nil)
expect(locator).to_be_hidden()
locator = page.locator('.my-element')
from playwright.sync_api import expect
```python sync
**Usage**
Ensures that `Locator` either does not resolve to any DOM node, or resolves to a [non-visible](../actionability.md#visible) one.
def to_be_hidden(timeout: nil) wrap_impl(@impl.to_be_hidden(timeout: unwrap_impl(timeout))) end
def to_be_in_viewport(ratio: nil, timeout: nil)
expect(locator).to_be_in_viewport(ratio=0.5)
# Make sure that at least half of the element intersects viewport.
expect(locator).not_to_be_in_viewport()
# Make sure element is fully outside of viewport.
expect(locator).to_be_in_viewport()
# Make sure at least some part of element intersects viewport.
locator = page.get_by_role("button")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` points to an element that intersects viewport, according to the [intersection observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
def to_be_in_viewport(ratio: nil, timeout: nil) wrap_impl(@impl.to_be_in_viewport(ratio: unwrap_impl(ratio), timeout: unwrap_impl(timeout))) end
def to_be_visible(timeout: nil, visible: nil)
).to_be_visible()
.first
.or_(page.get_by_role("button", name="Sign up"))
page.get_by_role("button", name="Sign in")
expect(
# At least one of the two elements is visible, possibly both.
expect(page.get_by_test_id("todo-item").first).to_be_visible()
# At least one item in the list is visible.
expect(page.get_by_text("Welcome")).to_be_visible()
# A specific element is visible.
```python sync
**Usage**
To check that at least one element from the list is visible, use [`method: Locator.first`].
Ensures that `Locator` points to an attached and [visible](../actionability.md#visible) DOM node.
def to_be_visible(timeout: nil, visible: nil) wrap_impl(@impl.to_be_visible(timeout: unwrap_impl(timeout), visible: unwrap_impl(visible))) end
def to_contain_class(expected, timeout: nil)
await expect(locator).to_contain_class(["inactive", "active", "inactive"])
locator = page.locator(".list > .component")
from playwright.sync_api import expect
```python sync
```
```html
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:
```
expect(locator).to_contain_class("row middle")
expect(locator).to_contain_class("selected")
expect(locator).to_contain_class("middle selected row")
locator = page.locator("#component")
from playwright.sync_api import expect
```python sync
```
```html
**Usage**
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](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList) in any order.
def to_contain_class(expected, timeout: nil) wrap_impl(@impl.to_contain_class(unwrap_impl(expected), timeout: unwrap_impl(timeout))) end
def to_contain_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil)
expect(page.locator("ul")).to_contain_text(["Text 3"])
# ✖ Locator points to the outer list element, not to the list items
expect(page.locator("ul > li")).to_contain_text(["Some 33"])
# ✖ No item contains this text
expect(page.locator("ul > li")).to_contain_text(["Text 3", "Text 2"])
# ✖ Wrong order
expect(page.locator("ul > li")).to_contain_text(["Text 1", "Text 3"])
# ✓ Contains the right items in the right order
from playwright.sync_api import expect
```python sync
Let's see how we can use the assertion:
```
```html
For example, consider the following list:
1. Each text value from the expected array is matched by some element from the list.
1. The matching subset of elements has the same order as the expected array.
1. Elements from a **subset** of this list contain text from the expected array, respectively.
1. Locator resolves to a list of elements.
If you pass an array as an expected value, the expectations are:
```
expect(locator).to_contain_text(re.compile(r"\d messages"))
expect(locator).to_contain_text("substring")
locator = page.locator('.title')
from playwright.sync_api import expect
import re
```python sync
**Usage**
in the expected string before matching. When regular expression is used, the actual text is matched as is.
When `expected` parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and
**Details**
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.
def to_contain_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) wrap_impl(@impl.to_contain_text(unwrap_impl(expected), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout), useInnerText: unwrap_impl(useInnerText))) end
def to_have_accessible_description(description, ignoreCase: nil, timeout: nil)
expect(locator).to_have_accessible_description("Save results to disk")
locator = page.get_by_test_id("save-button")
```python sync
**Usage**
Ensures the `Locator` points to an element with a given [accessible description](https://w3c.github.io/accname/#dfn-accessible-description).
def to_have_accessible_description(description, ignoreCase: nil, timeout: nil) wrap_impl(@impl.to_have_accessible_description(unwrap_impl(description), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end
def to_have_accessible_error_message(errorMessage, ignoreCase: nil, timeout: nil)
expect(locator).to_have_accessible_error_message("Username is required.")
locator = page.get_by_test_id("username-input")
```python sync
**Usage**
Ensures the `Locator` points to an element with a given [aria errormessage](https://w3c.github.io/aria/#aria-errormessage).
def to_have_accessible_error_message(errorMessage, ignoreCase: nil, timeout: nil) wrap_impl(@impl.to_have_accessible_error_message(unwrap_impl(errorMessage), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end
def to_have_accessible_name(name, ignoreCase: nil, timeout: nil)
expect(locator).to_have_accessible_name("Save to disk")
locator = page.get_by_test_id("save-button")
```python sync
**Usage**
Ensures the `Locator` points to an element with a given [accessible name](https://w3c.github.io/accname/#dfn-accessible-name).
def to_have_accessible_name(name, ignoreCase: nil, timeout: nil) wrap_impl(@impl.to_have_accessible_name(unwrap_impl(name), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end
def to_have_attribute(name, value, ignoreCase: nil, timeout: nil)
expect(locator).to_have_attribute("type", "text")
locator = page.locator("input")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` points to an element with given attribute.
def to_have_attribute(name, value, ignoreCase: nil, timeout: nil) wrap_impl(@impl.to_have_attribute(unwrap_impl(name), unwrap_impl(value), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end
def to_have_class(expected, timeout: nil)
expect(locator).to_have_class(["component", "component selected", "component"])
locator = page.locator(".list > .component")
from playwright.sync_api import expect
```python sync
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:
```
expect(locator).to_have_class(re.compile(r"(^|\\s)selected(\\s|$)"))
expect(locator).to_have_class("middle selected row")
locator = page.locator("#component")
from playwright.sync_api import expect
```python sync
```
```html
**Usage**
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 [`method: LocatorAssertions.toContainClass`].
def to_have_class(expected, timeout: nil) wrap_impl(@impl.to_have_class(unwrap_impl(expected), timeout: unwrap_impl(timeout))) end
def to_have_count(count, timeout: nil)
expect(locator).to_have_count(3)
locator = page.locator("list > .component")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` resolves to an exact number of DOM nodes.
def to_have_count(count, timeout: nil) wrap_impl(@impl.to_have_count(unwrap_impl(count), timeout: unwrap_impl(timeout))) end
def to_have_css(name, value, pseudo: nil, timeout: nil)
expect(locator).to_have_css("display", "flex")
locator = page.get_by_role("button")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` resolves to an element with the given computed CSS style.
def to_have_css(name, value, pseudo: nil, timeout: nil) wrap_impl(@impl.to_have_css(unwrap_impl(name), unwrap_impl(value), pseudo: unwrap_impl(pseudo), timeout: unwrap_impl(timeout))) end
def to_have_id(id, timeout: nil)
expect(locator).to_have_id("lastname")
locator = page.get_by_role("textbox")
from playwright.sync_api import expect
```python sync
**Usage**
Ensures the `Locator` points to an element with the given DOM Node ID.
def to_have_id(id, timeout: nil) wrap_impl(@impl.to_have_id(unwrap_impl(id), timeout: unwrap_impl(timeout))) end
def to_have_js_property(name, value, timeout: nil)
expect(locator).to_have_js_property("loaded", True)
locator = page.locator(".component")
from playwright.sync_api import expect
```python sync
**Usage**
of a primitive type as well as a plain serializable JavaScript object.
Ensures the `Locator` points to an element with given JavaScript property. Note that this property can be
def to_have_js_property(name, value, timeout: nil) wrap_impl(@impl.to_have_js_property(unwrap_impl(name), unwrap_impl(value), timeout: unwrap_impl(timeout))) end
def to_have_role(role, timeout: nil)
expect(locator).to_have_role("button")
locator = page.get_by_test_id("save-button")
```python sync
**Usage**
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.
Ensures the `Locator` points to an element with a given [ARIA role](https://www.w3.org/TR/wai-aria-1.2/#roles).
def to_have_role(role, timeout: nil) wrap_impl(@impl.to_have_role(unwrap_impl(role), timeout: unwrap_impl(timeout))) end
def to_have_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil)
expect(page.locator("ul")).to_have_text(["Text 1", "Text 2", "Text 3"])
# ✖ Locator points to the outer list element, not to the list items
expect(page.locator("ul > li")).to_have_text(["Text 1", "Text 2", "Text"])
# ✖ Last item does not match
expect(page.locator("ul > li")).to_have_text(["Text 3", "Text 2", "Text 1"])
# ✖ Wrong order
expect(page.locator("ul > li")).to_have_text(["Text 1", "Text 2", "Text 3"])
# ✓ Has the right items in the right order
from playwright.sync_api import expect
```python sync
Let's see how we can use the assertion:
```
```html
For example, consider the following list:
1. Elements from the list have text matching expected array values, one by one, in order.
1. The number of elements equals the number of expected values in the array.
1. Locator resolves to a list of elements.
If you pass an array as an expected value, the expectations are:
```
expect(locator).to_have_text(re.compile(r"Welcome, .*"))
expect(locator).to_have_text(re.compile(r"Welcome, Test User"))
locator = page.locator(".title")
from playwright.sync_api import expect
import re
```python sync
**Usage**
in the expected string before matching. When regular expression is used, the actual text is matched as is.
When `expected` parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and
**Details**
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.
def to_have_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) wrap_impl(@impl.to_have_text(unwrap_impl(expected), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout), useInnerText: unwrap_impl(useInnerText))) end
def to_have_value(value, timeout: nil)
expect(locator).to_have_value(re.compile(r"[0-9]"))
locator = page.locator("input[type=number]")
from playwright.sync_api import expect
import re
```python sync
**Usage**
Ensures the `Locator` points to an element with the given input value. You can use regular expressions for the value as well.
def to_have_value(value, timeout: nil) wrap_impl(@impl.to_have_value(unwrap_impl(value), timeout: unwrap_impl(timeout))) end
def to_have_values(values, timeout: nil)
expect(locator).to_have_values([re.compile(r"R"), re.compile(r"G")])
locator.select_option(["R", "G"])
locator = page.locator("id=favorite-colors")
from playwright.sync_api import expect
import re
```python sync
```
def to_have_values(values, timeout: nil) wrap_impl(@impl.to_have_values(unwrap_impl(values), timeout: unwrap_impl(timeout))) end
def to_match_aria_snapshot(expected, timeout: nil)
''')
- textbox "What needs to be done?"
- heading "todos"
expect(page.locator('body')).to_match_aria_snapshot('''
page.goto("https://demo.playwright.dev/todomvc/")
```python sync
**Usage**
Asserts that the target element matches the given [accessibility snapshot](../aria-snapshots.md).
def to_match_aria_snapshot(expected, timeout: nil) wrap_impl(@impl.to_match_aria_snapshot(unwrap_impl(expected), timeout: unwrap_impl(timeout))) end