Alert assertions¶
expect(alert) returns an ExpectAlert instance, which provides assertions for JavaScript Alert objects.
Presence¶
to_be_present()¶
Asserts that the alert is present (accessing alert.text does not raise).
Selenium API: driver.switch_to.alert (no exception)
Example:
from selenium.webdriver.common.alert import Alert
alert = driver.switch_to.alert
expect(alert).to_be_present()
Negation:
Text¶
to_have_text(text)¶
Asserts that alert.text equals text.
Selenium API: alert.text
Parameters:
text(str): Expected alert text.
Example:
to_have_text_contains(text)¶
Asserts that text is a substring of alert.text.
Selenium API: text in alert.text
Parameters:
text(str): Expected substring.
Example:
to_have_text_matches(pattern)¶
Asserts that re.search(pattern, alert.text) finds a match.
Selenium API: re.search(pattern, alert.text)
Parameters:
pattern(str): Regular expression pattern.
Example:
Tips and common patterns¶
Handling confirmation dialogs¶
from selenium.webdriver.common.by import By
# Trigger a confirmation dialog
driver.find_element(By.ID, "delete-button").click()
# Wait for the alert to appear
alert = driver.switch_to.alert
expect(alert).to_be_present(timeout=5)
# Verify the alert text
expect(alert).to_have_text("Are you sure you want to delete this item?")
# Accept the confirmation
alert.accept()
# Verify the alert is gone
expect(alert).not_.to_be_present(timeout=3)
Handling prompt dialogs¶
# Trigger a prompt dialog
driver.find_element(By.ID, "rename-button").click()
alert = driver.switch_to.alert
expect(alert).to_be_present()
# Verify the prompt message
expect(alert).to_have_text_contains("Enter new name")
# Provide input and accept
alert.send_keys("New Name")
alert.accept()
Verifying alert text with patterns¶
alert = driver.switch_to.alert
expect(alert).to_be_present()
# Exact match
expect(alert).to_have_text("Delete file 'report.pdf'?")
# Partial match — useful when the filename is dynamic
expect(alert).to_have_text_contains("Delete file")
# Regex match — extract dynamic content
expect(alert).to_have_text_matches(r"Delete file '(.+)'")