TestingDocs.com logoTestingDocs.com
Automation learning lab
TestingDocs • Self-paced course

Playwright Automation
Self Study Guide

Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.

60 chaptersBeginner friendlyHands-on practice
Playwright / Chapter 44 of 60~18 min • Beginner
Chapter 44Playwright

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

  1. Create a confirmation button
  2. Register a dialog handler
  3. Click and assert the result

Example

TypeScript
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');
});
Expected result: Confirmation accepted
💡 Point to remember
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