Skip to main content
Version: 1.23

Assertions

Playwright gives you Web-First Assertions with convenience methods for creating assertions that will wait and retry until the expected condition is met.

Consider the following example:

from playwright.sync_api import Page, expect

def test_status_becomes_submitted(page: Page) -> None:
# ..
page.click("#submit-button")
expect(page.locator(".status")).to_have_text("Submitted")

Playwright will be re-testing the node with the selector .status until fetched Node has the "Submitted" text. It will be re-fetching the node and checking it over and over, until the condition is met or until the timeout is reached. You can pass this timeout as an option.

By default, the timeout for assertions is set to 5 seconds.

expect(locator).not_to_be_checked(**kwargs)

The opposite of expect(locator).to_be_checked(**kwargs).

expect(locator).not_to_be_disabled(**kwargs)

The opposite of expect(locator).to_be_disabled(**kwargs).

expect(locator).not_to_be_editable(**kwargs)

The opposite of expect(locator).to_be_editable(**kwargs).

expect(locator).not_to_be_empty(**kwargs)

The opposite of expect(locator).to_be_empty(**kwargs).

expect(locator).not_to_be_enabled(**kwargs)

The opposite of expect(locator).to_be_enabled(**kwargs).

expect(locator).not_to_be_focused(**kwargs)

The opposite of expect(locator).to_be_focused(**kwargs).

expect(locator).not_to_be_hidden(**kwargs)

The opposite of expect(locator).to_be_hidden(**kwargs).

expect(locator).not_to_be_visible(**kwargs)

The opposite of expect(locator).to_be_visible(**kwargs).

expect(locator).not_to_contain_text(expected, **kwargs)

  • expected <str|Pattern|List[str|Pattern]> Expected substring or RegExp or a list of those.#
  • ignore_case <bool> Whether to perform case-insensitive match. ignore_case option takes precedence over the corresponding regular expression flag if specified.#
  • timeout <float> Time to retry the assertion for.#
  • use_inner_text <bool> Whether to use element.innerText instead of element.textContent when retrieving DOM node text.#
  • returns: <NoneType>#

The opposite of expect(locator).to_contain_text(expected, **kwargs).

expect(locator).not_to_have_attribute(name, value, **kwargs)

The opposite of expect(locator).to_have_attribute(name, value, **kwargs).

expect(locator).not_to_have_class(expected, **kwargs)

The opposite of expect(locator).to_have_class(expected, **kwargs).

expect(locator).not_to_have_count(count, **kwargs)

  • count <int> Expected count.#
  • timeout <float> Time to retry the assertion for.#
  • returns: <NoneType>#

The opposite of expect(locator).to_have_count(count, **kwargs).

expect(locator).not_to_have_css(name, value, **kwargs)

The opposite of expect(locator).to_have_css(name, value, **kwargs).

expect(locator).not_to_have_id(id, **kwargs)

The opposite of expect(locator).to_have_id(id, **kwargs).

expect(locator).not_to_have_js_property(name, value, **kwargs)

  • name <str> Property name.#
  • value <Any> Property value.#
  • timeout <float> Time to retry the assertion for.#
  • returns: <NoneType>#

The opposite of expect(locator).to_have_js_property(name, value, **kwargs).

expect(locator).not_to_have_text(expected, **kwargs)

  • expected <str|Pattern|List[str|Pattern]> Expected substring or RegExp or a list of those.#
  • ignore_case <bool> Whether to perform case-insensitive match. ignore_case option takes precedence over the corresponding regular expression flag if specified.#
  • timeout <float> Time to retry the assertion for.#
  • use_inner_text <bool> Whether to use element.innerText instead of element.textContent when retrieving DOM node text.#
  • returns: <NoneType>#

The opposite of expect(locator).to_have_text(expected, **kwargs).

expect(locator).not_to_have_value(value, **kwargs)

The opposite of expect(locator).to_have_value(value, **kwargs).

expect(locator).not_to_have_values(values, **kwargs)

The opposite of expect(locator).to_have_values(values, **kwargs).

expect(locator).to_be_checked(**kwargs)

Ensures the Locator points to a checked input.

from playwright.sync_api import expect

locator = page.locator(".subscribe")
expect(locator).to_be_checked()

expect(locator).to_be_disabled(**kwargs)

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.

from playwright.sync_api import expect

locator = page.locator("button.submit")
expect(locator).to_be_disabled()

expect(locator).to_be_editable(**kwargs)

Ensures the Locator points to an editable element.

from playwright.sync_api import expect

locator = page.locator(".input")
expect(locator).to_be_editable()

expect(locator).to_be_empty(**kwargs)

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

from playwright.sync_api import expect

locator = page.locator("div.warning")
expect(locator).to_be_empty()

expect(locator).to_be_enabled(**kwargs)

Ensures the Locator points to an enabled element.

from playwright.sync_api import expect

locator = page.locator("button.submit")
expect(locator).to_be_enabled()

expect(locator).to_be_focused(**kwargs)

Ensures the Locator points to a focused DOM node.

from playwright.sync_api import expect

locator = page.locator('input')
expect(locator).to_be_focused()

expect(locator).to_be_hidden(**kwargs)

Ensures the Locator points to a hidden DOM node, which is the opposite of visible.

from playwright.sync_api import expect

locator = page.locator('.my-element')
expect(locator).to_be_hidden()

expect(locator).to_be_visible(**kwargs)

Ensures the Locator points to a visible DOM node.

from playwright.sync_api import expect

locator = page.locator('.my-element')
expect(locator).to_be_visible()

expect(locator).to_contain_text(expected, **kwargs)

  • expected <str|Pattern|List[str|Pattern]> Expected substring or RegExp or a list of those.#
  • ignore_case <bool> Whether to perform case-insensitive match. ignore_case option takes precedence over the corresponding regular expression flag if specified.#
  • timeout <float> Time to retry the assertion for.#
  • use_inner_text <bool> Whether to use element.innerText instead of element.textContent when retrieving DOM node text.#
  • returns: <NoneType>#

Ensures the Locator points to an element that contains the given text. You can use regular expressions for the value as well.

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"))

Note that if array is passed as an expected value, entire lists of elements can be asserted:

import re
from playwright.sync_api import expect

locator = page.locator("list > .list-item")
expect(locator).to_contain_text(["Text 1", "Text 4", "Text 5"])

expect(locator).to_have_attribute(name, value, **kwargs)

Ensures the Locator points to an element with given attribute.

from playwright.sync_api import expect

locator = page.locator("input")
expect(locator).to_have_attribute("type", "text")

expect(locator).to_have_class(expected, **kwargs)

Ensures the Locator points to an element with given CSS class.

from playwright.sync_api import expect

locator = page.locator("#component")
expect(locator).to_have_class(re.compile(r"selected"))

Note that if array is passed as an expected value, entire lists of elements can be asserted:

from playwright.sync_api import expect

locator = page.locator("list > .component")
expect(locator).to_have_class(["component", "component selected", "component"])

expect(locator).to_have_count(count, **kwargs)

  • count <int> Expected count.#
  • timeout <float> Time to retry the assertion for.#
  • returns: <NoneType>#

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

from playwright.sync_api import expect

locator = page.locator("list > .component")
expect(locator).to_have_count(3)

expect(locator).to_have_css(name, value, **kwargs)

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

from playwright.sync_api import expect

locator = page.locator("button")
expect(locator).to_have_css("display", "flex")

expect(locator).to_have_id(id, **kwargs)

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

from playwright.sync_api import expect

locator = page.locator("input")
expect(locator).to_have_id("lastname")

expect(locator).to_have_js_property(name, value, **kwargs)

  • name <str> Property name.#
  • value <Any> Property value.#
  • timeout <float> Time to retry the assertion for.#
  • returns: <NoneType>#

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.

from playwright.sync_api import expect

locator = page.locator(".component")
expect(locator).to_have_js_property("loaded", True)

expect(locator).to_have_text(expected, **kwargs)

  • expected <str|Pattern|List[str|Pattern]> Expected substring or RegExp or a list of those.#
  • ignore_case <bool> Whether to perform case-insensitive match. ignore_case option takes precedence over the corresponding regular expression flag if specified.#
  • timeout <float> Time to retry the assertion for.#
  • use_inner_text <bool> Whether to use element.innerText instead of element.textContent when retrieving DOM node text.#
  • returns: <NoneType>#

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

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, .*"))

Note that if array is passed as an expected value, entire lists of elements can be asserted:

from playwright.sync_api import expect

locator = page.locator("list > .component")
expect(locator).to_have_text(["Text 1", "Text 2", "Text 3"])

expect(locator).to_have_value(value, **kwargs)

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

import re
from playwright.sync_api import expect

locator = page.locator("input[type=number]")
expect(locator).to_have_value(re.compile(r"[0-9]"))

expect(locator).to_have_values(values, **kwargs)

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

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")])

expect(page).not_to_have_title(title_or_reg_exp, **kwargs)

The opposite of expect(page).to_have_title(title_or_reg_exp, **kwargs).

expect(page).not_to_have_url(url_or_reg_exp, **kwargs)

The opposite of expect(page).to_have_url(url_or_reg_exp, **kwargs).

expect(page).to_have_title(title_or_reg_exp, **kwargs)

Ensures the page has the given title.

import re
from playwright.sync_api import expect

# ...
expect(page).to_have_title(re.compile(r".*checkout"))

expect(page).to_have_url(url_or_reg_exp, **kwargs)

Ensures the page is navigated to the given URL.

import re
from playwright.sync_api import expect

# ...
expect(page).to_have_url(re.compile(".*checkout"))

expect(api_response).not_to_be_ok()

The opposite of expect(api_response).to_be_ok().

expect(api_response).to_be_ok()

Ensures the response status code is within [200..299] range.

import re
from playwright.sync_api import expect

# ...
expect(response).to_be_ok()