Skip to main content
Back to Blog
Guide
2026-04-01

Playwright Dialog Handling Guide: Alerts, Confirms, and Prompts

Learn Playwright dialog handling for alerts, confirms, prompts, and stable tests that do not freeze on native dialogs.

If a test suddenly freezes around a destructive action, a native browser dialog is often the reason. That is why playwright dialog handling is a classic troubleshooting query: alerts, confirms, and prompts behave differently from regular DOM modals and need explicit handling.

Key Takeaways

  • Listen for dialogs before triggering the action that opens them.
  • Accept or dismiss the dialog explicitly and assert on the post-dialog result.
  • Do not confuse native dialogs with application modals rendered in the DOM.
  • Use prompt text handling deliberately so the test makes the business behavior obvious.

Why This Topic Matters in 2026

  • Dialogs block page execution until they are accepted or dismissed.
  • Delete flows, unsaved changes warnings, and browser-native prompts still appear in many real products.
  • The Playwright CLI can reproduce dialog behavior quickly with dialog-accept and dialog-dismiss while you debug the exact sequence.

Practical Workflow

Step 1: Handle destructive confirms explicitly

This keeps the test clear and prevents the page from stalling while the dialog waits for input.

page.once('dialog', async (dialog) => {
  expect(dialog.type()).toBe('confirm');
  expect(dialog.message()).toContain('Delete project');
  await dialog.accept();
});

await page.getByRole('button', { name: 'Delete project' }).click();

Step 2: Use prompt values intentionally

If a prompt asks for a reason, ticket number, or confirmation text, send the value that matches the real user flow.

page.once('dialog', async (dialog) => {
  expect(dialog.type()).toBe('prompt');
  await dialog.accept('QA automation cleanup');
});

Step 3: Reproduce dialog behavior quickly in the CLI

This is useful when you want to see whether the issue is the native dialog, the follow-up action, or the surrounding page state.

playwright-cli open https://example.com/settings
playwright-cli click e7
playwright-cli dialog-accept
playwright-cli snapshot

Where the Playwright CLI Skill Fits

This is exactly where Playwright CLI Browser Automation adds value. The skill gives your agent stable guidance for snapshots, uploads, downloads, tab handling, tracing, screenshots, PDFs, and fast browser investigation without forcing you to reinvent the command flow every time.

If you are building out a broader QA workflow, keep the skill installed and pair it with the wider QASkills.sh skills directory catalog so your agent can switch between browser automation, API testing, CI, accessibility, and reporting with less context loss.

Common Mistakes to Avoid

  • Waiting for the click to finish while a native dialog is blocking the page.
  • Asserting on DOM modal content when the app actually opened a browser dialog.
  • Accepting every dialog blindly without checking the dialog message or type.
  • Skipping the post-dialog assertion that proves the destructive or confirmation path really completed.

Related Reading on QASkills.sh

Conclusion

Dialog handling is straightforward once you treat native dialogs as a separate browser primitive. Listen first, respond explicitly, and verify the business outcome after the dialog closes.

Playwright Dialog Handling Guide: Alerts, Confirms, and Prompts | QASkills.sh