Practice Widgets Native dialogs
alert · confirm · prompt
Widget practice · Dialogs

Native dialogs & modal stacks

Three browser-native dialogs (alert / confirm / prompt) plus a custom modal that supports stacking and Escape-to-close. Native dialogs are intercepted by Playwright via page.on('dialog', ...); custom modals are just DOM you query normally.

① Browser-native dialogs

These call the browser's alert() / confirm() / prompt(). Playwright must register a dialog handler before clicking.

data-testid=show-alert / show-confirm / show-prompt / show-beforeunload handler=page.on('dialog', d => d.accept(...) | d.dismiss()) output=data-testid=dialog-output
No native dialog interaction yet.

② Custom modal — focus trap + Escape

A regular DOM modal — visible via data-testid="modal-confirm". Click Open another to stack a second one and confirm Playwright finds the topmost.

data-testid=open-modal / open-stacked / modal-confirm / modal-stacked / modal-cancel / modal-confirm-action role=dialog · aria-modal=true keys=Escape closes top modal
Mini practice · Classic 3-button drill

JavaScript Alerts

The textbook drill — three buttons, three native dialogs, one result line. Alert · Confirm · Prompt. Hook a Playwright page.on('dialog', …) handler before the click, then assert the data-testid="alerts-result" text matches.

Click the button

Each button fires its native dialog. The result line updates after you accept / dismiss / enter text.

id=js-alert · js-confirm · js-prompt · alerts-result data-testid=js-alert / js-confirm / js-prompt / alerts-result messages: 'I am a JS Alert' · 'I am a JS Confirm' · 'I am a JS prompt'

Result: