Dialogs
Native browser dialogs — alert, confirm, prompt — are invisible to locators. You handle them through page events, not clicks. Get this wrong and the test hangs forever.
Why native dialogs are different
When JavaScript calls window.alert(), window.confirm(), or window.prompt(), the browser opens a native OS dialog — not a DOM element. Playwright can't find it with getByRole or any locator because it's not in the page's HTML at all.
By default, Playwright auto-dismisses all dialogs. For alert that means clicking OK. For confirm it means clicking Cancel (returns false). For prompt it means clicking Cancel (returns null). If your code checks the return value of confirm() and does something on true, the default behavior will break that flow.
Handling alert, confirm, prompt
Register a dialog event handler on the page. The handler fires when any dialog opens. The critical detail: register it BEFORE the action that triggers the dialog. If you register after, the dialog might appear and auto-close before your handler is attached.
beforeunload — unsaved changes warning
The beforeunload event fires when the user tries to leave the page with unsaved changes. The browser shows "Are you sure you want to leave?" — a dialog you can't suppress with CSS or locators. Handle it the same way: register before the action that navigates away.