From 8aac8b1ccb1704f0a67165a7e06427c7db0b4b2f Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Mon, 10 Jun 2024 14:02:10 +0200 Subject: [PATCH] fix: exposed functions should only be called once (#12560) --- .../puppeteer-core/src/cdp/ExecutionContext.ts | 8 ++++---- test/src/page.spec.ts | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/puppeteer-core/src/cdp/ExecutionContext.ts b/packages/puppeteer-core/src/cdp/ExecutionContext.ts index 31e9deb994a..602da4995d7 100644 --- a/packages/puppeteer-core/src/cdp/ExecutionContext.ts +++ b/packages/puppeteer-core/src/cdp/ExecutionContext.ts @@ -158,6 +158,10 @@ export class ExecutionContext async #onBindingCalled( event: Protocol.Runtime.BindingCalledEvent ): Promise { + if (event.executionContextId !== this.#id) { + return; + } + let payload: BindingPayload; try { payload = JSON.parse(event.payload); @@ -177,10 +181,6 @@ export class ExecutionContext } try { - if (event.executionContextId !== this.#id) { - return; - } - const binding = this.#bindings.get(name); await binding?.run(this, seq, args, isTrivial); } catch (err) { diff --git a/test/src/page.spec.ts b/test/src/page.spec.ts index c0c59d16748..1a8bce40de1 100644 --- a/test/src/page.spec.ts +++ b/test/src/page.spec.ts @@ -1193,6 +1193,22 @@ describe('Page', function () { }); expect(result).toBe(36); }); + + it('should be called once', async () => { + const {page, server} = await getTestState(); + + await page.goto(server.PREFIX + '/frames/nested-frames.html'); + let calls = 0; + await page.exposeFunction('call', function () { + calls++; + }); + + const frame = page.frames()[1]!; + await frame.evaluate(async function () { + return (globalThis as any).call(); + }); + expect(calls).toBe(1); + }); }); describe('Page.removeExposedFunction', function () {