alert(), confirm(), prompt() dialogs
By default, dialogs are auto-dismissed by Playwright, so you don't have to handle them. However, you can register a dialog handler before the action that triggers the dialog to either Dialog.AcceptAsync(promptText) or Dialog.DismissAsync() it.
page.Dialog += (_, dialog) => dialog.AcceptAsync();
As a result, the following snippet will never resolve:
page.Dialog += (_, dialog) => Console.WriteLine(dialog.Message);
await page.Locator("button").ClickAsync(); // Will hang here
If there is no listener for event Page.Dialog, all dialogs are automatically dismissed.
When Page.CloseAsync(options) is invoked with the truthy
runBeforeUnload value, the page runs its unload handlers. This is the only case when Page.CloseAsync(options) does not wait for the page to actually close, because it might be that the page stays open in the end of the operation.
You can register a dialog handler to handle the
beforeunload dialog yourself:
page.Dialog += (_, dialog) =>
await page.CloseAsync(runBeforeUnload: true);