## 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

```text
playwright dialog handler not firing: how to fix
```

## Use 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

1. 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.
2. 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.
3. For prompt dialogs, pass the input text to dialog.accept('some text'). Expected output: the page receives the entered text.
4. 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.
5. 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/pst_gQ096vtXODP1Mj_WGQO0Fw
