import re
from halyard import expect

expect(page.get_by_role("status")).to_have_text("Saved")
expect(page.locator(".todo")).to_have_count(3)
expect(page).to_have_url(re.compile(r"/dashboard$"))
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()

expect(actual, *, timeout=None)
ParameterTypeDefaultDescription
actualLocator | PagerequiredWhat to check.
timeoutfloat | None5000Milliseconds, for every assertion made from this call. An assertion’s own timeout= wins.
Returns 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.
from halyard.expect import set_default_timeout
set_default_timeout(10_000)

Locator assertions

Every method takes timeout= (milliseconds).
AssertionPasses 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:
expect(page.locator(".todo")).to_have_text(["Buy milk", "Walk dog"])
Text is text_content() unless use_inner_text=True.

Page assertions

AssertionPasses 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().