Error messages¶
When an assertion fails, selenium-expect raises an AssertionError with a descriptive multi-line message.
Structure¶
Expected {entity} {condition}, but got {actual}
Expected: {expected}
Actual: {actual}
Element: {truncated_html}
Waited: {elapsed_ms}ms ({poll_count} polls at {polling_interval}s interval)
Message: {custom_message}
Timeline: [poll N: actual, ...]
Example¶
Expected <h1> to have text 'Hello, World!', but got Goodbye
Expected: Hello, World!
Actual: Goodbye
Element: <h1 id="greeting">Goodbye</h1>
Waited: 5001ms (10 polls at 0.5s interval)
Timeline: [poll 6: Goodbye, poll 7: Goodbye, poll 8: Goodbye, poll 9: Goodbye, poll 10: Goodbye]
Fields¶
| Field | Description |
|---|---|
entity |
Description of the target (e.g. <h1 id="greeting">, driver, ShadowRoot) |
condition |
What was expected (e.g. to have text 'Hello') |
expected |
Expected value |
actual |
Actual value at the last poll |
Element |
Truncated outerHTML of the element (first 200 chars) |
Waited |
Total time waited, poll count, and interval |
Message |
Custom message passed via expect(target, message="...") |
Timeline |
Last 5 poll results for debugging intermittent issues |
Custom messages¶
Pass a message to expect() for additional context:
Debug mode¶
Enable debug logging to see retry loop details in real time:
This prints poll count, elapsed time, and actual values to the selenium_expect logger.
Screenshots¶
Enable automatic screenshot capture on failure:
from selenium_expect import set_screenshot_on_failure
set_screenshot_on_failure(True, path="./screenshots/")
Screenshots are saved as screenshot_{timestamp}_{condition}.png.
Sensitive data in error output
Failure messages include the element's outerHTML and actual values
(texts, attribute values, cookie values, JS results). These may contain
tokens or personal data — keep test logs private, and avoid asserting
on secrets with descriptive messages if logs are shared.
Examples¶
Understanding the timeline¶
The timeline shows the last 5 poll results, which helps diagnose intermittent issues:
Expected <span id="status"> to have text 'Ready', but got Loading
Expected: Ready
Actual: Loading
Element: <span id="status">Loading</span>
Waited: 5001ms (10 polls at 0.5s interval)
Timeline: [poll 6: Loading, poll 7: Loading, poll 8: Loading, poll 9: Loading, poll 10: Loading]
If the value was changing during the wait, the timeline would show it:
Expected <span id="status"> to have text 'Ready', but got Loading
Expected: Ready
Actual: Loading
Element: <span id="status">Loading</span>
Waited: 5001ms (10 polls at 0.5s interval)
Timeline: [poll 6: Pending, poll 7: Loading, poll 8: Loading, poll 9: Loading, poll 10: Loading]
Using custom messages for context¶
# Add context to error messages
expect(
driver.find_element(By.ID, "checkout-btn"),
message="Checkout button should be enabled after cart has items"
).to_be_enabled()
expect(
driver,
message="Should redirect to dashboard after login"
).to_have_url_contains("/dashboard")
Debug mode output¶
When debug mode is enabled, each poll logs details on the selenium_expect logger (DEBUG level):
poll 1: passed=False actual=False — 0.4ms elapsed
poll 2: passed=False actual=False — 500.9ms elapsed
poll 3: passed=True actual=True — 1001.8ms elapsed
Screenshots on failure¶
from selenium_expect import set_screenshot_on_failure
# Enable with a custom path
set_screenshot_on_failure(True, path="./screenshots/")
# Now any assertion failure saves a screenshot
expect(element).to_be_visible(timeout=5)
# If this fails, you'll find: ./screenshots/screenshot_20240101_120000_to_be_visible.png