In one of my tests I need to assert message in browser dialog. I created dialog handler in test
page.on('dialog', BasePage.dialog_handle)
page
is default playwright fixture.
The handler itself is
def dialog_handle(dialog):
message = dialog.message
dialog.dismiss()
Here is full example of the test with dialog on the page
from playwright.sync_api import Page
def dialog_handle(dialog):
message = dialog.message
dialog.dismiss()
def test_dialog(page):
page.goto('https://dialog.antonzimaiev.repl.co')
page.on('dialog', dialog_handle)
page.get_by_role("button", name="Диалог Alert").click()
How can I retrieve dialog.message to assert it using pytest?
This can be solved with some scoping readjustments:
There's nothing special about pytest per se here--you can drop the
test_dialog
function body right into a pytest test case and use assertions from that, or any other testing library you like.Race conditions aren't an issue here since the dialog blocks the script after the
.click()
operation until it's dismissed.This pattern can be factored to be reusable across tests or in a POM, if that's a concern. For example: