diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 69621ba52947..93a0126489ef 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -14,6 +14,7 @@ _Released 07/01/2025 (PENDING)_ - `@cypress/webpack-dev-server` and `@cypress/webpack-preprocessor` no longer support `webpack` version 4. Addresses [#31344](https://github.com/cypress-io/cypress/issues/31344). If you still need to use `webpack` version 4, please see our [migration guide](https://docs.cypress.io/app/references/migration-guide#Migrating-to-Cypress-150). - `@cypress/webpack-dev-server` no longer supports `webpack-dev-server` version 4. Addresses [#31605](https://github.com/cypress-io/cypress/issues/31605). If you still need to use `webpack-dev-server` version 4, please see our [migration guide](https://docs.cypress.io/app/references/migration-guide#Migrating-to-Cypress-150). - The `@cypress/webpack-batteries-included-preprocessor` no longer includes browser built-ins that were automatically provided by Webpack 4. To add built-ins manually, refer to the Webpack [resolve.fallback](https://webpack.js.org/configuration/resolve/#resolvefallback) documentation and the [`@cypress/webpack-batteries-included-preprocessor` README](../npm/webpack-batteries-included-preprocessor/README.md). Addresses [#31039](https://github.com/cypress-io/cypress/issues/31039). +- The application under test's `pagehide` event in Chromium browsers will no longer trigger Cypress's `window:unload` event. Addressed in [#31853](https://github.com/cypress-io/cypress/pull/31853). **Features:** diff --git a/packages/driver/cypress/e2e/commands/navigation.cy.js b/packages/driver/cypress/e2e/commands/navigation.cy.js index 433e8826004a..2afd8086266d 100644 --- a/packages/driver/cypress/e2e/commands/navigation.cy.js +++ b/packages/driver/cypress/e2e/commands/navigation.cy.js @@ -97,15 +97,13 @@ describe('src/cy/commands/navigation', () => { }) it('removes listeners', () => { - cy.log(Cypress.browser) - const unloadEvent = Cypress.browser.family === 'chromium' ? 'pagehide' : 'unload' const win = cy.state('window') const rel = cy.stub(win, 'removeEventListener') cy.reload().then(() => { expect(rel).to.be.calledWith('beforeunload') - expect(rel).to.be.calledWith(unloadEvent) + expect(rel).to.be.calledWith('unload') }) }) @@ -415,10 +413,8 @@ describe('src/cy/commands/navigation', () => { const rel = cy.stub(win, 'removeEventListener') cy.go('back').then(() => { - const unloadEvent = cy.browser.family === 'chromium' ? 'pagehide' : 'unload' - expect(rel).to.be.calledWith('beforeunload') - expect(rel).to.be.calledWith(unloadEvent) + expect(rel).to.be.calledWith('unload') }) }) }) diff --git a/packages/driver/src/cross-origin/cypress.ts b/packages/driver/src/cross-origin/cypress.ts index c6af33aad03c..f0434639b93f 100644 --- a/packages/driver/src/cross-origin/cypress.ts +++ b/packages/driver/src/cross-origin/cypress.ts @@ -220,13 +220,10 @@ const attachToWindow = (autWindow: Window) => { Cypress.specBridgeCommunicator.toPrimary('window:load', { url: remoteLocation.href }) cy.isStable(true, 'load') }, - onPageHide (e) { + onUnload (e) { cy.state('window', undefined) cy.state('document', undefined) - // unload is being actively deprecated/removed by chrome, so for - // compatibility, we are using `window`'s `pagehide` event as a proxy - // for the `window:unload` event that we emit. See: https://github.com/cypress-io/cypress/pull/29525 return Cypress.action('app:window:unload', e) }, onNavigation (...args) { diff --git a/packages/driver/src/cy/listeners.ts b/packages/driver/src/cy/listeners.ts index f12ced5e36f0..c31f6456fab0 100644 --- a/packages/driver/src/cy/listeners.ts +++ b/packages/driver/src/cy/listeners.ts @@ -61,7 +61,7 @@ type BoundCallbacks = { onSubmit: (e) => any onLoad: (e) => any onBeforeUnload: (e) => undefined | Promise - onPageHide: (e: PageTransitionEvent) => any + onUnload: (e: BeforeUnloadEvent) => any onNavigation: (...args) => any onAlert: (str) => any onConfirm: (str) => boolean @@ -93,18 +93,12 @@ export const bindToListeners = (contentWindow, callbacks: BoundCallbacks) => { await callbacks.onBeforeUnload(e) }) - // While we must move to pagehide for Chromium, it does not work for our - // needs in Firefox. Until that is addressed, only Chromium uses the pagehide - // event as a proxy for AUT unloads. - - const unloadEvent = Cypress.browser.family === 'chromium' ? 'pagehide' : 'unload' - - addListener(contentWindow, unloadEvent, (e) => { + addListener(contentWindow, 'unload', (e) => { // when we unload we need to remove all of the event listeners removeAllListeners() // else we know to proceed onwards! - callbacks.onPageHide(e) + callbacks.onUnload(e) }) addListener(contentWindow, 'hashchange', (e) => { diff --git a/packages/driver/src/cypress/cy.ts b/packages/driver/src/cypress/cy.ts index 516e74436d8f..3b85f5cb5565 100644 --- a/packages/driver/src/cypress/cy.ts +++ b/packages/driver/src/cypress/cy.ts @@ -1130,10 +1130,7 @@ export class $Cy extends EventEmitter2 implements ITimeouts, IStability, IAssert // doesn't trigger a confirmation dialog return undefined }, - onPageHide (e) { - // unload is being actively deprecated/removed by chrome, so for - // compatibility, we are using `window`'s `pagehide` event as a proxy - // for the `window:unload` event that we emit. See: https://github.com/cypress-io/cypress/pull/29525 + onUnload (e) { return cy.Cypress.action('app:window:unload', e) }, onNavigation (...args) {