From d70ffea0bf0d5df72a949c7d1a44030f4b39dec1 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Tue, 7 Nov 2023 07:37:12 -0600 Subject: [PATCH] fix: runIfWaitingForDebugger when targets are reloaded after crashing (#28254) --- cli/CHANGELOG.md | 1 + packages/server/lib/browsers/browser-cri-client.ts | 11 +++++++++++ packages/server/lib/browsers/cdp_automation.ts | 2 +- .../projects/e2e/cypress/e2e/web_worker.cy.js | 9 +++++++++ system-tests/test/web_worker_spec.js | 4 ++-- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 7af27d0c96e3..ae74737aee93 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -17,6 +17,7 @@ _Released 11/7/2023 (PENDING)_ - Fixed an issue with 'other' targets (e.g. pdf documents embedded in an object tag) not fully loading. Fixes [#28228](https://github.com/cypress-io/cypress/issues/28228) and [#28162](https://github.com/cypress-io/cypress/issues/28162). - Fixed an issue where network requests made from tabs/windows other than the main Cypress tab would be delayed. Fixes [#28113](https://github.com/cypress-io/cypress/issues/28113). - Stopped processing CDP events at the end of a spec when Test Isolation is off and Test Replay is enabled. Addressed in [#28213](https://github.com/cypress-io/cypress/pull/28213). +- Fixed a regression in [`13.3.3`](https://docs.cypress.io/guides/references/changelog/13.3.3) where Cypress would hang on loading shared workers when using `cy.reload` to reload the page. Fixes [#28248](https://github.com/cypress-io/cypress/issues/28248). ## 13.4.0 diff --git a/packages/server/lib/browsers/browser-cri-client.ts b/packages/server/lib/browsers/browser-cri-client.ts index 8d11b913afed..bc2c85fdb0d1 100644 --- a/packages/server/lib/browsers/browser-cri-client.ts +++ b/packages/server/lib/browsers/browser-cri-client.ts @@ -263,6 +263,17 @@ export class BrowserCriClient { this._onTargetDestroyed({ browserClient, browserCriClient, browserName, event, onAsynchronousError }) }) + browserClient.on('Inspector.targetReloadedAfterCrash', async (event, sessionId) => { + try { + // Things like service workers will effectively crash in terms of CDP when the page is reloaded in the middle of things + // We will still auto attach in this case, but we need to runIfWaitingForDebugger to get the page back to a running state + await browserClient.send('Runtime.runIfWaitingForDebugger', undefined, sessionId) + } catch (error) { + // it's possible that the target was closed before we can run. If so, just ignore + debug('error running Runtime.runIfWaitingForDebugger:', error) + } + }) + await Promise.all(promises) } diff --git a/packages/server/lib/browsers/cdp_automation.ts b/packages/server/lib/browsers/cdp_automation.ts index df65885c0a2d..ff0992a284a5 100644 --- a/packages/server/lib/browsers/cdp_automation.ts +++ b/packages/server/lib/browsers/cdp_automation.ts @@ -142,7 +142,7 @@ export const normalizeResourceType = (resourceType: string | undefined): Resourc export type SendDebuggerCommand = (message: T, data?: ProtocolMapping.Commands[T]['paramsType'][0], sessionId?: string) => Promise -export type OnFn = (eventName: T, cb: (data: ProtocolMapping.Events[T][0]) => void) => void +export type OnFn = (eventName: T, cb: (data: ProtocolMapping.Events[T][0], sessionId?: string) => void) => void export type OffFn = (eventName: string, cb: (data: any) => void) => void diff --git a/system-tests/projects/e2e/cypress/e2e/web_worker.cy.js b/system-tests/projects/e2e/cypress/e2e/web_worker.cy.js index 997341952fd0..6f93562dc5f3 100644 --- a/system-tests/projects/e2e/cypress/e2e/web_worker.cy.js +++ b/system-tests/projects/e2e/cypress/e2e/web_worker.cy.js @@ -32,3 +32,12 @@ it('loads web workers', { defaultCommandTimeout: 1900 }, () => { .then(webWorker) .then(sharedWorker) }) + +// Timeout of 1900 will ensure that the proxy correlation timeout is not hit +it('reloads web workers', { defaultCommandTimeout: 1900 }, () => { + cy.visit('https://localhost:1515/web_worker.html') + + cy.reload() + .then(webWorker) + .then(sharedWorker) +}) diff --git a/system-tests/test/web_worker_spec.js b/system-tests/test/web_worker_spec.js index f2c519de65e5..25f6bf0a6815 100644 --- a/system-tests/test/web_worker_spec.js +++ b/system-tests/test/web_worker_spec.js @@ -46,8 +46,8 @@ describe('e2e web worker', () => { spec: 'web_worker.cy.js', onRun: async (exec, browser) => { await exec() - expect(requestsForWebWorker).to.eq(1) - expect(requestsForSharedWorker).to.eq(1) + expect(requestsForWebWorker).to.eq(2) + expect(requestsForSharedWorker).to.eq(2) }, }) })