← Back to library
Playwrightnavigationintermediatev1.20+
Handle browser dialog (alert / confirm)
Listen for alert and confirm dialogs and accept or dismiss them programmatically.
1test('accepts a confirm dialog before deleting', async ({ page }) => {
2 // Register the handler BEFORE the action that triggers the dialog
3 page.on('dialog', async (dialog) => {
4 expect(dialog.message()).toContain('Are you sure?');
5 await dialog.accept(); // use dialog.dismiss() to click Cancel
6 });
7
8 await page.goto('/settings');
9 await page.getByRole('button', { name: 'Delete account' }).click();
10
11 await expect(page.getByText('Account deleted')).toBeVisible();
12});#dialog#alert#confirm#dismiss#accept