Skip to content
CodeKruPro

Handle Alert Dialog

42/70

Handle Alert Dialog

In Playwright, dialogs are handled via event listeners. Register a handler with page.on('dialog', handler) before the action that triggers the dialog.

dialog.accept() clicks OK, dialog.dismiss() clicks Cancel.

On the Alerts page, clicking "Show Alert" triggers a simple alert.

Task:

  1. Navigate to https://testkru.com/Interactions/Alerts
  2. Set up a dialog handler that prints the message and accepts
  3. Click the "Show Alert" button

Expected output:

Dialog message: This is a simple alert!
Dialog accepted