← Back to library
Cypressnavigationintermediatev10+

Handle a browser alert or confirm dialog

Stub window.confirm to accept or dismiss a dialog without freezing the Cypress runner.

1it('accepts a confirm dialog before deleting', () => {
2  cy.visit('/settings');
3
4  // Register the handler BEFORE the action that triggers the dialog
5  cy.on('window:confirm', (message) => {
6    expect(message).to.include('Are you sure?');
7    return true; // return false to click Cancel
8  });
9
10  cy.get('[data-testid="delete-account-btn"]').click();
11  cy.contains('Account deleted').should('be.visible');
12});
#alert#confirm#dialog#window:confirm#stub