Class: Playwright::LocatorAssertions
- Inherits:
-
PlaywrightApi
- Object
- PlaywrightApi
- Playwright::LocatorAssertions
- Defined in:
- lib/playwright_api/locator_assertions.rb
Overview
The LocatorAssertions class provides assertion methods that can be used to make assertions about the Locator state in the tests.
from playwright.sync_api import Page, expect
def test_status_becomes_submitted(page: Page) -> None:
# ..
page.get_by_role("button").click()
expect(page.locator(".status")).to_have_text("Submitted")
Instance Method Summary collapse
-
#not_to_be_attached(attached: nil, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeAttached]. -
#not_to_be_checked(timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeChecked]. -
#not_to_be_disabled(timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeDisabled]. -
#not_to_be_editable(editable: nil, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeEditable]. -
#not_to_be_empty(timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeEmpty]. -
#not_to_be_enabled(enabled: nil, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeEnabled]. -
#not_to_be_focused(timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeFocused]. -
#not_to_be_hidden(timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeHidden]. -
#not_to_be_in_viewport(ratio: nil, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeInViewport]. -
#not_to_be_visible(timeout: nil, visible: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toBeVisible]. -
#not_to_contain_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toContainText]. -
#not_to_have_accessible_description(name, ignoreCase: nil, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveAccessibleDescription]. -
#not_to_have_accessible_error_message(errorMessage, ignoreCase: nil, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveAccessibleErrorMessage]. -
#not_to_have_accessible_name(name, ignoreCase: nil, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveAccessibleName]. -
#not_to_have_attribute(name, value, ignoreCase: nil, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveAttribute]. -
#not_to_have_class(expected, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveClass]. -
#not_to_have_count(count, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveCount]. -
#not_to_have_css(name, value, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveCSS]. -
#not_to_have_id(id, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveId]. -
#not_to_have_js_property(name, value, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveJSProperty]. -
#not_to_have_role(role, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveRole]. -
#not_to_have_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveText]. -
#not_to_have_value(value, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveValue]. -
#not_to_have_values(values, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toHaveValues]. -
#not_to_match_aria_snapshot(expected, timeout: nil) ⇒ Object
The opposite of [
method: LocatorAssertions.toMatchAriaSnapshot]. -
#to_be_attached(attached: nil, timeout: nil) ⇒ Object
Ensures that
Locatorpoints to an element that is connected to a Document or a ShadowRoot. -
#to_be_checked(checked: nil, indeterminate: nil, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to a checked input. -
#to_be_disabled(timeout: nil) ⇒ Object
Ensures the
Locatorpoints to a disabled element. -
#to_be_editable(editable: nil, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an editable element. -
#to_be_empty(timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an empty editable element or to a DOM node that has no text. -
#to_be_enabled(enabled: nil, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an enabled element. -
#to_be_focused(timeout: nil) ⇒ Object
Ensures the
Locatorpoints to a focused DOM node. -
#to_be_hidden(timeout: nil) ⇒ Object
Ensures that
Locatoreither does not resolve to any DOM node, or resolves to a non-visible one. -
#to_be_in_viewport(ratio: nil, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element that intersects viewport, according to the intersection observer API. -
#to_be_visible(timeout: nil, visible: nil) ⇒ Object
Ensures that
Locatorpoints to an attached and visible DOM node. -
#to_contain_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) ⇒ Object
Ensures the
Locatorpoints to an element that contains the given text. -
#to_have_accessible_description(description, ignoreCase: nil, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element with a given accessible description. -
#to_have_accessible_error_message(errorMessage, ignoreCase: nil, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element with a given aria errormessage. -
#to_have_accessible_name(name, ignoreCase: nil, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element with a given accessible name. -
#to_have_attribute(name, value, ignoreCase: nil, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element with given attribute. -
#to_have_class(expected, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element with given CSS classes. -
#to_have_count(count, timeout: nil) ⇒ Object
Ensures the
Locatorresolves to an exact number of DOM nodes. -
#to_have_css(name, value, timeout: nil) ⇒ Object
Ensures the
Locatorresolves to an element with the given computed CSS style. -
#to_have_id(id, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element with the given DOM Node ID. -
#to_have_js_property(name, value, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element with given JavaScript property. -
#to_have_role(role, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element with a given ARIA role. -
#to_have_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) ⇒ Object
Ensures the
Locatorpoints to an element with the given text. -
#to_have_value(value, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to an element with the given input value. -
#to_have_values(values, timeout: nil) ⇒ Object
Ensures the
Locatorpoints to multi-select/combobox (i.e. aselectwith themultipleattribute) and the specified values are selected. -
#to_match_aria_snapshot(expected, timeout: nil) ⇒ Object
Asserts that the target element matches the given accessibility snapshot.
Methods inherited from PlaywrightApi
Constructor Details
This class inherits a constructor from Playwright::PlaywrightApi
Instance Method Details
#not_to_be_attached(attached: nil, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeAttached].
17 18 19 |
# File 'lib/playwright_api/locator_assertions.rb', line 17 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 |
#not_to_be_checked(timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeChecked].
23 24 25 |
# File 'lib/playwright_api/locator_assertions.rb', line 23 def not_to_be_checked(timeout: nil) wrap_impl(@impl.not_to_be_checked(timeout: unwrap_impl(timeout))) end |
#not_to_be_disabled(timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeDisabled].
29 30 31 |
# File 'lib/playwright_api/locator_assertions.rb', line 29 def not_to_be_disabled(timeout: nil) wrap_impl(@impl.not_to_be_disabled(timeout: unwrap_impl(timeout))) end |
#not_to_be_editable(editable: nil, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeEditable].
35 36 37 |
# File 'lib/playwright_api/locator_assertions.rb', line 35 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 |
#not_to_be_empty(timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeEmpty].
41 42 43 |
# File 'lib/playwright_api/locator_assertions.rb', line 41 def not_to_be_empty(timeout: nil) wrap_impl(@impl.not_to_be_empty(timeout: unwrap_impl(timeout))) end |
#not_to_be_enabled(enabled: nil, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeEnabled].
47 48 49 |
# File 'lib/playwright_api/locator_assertions.rb', line 47 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 |
#not_to_be_focused(timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeFocused].
53 54 55 |
# File 'lib/playwright_api/locator_assertions.rb', line 53 def not_to_be_focused(timeout: nil) wrap_impl(@impl.not_to_be_focused(timeout: unwrap_impl(timeout))) end |
#not_to_be_hidden(timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeHidden].
59 60 61 |
# File 'lib/playwright_api/locator_assertions.rb', line 59 def not_to_be_hidden(timeout: nil) wrap_impl(@impl.not_to_be_hidden(timeout: unwrap_impl(timeout))) end |
#not_to_be_in_viewport(ratio: nil, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeInViewport].
65 66 67 |
# File 'lib/playwright_api/locator_assertions.rb', line 65 def (ratio: nil, timeout: nil) wrap_impl(@impl.(ratio: unwrap_impl(ratio), timeout: unwrap_impl(timeout))) end |
#not_to_be_visible(timeout: nil, visible: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toBeVisible].
71 72 73 |
# File 'lib/playwright_api/locator_assertions.rb', line 71 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 |
#not_to_contain_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toContainText].
77 78 79 |
# File 'lib/playwright_api/locator_assertions.rb', line 77 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 |
#not_to_have_accessible_description(name, ignoreCase: nil, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveAccessibleDescription].
83 84 85 |
# File 'lib/playwright_api/locator_assertions.rb', line 83 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 |
#not_to_have_accessible_error_message(errorMessage, ignoreCase: nil, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveAccessibleErrorMessage].
89 90 91 |
# File 'lib/playwright_api/locator_assertions.rb', line 89 def (errorMessage, ignoreCase: nil, timeout: nil) wrap_impl(@impl.(unwrap_impl(errorMessage), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end |
#not_to_have_accessible_name(name, ignoreCase: nil, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveAccessibleName].
95 96 97 |
# File 'lib/playwright_api/locator_assertions.rb', line 95 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 |
#not_to_have_attribute(name, value, ignoreCase: nil, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveAttribute].
101 102 103 |
# File 'lib/playwright_api/locator_assertions.rb', line 101 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 |
#not_to_have_class(expected, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveClass].
107 108 109 |
# File 'lib/playwright_api/locator_assertions.rb', line 107 def not_to_have_class(expected, timeout: nil) wrap_impl(@impl.not_to_have_class(unwrap_impl(expected), timeout: unwrap_impl(timeout))) end |
#not_to_have_count(count, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveCount].
113 114 115 |
# File 'lib/playwright_api/locator_assertions.rb', line 113 def not_to_have_count(count, timeout: nil) wrap_impl(@impl.not_to_have_count(unwrap_impl(count), timeout: unwrap_impl(timeout))) end |
#not_to_have_css(name, value, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveCSS].
119 120 121 |
# File 'lib/playwright_api/locator_assertions.rb', line 119 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 |
#not_to_have_id(id, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveId].
125 126 127 |
# File 'lib/playwright_api/locator_assertions.rb', line 125 def not_to_have_id(id, timeout: nil) wrap_impl(@impl.not_to_have_id(unwrap_impl(id), timeout: unwrap_impl(timeout))) end |
#not_to_have_js_property(name, value, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveJSProperty].
131 132 133 |
# File 'lib/playwright_api/locator_assertions.rb', line 131 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 |
#not_to_have_role(role, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveRole].
137 138 139 |
# File 'lib/playwright_api/locator_assertions.rb', line 137 def not_to_have_role(role, timeout: nil) wrap_impl(@impl.not_to_have_role(unwrap_impl(role), timeout: unwrap_impl(timeout))) end |
#not_to_have_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveText].
143 144 145 |
# File 'lib/playwright_api/locator_assertions.rb', line 143 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 |
#not_to_have_value(value, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveValue].
149 150 151 |
# File 'lib/playwright_api/locator_assertions.rb', line 149 def not_to_have_value(value, timeout: nil) wrap_impl(@impl.not_to_have_value(unwrap_impl(value), timeout: unwrap_impl(timeout))) end |
#not_to_have_values(values, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toHaveValues].
155 156 157 |
# File 'lib/playwright_api/locator_assertions.rb', line 155 def not_to_have_values(values, timeout: nil) wrap_impl(@impl.not_to_have_values(unwrap_impl(values), timeout: unwrap_impl(timeout))) end |
#not_to_match_aria_snapshot(expected, timeout: nil) ⇒ Object
The opposite of [method: LocatorAssertions.toMatchAriaSnapshot].
161 162 163 |
# File 'lib/playwright_api/locator_assertions.rb', line 161 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 |
#to_be_attached(attached: nil, timeout: nil) ⇒ Object
Ensures that Locator points to an element that is connected to a Document or a ShadowRoot.
Usage
expect(page.get_by_text("Hidden text")).to_be_attached()
173 174 175 |
# File 'lib/playwright_api/locator_assertions.rb', line 173 def to_be_attached(attached: nil, timeout: nil) wrap_impl(@impl.to_be_attached(attached: unwrap_impl(attached), timeout: unwrap_impl(timeout))) end |
#to_be_checked(checked: nil, indeterminate: nil, timeout: nil) ⇒ Object
Ensures the Locator points to a checked input.
Usage
from playwright.sync_api import expect
locator = page.get_by_label("Subscribe to newsletter")
expect(locator).to_be_checked()
188 189 190 |
# File 'lib/playwright_api/locator_assertions.rb', line 188 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 |
#to_be_disabled(timeout: nil) ⇒ Object
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
from playwright.sync_api import expect
locator = page.locator("button.submit")
expect(locator).to_be_disabled()
207 208 209 |
# File 'lib/playwright_api/locator_assertions.rb', line 207 def to_be_disabled(timeout: nil) wrap_impl(@impl.to_be_disabled(timeout: unwrap_impl(timeout))) end |
#to_be_editable(editable: nil, timeout: nil) ⇒ Object
Ensures the Locator points to an editable element.
Usage
from playwright.sync_api import expect
locator = page.get_by_role("textbox")
expect(locator).to_be_editable()
222 223 224 |
# File 'lib/playwright_api/locator_assertions.rb', line 222 def to_be_editable(editable: nil, timeout: nil) wrap_impl(@impl.to_be_editable(editable: unwrap_impl(editable), timeout: unwrap_impl(timeout))) end |
#to_be_empty(timeout: nil) ⇒ Object
Ensures the Locator points to an empty editable element or to a DOM node that has no text.
Usage
from playwright.sync_api import expect
locator = page.locator("div.warning")
expect(locator).to_be_empty()
237 238 239 |
# File 'lib/playwright_api/locator_assertions.rb', line 237 def to_be_empty(timeout: nil) wrap_impl(@impl.to_be_empty(timeout: unwrap_impl(timeout))) end |
#to_be_enabled(enabled: nil, timeout: nil) ⇒ Object
Ensures the Locator points to an enabled element.
Usage
from playwright.sync_api import expect
locator = page.locator("button.submit")
expect(locator).to_be_enabled()
252 253 254 |
# File 'lib/playwright_api/locator_assertions.rb', line 252 def to_be_enabled(enabled: nil, timeout: nil) wrap_impl(@impl.to_be_enabled(enabled: unwrap_impl(enabled), timeout: unwrap_impl(timeout))) end |
#to_be_focused(timeout: nil) ⇒ Object
Ensures the Locator points to a focused DOM node.
Usage
from playwright.sync_api import expect
locator = page.get_by_role("textbox")
expect(locator).to_be_focused()
267 268 269 |
# File 'lib/playwright_api/locator_assertions.rb', line 267 def to_be_focused(timeout: nil) wrap_impl(@impl.to_be_focused(timeout: unwrap_impl(timeout))) end |
#to_be_hidden(timeout: nil) ⇒ Object
Ensures that Locator either does not resolve to any DOM node, or resolves to a non-visible one.
Usage
from playwright.sync_api import expect
locator = page.locator('.my-element')
expect(locator).to_be_hidden()
282 283 284 |
# File 'lib/playwright_api/locator_assertions.rb', line 282 def to_be_hidden(timeout: nil) wrap_impl(@impl.to_be_hidden(timeout: unwrap_impl(timeout))) end |
#to_be_in_viewport(ratio: nil, timeout: nil) ⇒ Object
Ensures the Locator points to an element that intersects viewport, according to the intersection observer API.
Usage
from playwright.sync_api import expect
locator = page.get_by_role("button")
# Make sure at least some part of element intersects viewport.
expect(locator).to_be_in_viewport()
# Make sure element is fully outside of viewport.
expect(locator).not_to_be_in_viewport()
# Make sure that at least half of the element intersects viewport.
expect(locator).to_be_in_viewport(ratio=0.5)
302 303 304 |
# File 'lib/playwright_api/locator_assertions.rb', line 302 def (ratio: nil, timeout: nil) wrap_impl(@impl.(ratio: unwrap_impl(ratio), timeout: unwrap_impl(timeout))) end |
#to_be_visible(timeout: nil, visible: nil) ⇒ Object
Ensures that Locator points to an attached and visible DOM node.
To check that at least one element from the list is visible, use [method: Locator.first].
Usage
# A specific element is visible.
expect(page.get_by_text("Welcome")).to_be_visible()
# At least one item in the list is visible.
expect(page.get_by_test_id("todo-item").first).to_be_visible()
# At least one of the two elements is visible, possibly both.
expect(
page.get_by_role("button", name="Sign in")
.or_(page.get_by_role("button", name="Sign up"))
.first
).to_be_visible()
327 328 329 |
# File 'lib/playwright_api/locator_assertions.rb', line 327 def to_be_visible(timeout: nil, visible: nil) wrap_impl(@impl.to_be_visible(timeout: unwrap_impl(timeout), visible: unwrap_impl(visible))) end |
#to_contain_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) ⇒ Object
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
import re
from playwright.sync_api import expect
locator = page.locator('.title')
expect(locator).to_contain_text("substring")
expect(locator).to_contain_text(re.compile(r"\d messages"))
If you pass an array as an expected value, the expectations are:
- Locator resolves to a list of elements.
- Elements from a subset of this list contain text from the expected array, respectively.
- The matching subset of elements has the same order as the expected array.
- 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:
from playwright.sync_api import expect
# ✓ Contains the right items in the right order
expect(page.locator("ul > li")).to_contain_text(["Text 1", "Text 3", "Text 4"])
# ✖ Wrong order
expect(page.locator("ul > li")).to_contain_text(["Text 3", "Text 2"])
# ✖ No item contains this text
expect(page.locator("ul > li")).to_contain_text(["Some 33"])
# ✖ Locator points to the outer list element, not to the list items
expect(page.locator("ul")).to_contain_text(["Text 3"])
383 384 385 |
# File 'lib/playwright_api/locator_assertions.rb', line 383 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 |
#to_have_accessible_description(description, ignoreCase: nil, timeout: nil) ⇒ Object
Ensures the Locator points to an element with a given accessible description.
Usage
locator = page.get_by_test_id("save-button")
expect(locator).to_have_accessible_description("Save results to disk")
396 397 398 |
# File 'lib/playwright_api/locator_assertions.rb', line 396 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 |
#to_have_accessible_error_message(errorMessage, ignoreCase: nil, timeout: nil) ⇒ Object
Ensures the Locator points to an element with a given aria errormessage.
Usage
locator = page.get_by_test_id("username-input")
expect(locator).to_have_accessible_error_message("Username is required.")
409 410 411 |
# File 'lib/playwright_api/locator_assertions.rb', line 409 def (errorMessage, ignoreCase: nil, timeout: nil) wrap_impl(@impl.(unwrap_impl(errorMessage), ignoreCase: unwrap_impl(ignoreCase), timeout: unwrap_impl(timeout))) end |
#to_have_accessible_name(name, ignoreCase: nil, timeout: nil) ⇒ Object
Ensures the Locator points to an element with a given accessible name.
Usage
locator = page.get_by_test_id("save-button")
expect(locator).to_have_accessible_name("Save to disk")
422 423 424 |
# File 'lib/playwright_api/locator_assertions.rb', line 422 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 |
#to_have_attribute(name, value, ignoreCase: nil, timeout: nil) ⇒ Object
Ensures the Locator points to an element with given attribute.
Usage
from playwright.sync_api import expect
locator = page.locator("input")
expect(locator).to_have_attribute("type", "text")
437 438 439 |
# File 'lib/playwright_api/locator_assertions.rb', line 437 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 |
#to_have_class(expected, timeout: nil) ⇒ Object
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 or perform partial matches, use a regular expression:
Usage
<div class='middle selected row' id='component'></div>
from playwright.sync_api import expect
locator = page.locator("#component")
expect(locator).to_have_class(re.compile(r"(^|\\s)selected(\\s|$)"))
expect(locator).to_have_class("middle selected row")
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:
from playwright.sync_api import expect
locator = page.locator("list > .component")
expect(locator).to_have_class(["component", "component selected", "component"])
466 467 468 |
# File 'lib/playwright_api/locator_assertions.rb', line 466 def to_have_class(expected, timeout: nil) wrap_impl(@impl.to_have_class(unwrap_impl(expected), timeout: unwrap_impl(timeout))) end |
#to_have_count(count, timeout: nil) ⇒ Object
Ensures the Locator resolves to an exact number of DOM nodes.
Usage
from playwright.sync_api import expect
locator = page.locator("list > .component")
expect(locator).to_have_count(3)
481 482 483 |
# File 'lib/playwright_api/locator_assertions.rb', line 481 def to_have_count(count, timeout: nil) wrap_impl(@impl.to_have_count(unwrap_impl(count), timeout: unwrap_impl(timeout))) end |
#to_have_css(name, value, timeout: nil) ⇒ Object
Ensures the Locator resolves to an element with the given computed CSS style.
Usage
from playwright.sync_api import expect
locator = page.get_by_role("button")
expect(locator).to_have_css("display", "flex")
496 497 498 |
# File 'lib/playwright_api/locator_assertions.rb', line 496 def to_have_css(name, value, timeout: nil) wrap_impl(@impl.to_have_css(unwrap_impl(name), unwrap_impl(value), timeout: unwrap_impl(timeout))) end |
#to_have_id(id, timeout: nil) ⇒ Object
Ensures the Locator points to an element with the given DOM Node ID.
Usage
from playwright.sync_api import expect
locator = page.get_by_role("textbox")
expect(locator).to_have_id("lastname")
511 512 513 |
# File 'lib/playwright_api/locator_assertions.rb', line 511 def to_have_id(id, timeout: nil) wrap_impl(@impl.to_have_id(unwrap_impl(id), timeout: unwrap_impl(timeout))) end |
#to_have_js_property(name, value, timeout: nil) ⇒ Object
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
from playwright.sync_api import expect
locator = page.locator(".component")
expect(locator).to_have_js_property("loaded", True)
527 528 529 |
# File 'lib/playwright_api/locator_assertions.rb', line 527 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 |
#to_have_role(role, timeout: nil) ⇒ Object
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 = page.get_by_test_id("save-button")
expect(locator).to_have_role("button")
542 543 544 |
# File 'lib/playwright_api/locator_assertions.rb', line 542 def to_have_role(role, timeout: nil) wrap_impl(@impl.to_have_role(unwrap_impl(role), timeout: unwrap_impl(timeout))) end |
#to_have_text(expected, ignoreCase: nil, timeout: nil, useInnerText: nil) ⇒ Object
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
import re
from playwright.sync_api import expect
locator = page.locator(".title")
expect(locator).to_have_text(re.compile(r"Welcome, Test User"))
expect(locator).to_have_text(re.compile(r"Welcome, .*"))
If you pass an array as an expected value, the expectations are:
- Locator resolves to a list of elements.
- The number of elements equals the number of expected values in the array.
- 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:
from playwright.sync_api import expect
# ✓ Has the right items in the right order
expect(page.locator("ul > li")).to_have_text(["Text 1", "Text 2", "Text 3"])
# ✖ Wrong order
expect(page.locator("ul > li")).to_have_text(["Text 3", "Text 2", "Text 1"])
# ✖ Last item does not match
expect(page.locator("ul > li")).to_have_text(["Text 1", "Text 2", "Text"])
# ✖ Locator points to the outer list element, not to the list items
expect(page.locator("ul")).to_have_text(["Text 1", "Text 2", "Text 3"])
597 598 599 |
# File 'lib/playwright_api/locator_assertions.rb', line 597 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 |
#to_have_value(value, timeout: nil) ⇒ Object
Ensures the Locator points to an element with the given input value. You can use regular expressions for the value as well.
Usage
import re
from playwright.sync_api import expect
locator = page.locator("input[type=number]")
expect(locator).to_have_value(re.compile(r"[0-9]"))
613 614 615 |
# File 'lib/playwright_api/locator_assertions.rb', line 613 def to_have_value(value, timeout: nil) wrap_impl(@impl.to_have_value(unwrap_impl(value), timeout: unwrap_impl(timeout))) end |
#to_have_values(values, timeout: nil) ⇒ Object
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>
import re
from playwright.sync_api import expect
locator = page.locator("id=favorite-colors")
locator.select_option(["R", "G"])
expect(locator).to_have_values([re.compile(r"R"), re.compile(r"G")])
640 641 642 |
# File 'lib/playwright_api/locator_assertions.rb', line 640 def to_have_values(values, timeout: nil) wrap_impl(@impl.to_have_values(unwrap_impl(values), timeout: unwrap_impl(timeout))) end |
#to_match_aria_snapshot(expected, timeout: nil) ⇒ Object
Asserts that the target element matches the given accessibility snapshot.
Usage
page.goto("https://demo.playwright.dev/todomvc/")
expect(page.locator('body')).to_match_aria_snapshot('''
- heading "todos"
- textbox "What needs to be done?"
''')
656 657 658 |
# File 'lib/playwright_api/locator_assertions.rb', line 656 def to_match_aria_snapshot(expected, timeout: nil) wrap_impl(@impl.to_match_aria_snapshot(unwrap_impl(expected), timeout: unwrap_impl(timeout))) end |