expect.poll()¶
expect.poll(fn) wraps any zero-arg callable and provides fluent assertion methods that retry until the function's return value satisfies the condition or the timeout expires.
Usage¶
from selenium_expect import expect
expect.poll(lambda: driver.execute_script("return document.readyState")).to_equal("complete")
Available methods¶
to_equal(expected)¶
Asserts fn() == expected.
to_be_truthy()¶
Asserts bool(fn()) is True.
to_be_falsy()¶
Asserts bool(fn()) is False.
to_be_none()¶
Asserts fn() is None.
to_contain(expected)¶
Asserts expected in fn().
to_match(pattern)¶
Asserts re.search(pattern, str(fn())) finds a match.
to_be_greater_than(expected)¶
Asserts fn() > expected.
to_be_less_than(expected)¶
Asserts fn() < expected.
expect.poll(lambda: driver.execute_script("return document.querySelectorAll('.error').length")).to_be_less_than(1)
to_be_in_list(expected)¶
Asserts fn() in expected.
to_have_length(expected)¶
Asserts len(fn()) == expected.
Timeout and polling¶
expect.poll(fn, timeout=10, polling=0.2).to_equal("done")
expect.poll(fn, polling=[0.1, 0.2, 0.5]).to_equal("done")
Standalone poll()¶
You can also use the standalone poll function:
from selenium_expect import poll
poll(lambda: driver.execute_script("return document.readyState"), timeout=10).to_equal("complete")
More examples¶
Waiting for an API call to complete¶
# Poll a JavaScript variable that's set when an API call finishes
expect.poll(
lambda: driver.execute_script("return window.apiResponse?.status"),
timeout=30,
polling=0.5,
).to_equal("success")
Waiting for a specific number of elements¶
from selenium.webdriver.common.by import By
# Wait until at least 10 search results are loaded
expect.poll(
lambda: len(driver.find_elements(By.CSS_SELECTOR, ".search-result")),
timeout=15,
).to_be_greater_than(9)
Waiting for a URL to match a pattern¶
import re
# Wait for a redirect to complete
expect.poll(lambda: driver.current_url).to_match(r"https://app\.example\.com/dashboard")
Waiting for a cookie to be set¶
# Wait for a cookie to appear
expect.poll(
lambda: driver.get_cookie("auth_token"),
timeout=10,
).to_be_truthy()
Waiting for page to be fully loaded¶
# Wait for document.readyState to be 'complete'
expect.poll(
lambda: driver.execute_script("return document.readyState"),
timeout=30,
polling=[0.1, 0.2, 0.5, 1.0],
).to_equal("complete")
# Also verify no pending AJAX requests (jQuery)
expect.poll(
lambda: driver.execute_script("return jQuery.active"),
timeout=10,
).to_equal(0)