expect() takes a locator or a page and returns its assertions. Each one
checks again until it passes or its timeout runs out, then raises
AssertionError with the last value it saw. Text comparisons collapse runs of
whitespace, as Playwright’s do.
expect()
| Parameter | Type | Default | Description |
|---|---|---|---|
actual | Locator | Page | required | What to check. |
timeout | float | None | 5000 | Milliseconds, for every assertion made from this call. An assertion’s own timeout= wins. |
LocatorAssertions or PageAssertions. Raises TypeError for anything else.
.not_
Invert the next assertion: expect(locator).not_.to_be_visible().
set_default_timeout()
Change the default for every expect(), in milliseconds.
Locator assertions
Every method takestimeout= (milliseconds).
| Assertion | Passes when |
|---|---|
to_be_visible(*, visible=True) | The element is visible (or hidden, with visible=False). |
to_be_hidden() | It is hidden or absent. |
to_be_attached(*, attached=True) | It is in the document. |
to_be_enabled(*, enabled=True) | It is not disabled. |
to_be_disabled() | It is disabled. |
to_be_editable(*, editable=True) | It accepts typing. |
to_be_checked(*, checked=True) | A checkbox or radio is checked. |
to_be_focused() | It has focus. |
to_be_empty() | An input’s value, or the element’s text, is empty. |
to_have_text(expected, *, use_inner_text=False, ignore_case=False) | Its text equals expected. |
to_contain_text(expected, *, use_inner_text=False, ignore_case=False) | Its text contains expected. |
to_have_value(value) | An input’s value equals value. |
to_have_attribute(name, value) | The attribute equals value. |
to_have_id(id) | Its id equals id. |
to_have_class(expected) | Its class equals expected (or matches a regular expression). |
to_have_count(count) | The locator matches exactly count elements. |
to_have_css(name, value) | The computed style property equals value. |
expected and value are a string or a compiled regular expression. For
to_have_text and to_contain_text, a list checks every match in order:
text_content() unless use_inner_text=True.
Page assertions
| Assertion | Passes when |
|---|---|
to_have_url(url) | The URL equals url, or matches it as a regular expression. |
to_have_title(title) | The title equals title, or matches it as a regular expression. |
to_have_url takes an exact string or a regular expression, not a glob. To
wait for a glob such as "**/dashboard", use
page.wait_for_url().