Chapter 44: Dialogs and popups
Automation learning lab
Playwright Automation
Self Study Guide
Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.
Dialogs and popups
JavaScript dialogs block page execution until accepted or dismissed. Without a dialog listener Playwright automatically dismisses dialogs. When registering a listener, handle the dialog. A new tab or popup can be captured by creating the event promise before the triggering action.
How it works
Step-by-step walkthrough
- Create a confirmation button
- Register a dialog handler
- Click and assert the result
Example
import { test, expect } from '@playwright/test';
test('chapter 44 practice', async ({ page }) => {
await page.setContent('<button onclick="document.body.dataset.ok = confirm(\'Proceed?\')">Confirm</button>');
page.once('dialog', dialog => dialog.accept());
await page.getByRole('button').click();
await expect(page.locator('body')).toHaveAttribute('data-ok', 'true');
});Register popup and download listeners before clicking the trigger.
✎ Your practice task
Use dialog.dismiss() and verify false.
Knowledge check
When should a popup event promise be registered?
🌐 Continue learning
60-chapter learning path
Start at chapter 1, or choose any topic. Your progress is tracked separately for each chapter.
JavaScript fundamentals
TypeScript fundamentals
Playwright • setup & locators
Playwright • interactions & assertions
Playwright • framework & debugging
Playwright • real-world automation
Interactive practice lab
Try the controls below and observe the corresponding Playwright locator instructions. This browser demo does not execute Node.js or Playwright Test; copy the code to your local Playwright project to automate it.
Task manager demo
Suggested automation
Actions on the demo update these examples. In your local test use a page that contains the same controls.
Download a standalone lab page, then serve it locally (for example with Python's http.server). Navigate to it in a Playwright test.