From 92110151d9a33f26abc07bc805f4f2f3943697a0 Mon Sep 17 00:00:00 2001 From: Junyan Date: Tue, 31 May 2022 04:34:08 +0800 Subject: [PATCH] fix: multiple same request event listener (#8404) --- src/common/Page.ts | 12 +++++++----- test/page.spec.ts | 10 +++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/common/Page.ts b/src/common/Page.ts index 0493a324..5cfd88bd 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -631,11 +631,13 @@ export class Page extends EventEmitter { handler: (event: PageEventObject[K]) => void ): EventEmitter { if (eventName === 'request') { - const wrap = (event: HTTPRequest) => { - event.enqueueInterceptAction(() => - handler(event as PageEventObject[K]) - ); - }; + const wrap = + this._handlerMap.get(handler) || + ((event: HTTPRequest) => { + event.enqueueInterceptAction(() => + handler(event as PageEventObject[K]) + ); + }); this._handlerMap.set(handler, wrap); diff --git a/test/page.spec.ts b/test/page.spec.ts index 4a1a5b2a..9c31f5b7 100644 --- a/test/page.spec.ts +++ b/test/page.spec.ts @@ -153,17 +153,21 @@ describe('Page', function () { } }; + page.on('request', onResponse); page.on('request', onResponse); await page.goto(server.EMPTY_PAGE); - expect(handler.callCount).toBe(1); + expect(handler.callCount).toBe(2); page.off('request', onResponse); await page.goto(server.EMPTY_PAGE); // Still one because we removed the handler. - expect(handler.callCount).toBe(1); + expect(handler.callCount).toBe(3); + page.off('request', onResponse); + await page.goto(server.EMPTY_PAGE); + expect(handler.callCount).toBe(3); page.on('request', onResponse); await page.goto(server.EMPTY_PAGE); // Two now because we added the handler back. - expect(handler.callCount).toBe(2); + expect(handler.callCount).toBe(4); }); });