cypress: Electron browser hangs/errors on apps with window.onbeforeunload alerts
Current behavior:
When running a test using the Electron browser on a webpage that contains a window.onbeforeunload script and trying to navigate away, Cypress will fail to load the next page. The test will eventually fail after a PageLoadTimeout exception. This occurs when using any kind of navigation such as cy.reload(), cy.visit(), or interacting with an element that contains an href linking to somewhere else.
Test run with Electron 59:

Desired behavior:
Test run with Chrome 67:

Steps to reproduce:
describe('Electron 59 alert prompt bug', function() {
it('Should navigate away from the page', function() {
cy.on('window:confirm', function() {
return true;
});
cy.visit('http://localhost:8080/');
cy.visit('http://localhost:8080/form');
});
it('Should reload the page', function() {
cy.visit('http://localhost:8080/');
cy.reload();
});
});
The page under test needs to contain a window.onbeforeunload function which alters the returnValue. To reproduce, I use http-server to serve a barebones HTML file
<!DOCTYPE html>
<script>
window.onbeforeunload = function (e) {
e.returnValue = 'Dialog';
return;
}
</script>
Versions
Cypress: 3.0.2
MacOS: High Sierra 10.13.5
Browsers: Chrome 67, Electron 59
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 33
- Comments: 49 (14 by maintainers)
Commits related to this issue
- Use Chrome as the Cypress browser See https://github.com/cypress-io/cypress/issues/2118 — committed to learning-with-data/dataland-gui by deleted user 4 years ago
- Add code coverage plugin for Cypress Also changed the browser for tests to Chrome due to https://github.com/cypress-io/cypress/issues/2118 — committed to learning-with-data/dataland-web by deleted user 4 years ago
- PW-34 Workaround for https://github.com/cypress-io/cypress/issues/2118 — committed to P3K-TEAM/prezentacny-web-timu by MCFreddie777 3 years ago
- try to fix `CypressError: Timed out after waiting `60000ms` for your remote page to load.` cf https://github.com/openwhyd/openwhyd/runs/4482557359?check_suite_focus=true#step:8:214 by using chrome bro... — committed to openwhyd/openwhyd by adrienjoly 3 years ago
- fix(tests): Cypress errors (#501) * try to fix `Unable to locate executable file` * try to load env file using a github action * try to fix `/entrypoint.sh: export: line 1: -o: bad variable nam... — committed to openwhyd/openwhyd by adrienjoly 3 years ago
- Remove onbeforeunload callback in test - Implements a workaround described here: https://github.com/cypress-io/cypress/issues/2118 — committed to tapestry-tool/tapestry-wp by zifgu 2 years ago
- Work around Cypress not handling beforeunload-related prompts * Our app registers beforeunload event listener e.g. when editing a native SQL question. * Cypress does not automatically close the brows... — committed to metabase/metabase by kamilmielnik 8 months ago
- Add custom warning message when leaving creating an SQL question (#34696) * Update isNavigationAllowed logic to prevent new model creation * Add simple model creation test * Remove identifier ... — committed to metabase/metabase by kamilmielnik 8 months ago
- Use Cypress event listener for `beforeunload` globally (#38264) https://github.com/cypress-io/cypress/issues/2118 — committed to metabase/metabase by nemanjaglumac 5 months ago
- Use Cypress event listener for `beforeunload` globally (#38264) https://github.com/cypress-io/cypress/issues/2118 — committed to metabase/metabase by nemanjaglumac 5 months ago
- Use Cypress event listener for `beforeunload` globally (#38264) (#38266) https://github.com/cypress-io/cypress/issues/2118 Co-authored-by: Nemanja Glumac <31325167+nemanjaglumac@users.noreply.github... — committed to metabase/metabase by metabase-bot[bot] 5 months ago
- Use Cypress event listener for `beforeunload` globally (#38264) https://github.com/cypress-io/cypress/issues/2118 — committed to metabase/metabase by nemanjaglumac 5 months ago
- Use Cypress event listener for `beforeunload` globally (#38264) https://github.com/cypress-io/cypress/issues/2118 — committed to metabase/metabase by nemanjaglumac 5 months ago
I am reopening this issue. While this was partially addressed in https://github.com/cypress-io/cypress/pull/5603 in 3.6.1 release by fixing the hanging while running in Electron (aka - the process would never exit), this did not actually fix the error portion of the issue.
Problem
Any application that has a defined
onbeforeunloadhandler that triggers an alert causes Cypress page load to time out within the Electron browser (not Chrome). Typically this is only noticed in CI since Electron is the chosen browser by default. Resulting in the error below:To reproduce
Example 1
index.htmlspec.jsExample 2
Workaround
There are 2 possible workarounds.
--browserflag duringcypress runor choosing Chrome duringcypress open.OR
support/index.jsfile. This will prevent the prompts from occurring - and not hang Electron.Started encountering this problem in our CI build. For now going to workaround by monkey patching
window.addEventListener:There is also a situation where, where after visiting the site above, this will exhibit as a Page Load timeout.
Failing test code below:
Electron will not display this error in the Console when this is happening also (if you have not manually interacted with the page)
Workaround
There are 2 possible workarounds.
--chromeflag duringcypress runor choosing Chrome duringcypress open.OR
support/index.jsfile. This will prevent the prompts from occurring - and not hang Electron.I was able to work around the issue with the following code in the test:
@melibe23 No updates on this. It is still an issue.
Just chiming in here to say we have an issue within our tests because Cypress doesn’t properly handle the
window.onbeforeunloadevent in Electron w/ Chromium 59, but works perfectly fine using Chrome 70 running the test runner. In our Jenkins env using docker, it also experiences the same issue with thecypress/base:10image.My guess is that the version of Chromium used by Electron (59) that ships with Cypress currently (3.1.0) has some major bugs and issues. I see #2559 is acknowledged, any ETA when that might get rolled out? I imagine this would fix a swath of issues being worked around due to an old version of Chromium.
Ya’ll doin’ great work though, I really appreciate Cypress 😃
Cypress:
3.1.0macOS:10.14.1Browsers:Electron 59,Chrome 70@valscion Commenting certainly doesn’t make Cypress less interested in fixing an issue, but for longstanding issues, we do prioritize them based on impact and effort to fix and this is certainly a known issue in our backlog to be prioritized against other work.
There’s this other issue with onbeforeunload alerts that’s popped up and made this prevelant within Chrome browsers which has put this somewhat on our radar. https://github.com/cypress-io/cypress/issues/28553 But I’m not sure they can both be addressed together or not.
@adnanerlansyah403 No, this hasn’t been fixed yet. It’s on a list of bugs that we’d like to prioritize fixing though, if we find the time.
This worked for me
Team any idea if this will get fixed any time soon? This blocker happens in chrome browser too (Both headless & normal).
I have pushed another example https://github.com/cypress-io/cypress-test-tiny/tree/test-unload that shows the problem in Electron 61 (Chrome passes, despite showing an error). The website in question is http://webdriverjsdemo.github.io/leave/
and has the following script
The error displayed in the browser console:
This error has docs in https://www.chromestatus.com/feature/5082396709879808 and probably is not disabled in Electron
Funnily, you cannot close the Electron browser in GUI mode - it just keeps showing the error
Running
cypress run --headedshows the error that happens on CI - the test keeps runningPutting the code from workaround number 2 into my beforeEach() block solved this for me.
This issue is keeping me from using
cy.origin(), since when I click the href to navigate to an external site, I can see the browser is loading the page, but cypress is still failing because of the page load timeout error. Unable to post a reproducer since it’s proprietary code but here’s an excerpt of what I’m doing:According to the docs, the above code should work, but this page load timeout error is keeping me from using it.
Please suggest for any other workarounds and I shall try it!! Thanks in advance!!!
Thanks for the quick response. Tried the below workaround, however still not working.
Still the same issue occurs. Accessing an URL and throws one pop-up since one file couldn’t be loaded. After that, even after closing the pop-up the tool shows ‘Loading’ progress and keeps waiting for the page to load and throws the below error.
If you’re using
window.onbeforeunload=function() {...}instead ofwindow.addEventListener('beforeunload', function() {...}, you can use this workaround: