Electron browser hangs/errors on apps with window.onbeforeunload alerts
See original GitHub issueCurrent 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
Issue Analytics
- State:
- Created 5 years ago
- Reactions:28
- Comments:35 (10 by maintainers)
Top Results From Across the Web
Developers - Electron browser hangs/errors on apps with ...
Electron browser hangs/errors on apps with window.onbeforeunload alerts.
Read more >Confirm beforeunload in Electron - javascript - Stack Overflow
It seems to be working fine for electron apps. It works slightly different from 'beforeunload' as it can't prevent closing window/tab, ...
Read more >Wassim Chegham on Twitter: "Is anyone having issues with ...
Electron browser hangs/errors on apps with window.onbeforeunload alerts · Issue #2118 · cypress-i... Current behavior: When running a test ...
Read more >window.onbeforeunload and Cypress - Gleb Bahmutov
The application code uses window.onbeforeunload callback to ask the user to ... Cypress can prevent the user prompt in the Electron browser, ...
Read more >onbeforeunload Event - W3Schools
The default message that appears in the confirmation box, is different in different browsers. However, the standard message is something like "Are you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
onbeforeunload
handler 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.html
spec.js
Example 2
Workaround
There are 2 possible workarounds.
--browser
flag duringcypress run
or choosing Chrome duringcypress open
.OR
support/index.js
file. This will prevent the prompts from occurring - and not hang Electron.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.
--chrome
flag duringcypress run
or choosing Chrome duringcypress open
.OR
support/index.js
file. This will prevent the prompts from occurring - and not hang Electron.