From d75dab107e3903da47b94794b40c55e8bf6e7ab7 Mon Sep 17 00:00:00 2001 From: Ryan Manuel Date: Sun, 27 Aug 2023 21:44:29 -0500 Subject: [PATCH] chore: remove prerequests on request failed events (#27674) * chore: remove prerequests on request failed events * fix test --- packages/server/lib/automation/automation.ts | 2 +- packages/server/lib/browsers/cdp_automation.ts | 5 +++++ packages/server/lib/project-base.ts | 10 +++++++--- .../test/unit/browsers/cdp_automation_spec.ts | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/server/lib/automation/automation.ts b/packages/server/lib/automation/automation.ts index ab0e0ae5a5e1..b1101a505ca4 100644 --- a/packages/server/lib/automation/automation.ts +++ b/packages/server/lib/automation/automation.ts @@ -14,7 +14,7 @@ export class Automation { private cookies: Cookies private screenshot: { capture: (data: any, automate: any) => any } - constructor (cyNamespace?: string, cookieNamespace?: string, screenshotsFolder?: string | false, public onBrowserPreRequest?: OnBrowserPreRequest, public onRequestEvent?: OnRequestEvent, public onRequestServedFromCache?: (requestId: string) => void) { + constructor (cyNamespace?: string, cookieNamespace?: string, screenshotsFolder?: string | false, public onBrowserPreRequest?: OnBrowserPreRequest, public onRequestEvent?: OnRequestEvent, public onRequestServedFromCache?: (requestId: string) => void, public onRequestFailed?: (requestId: string) => void) { this.requests = {} // set the middleware diff --git a/packages/server/lib/browsers/cdp_automation.ts b/packages/server/lib/browsers/cdp_automation.ts index 45bfc30184a4..6f37150298b9 100644 --- a/packages/server/lib/browsers/cdp_automation.ts +++ b/packages/server/lib/browsers/cdp_automation.ts @@ -171,6 +171,7 @@ export class CdpAutomation implements CDPClient { onFn('Network.requestWillBeSent', this.onNetworkRequestWillBeSent) onFn('Network.responseReceived', this.onResponseReceived) onFn('Network.requestServedFromCache', this.onRequestServedFromCache) + onFn('Network.loadingFailed', this.onRequestFailed) this.on = onFn this.off = offFn @@ -245,6 +246,10 @@ export class CdpAutomation implements CDPClient { this.automation.onRequestServedFromCache?.(params.requestId) } + private onRequestFailed = (params: Protocol.Network.LoadingFailedEvent) => { + this.automation.onRequestFailed?.(params.requestId) + } + private onResponseReceived = (params: Protocol.Network.ResponseReceivedEvent) => { const browserResponseReceived: BrowserResponseReceived = { requestId: params.requestId, diff --git a/packages/server/lib/project-base.ts b/packages/server/lib/project-base.ts index 4d5487951fc7..f9ac70da24e9 100644 --- a/packages/server/lib/project-base.ts +++ b/packages/server/lib/project-base.ts @@ -313,8 +313,8 @@ export class ProjectBase extends EE { } startWebsockets (options: Omit, { socketIoCookie, namespace, screenshotsFolder, report, reporter, reporterOptions, projectRoot }: StartWebsocketOptions) { - // if we've passed down reporter - // then record these via mocha reporter + // if we've passed down reporter + // then record these via mocha reporter const reporterInstance = this.initializeReporter({ report, reporter, @@ -334,7 +334,11 @@ export class ProjectBase extends EE { this.server.removeBrowserPreRequest(requestId) } - this._automation = new Automation(namespace, socketIoCookie, screenshotsFolder, onBrowserPreRequest, onRequestEvent, onRequestServedFromCache) + const onRequestFailed = (requestId: string) => { + this.server.removeBrowserPreRequest(requestId) + } + + this._automation = new Automation(namespace, socketIoCookie, screenshotsFolder, onBrowserPreRequest, onRequestEvent, onRequestServedFromCache, onRequestFailed) const ios = this.server.startWebsockets(this.automation, this.cfg, { onReloadBrowser: options.onReloadBrowser, diff --git a/packages/server/test/unit/browsers/cdp_automation_spec.ts b/packages/server/test/unit/browsers/cdp_automation_spec.ts index 7843ae6ee35d..ade10b8513b0 100644 --- a/packages/server/test/unit/browsers/cdp_automation_spec.ts +++ b/packages/server/test/unit/browsers/cdp_automation_spec.ts @@ -71,6 +71,7 @@ context('lib/browsers/cdp_automation', () => { onBrowserPreRequest: sinon.stub(), onRequestEvent: sinon.stub(), onRequestServedFromCache: sinon.stub(), + onRequestFailed: sinon.stub(), } cdpAutomation = await CdpAutomation.create(this.sendDebuggerCommand, this.onFn, this.offFn, this.sendCloseTargetCommand, this.automation) @@ -196,6 +197,20 @@ context('lib/browsers/cdp_automation', () => { }) }) + describe('.onRequestFailed', function () { + it('triggers onRequestFailed', function () { + const browserRequestFailed = { + requestId: '0', + } + + this.onFn + .withArgs('Network.loadingFailed') + .yield(browserRequestFailed) + + expect(this.automation.onRequestFailed).to.have.been.calledWith(browserRequestFailed.requestId) + }) + }) + describe('get:cookies', () => { beforeEach(function () { this.sendDebuggerCommand.withArgs('Network.getAllCookies')