Browser confirm/alert handling silently stopped working as of Chrome 153
Current behavior
Recently we noticed that when running our e2e suite using cypress open (but not cypress run), any native browser dialogs (e.g. window.confirm(), window.alert()) were no longer being automatically dismissed by Cypress.
The confirm dialog would be shown and halt the run until it was manually dismissed by a human, which is obviously a problem from a browser automation perspective.
Desired behavior
Prior to the above, any native browser dialogs would be automatically dismissed; including where cy.on("window:confirm", () => false) was used to simulate a cancel of the dialog.
Test code to reproduce
Any app, Chrome >=153, cypress open.
Two cy.visit() commands, then assert String(window.confirm).includes("native code").
Alternatively, force it deterministically in cypress run by passing --enable-features=DeprecateUnload.
Cypress Version
16.0.0
Debug Logs
Other
Here's essentially the diagnosis of what is happening:
The handlers for confirm, alert etc. are registered in https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/listeners.ts
At the top of the module a variable is declared that stores whether the listeners have been registered or not:
let listenersAdded: boolean | null = null;The only code that resets this boolean to false is in the removeAllListeners function:
const removeAllListeners = () => {
listenersAdded = false
...
}Later in the module, the bindToListeners function is what sets up the various listeners, including the confirm and alert handlers:
export const bindToListeners = (contentWindow, callbacks: BoundCallbacks) => {
if (listenersAdded) { // <- early return if listeners already registered
return
}
removeAllListeners() // <- resets the listenersAdded flag
listenersAdded = true // <- sets the listenersAdded flag
...
addListener(contentWindow, 'unload', (e) => {
// when we unload we need to remove all of the event listeners
removeAllListeners()
// else we know to proceed onwards!
callbacks.onUnload(e)
})
...
contentWindow.alert = callbacks.onAlert
contentWindow.confirm = callbacks.onConfirm // <- confirm handler here
}Note that after bindToListeners is called once, it will always exit early unless something calls removeAllListeners to reset the listenersAdded flag.
And the only thing that calls removeAllListeners is..... the handler for the unload event.
Chrome has been progressively deprecating the unload event for over a year. Recently, we reached a point where Chrome 152 has the unload event deprecated for 80% of all sites; with the next stage being fully deprecated by September 22 2026.
So putting this altogether:
- Chrome no longer fires the
unloadevent - Therefore nothing in the above code ever resets the
listenersAddedflag after it is set once - This means that the confirm/alert handling is set on first cy.visit() (often
about:blank), but never again
Why cypress open and not cypress run?
cypress run uses a throwaway run-<pid> profile which has the unload event still enabled, where as cypress open uses a persistent profile where the unload event is disabled.
Source: cypress-io/cypress