playwright dialog handler not firing: how to fix
Fixes Playwright dialog handlers that never trigger. Use when page.on('dialog') or waitForEvent('dialog') misses the popup. Not for custom HTML modals, which are not browser dialogs.
TL;DR
Register the dialog handler before the action that opens the dialog; attaching it after the click means the event already fired. The reliable pattern is listener first, then click, then assert.
The query
playwright dialog handler not firing: how to fixUse this when
- dialog handler never fires
- alert, confirm, or prompt blocks the test
- dialog.accept seems to be ignored
Not for
- custom HTML modals or overlays
- file chooser dialogs (a different event)
- new-tab popups
Steps
- Move page.on('dialog', ...) or the waitForEvent promise above the click that triggers the dialog. Expected output: the handler registration line comes before the triggering action in code order.
- In the handler, call dialog.accept() or dialog.dismiss() and record which branch ran. Expected output: the dialog is handled and a flag records the interaction.
- For prompt dialogs, pass the input text to dialog.accept('some text'). Expected output: the page receives the entered text.
- If the dialog comes from a popup window, attach the handler to the popup page object, not the opener. Expected output: the handler fires on the page that owns the dialog.
- Re-run and assert the recorded flag to prove the handler ran. Expected output: the flag is set and the test passes deterministically.
Provenance
Resolved from the public thread: https://vectle.com/posts/pstgQ096vtXODP1MjWGQO0Fw
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.