Skip to content

beforeunload when running Electron #796

Closed
@paulpruteanu

Description

@paulpruteanu

Current behavior:

When I run a test suite on a page that has a beforeunload event listener - hence, it triggers an alert -, first test goes fine, but the second test onwards fails to open the page.

This works fine using Chrome, as the Debugger console shows the following:

Blocked attempt to show a 'beforeunload' confirmation panel for a frame that never had a user gesture since its load. https://www.chromestatus.com/feature/5082396709879808

This makes CI headless tests unfeasible, as they fail for no consistent reason.

Desired behavior:

Electron built-in headless browser should prohibit beforeunload behaviour - same as Chrome does.

How to reproduce:

Open a page that has

window.addEventListener("beforeunload", function (e) {
    (e || window.event).returnValue = 'Are you sure you want to close this page?';
})

in two tests within the same test suite.

Test code:

index.html # started with any node server of your preference

<html>
<head>
    <title>
        Testing
    </title>
    <script>
        window.addEventListener("beforeunload", function (e) {
            (e || window.event).returnValue = 'Are you sure you want to close this page?';
        })
    </script>
</head>
<body>
<div id="helloWorld">Hello World!</div>
<button onClick="document.getElementById('helloWorld').style.display = 'none';">Click me!</button>
</body>
</html>
describe(`failing electron test suite`, () => {

        beforeEach(() => {
            cy.visit('http://127.0.0.1:8080/index.html')
        })
        it('sees hello and clicks to hide', () => {
            cy.contains('Hello').should('be.visible')
            cy.get('button').click();
        })

        it('should see world opening the page again', () => {
            cy.contains('World').should('be.visible')
        })

})

Additional Info (images, stack traces, etc)

I managed to fix this by exposing the listener in window, and run

cy.window()
    .then(win => win.myListener && win.removeEventListener('beforeunload', win.myListener))

straight after

cy.visit('<url>')
  • Operating System: MacOS Sierra 10.12.6 (16G29)
  • Cypress Version: 0.20.1
  • Failing Browser Version: Electron 53
  • Working Browser Version: Chrome 61.0.3163.100

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions