From 913bdddc94b9ebaed92a89047814512bd04bae95 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov Date: Mon, 22 Sep 2025 15:53:55 +0200 Subject: [PATCH 1/2] fix(emulation): correctly report info for selected page --- src/McpContext.ts | 20 ++++++++++++----- src/WaitForHelper.ts | 2 ++ tests/tools/emulation.test.ts | 42 +++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/McpContext.ts b/src/McpContext.ts index c7d4689f..b2b2883c 100644 --- a/src/McpContext.ts +++ b/src/McpContext.ts @@ -49,8 +49,8 @@ export class McpContext implements Context { #consoleCollector: PageCollector; #isRunningTrace = false; - #networkConditions: string | null = null; - #cpuThrottlingRate = 1; + #networkConditionsMap = new WeakMap(); + #cpuThrottlingRateMap = new WeakMap(); #dialog?: Dialog; #nextSnapshotId = 1; @@ -130,19 +130,27 @@ export class McpContext implements Context { } setNetworkConditions(conditions: string | null): void { - this.#networkConditions = conditions; + const page = this.getSelectedPage(); + if (conditions === null) { + this.#networkConditionsMap.delete(page); + } else { + this.#networkConditionsMap.set(page, conditions); + } } getNetworkConditions(): string | null { - return this.#networkConditions; + const page = this.getSelectedPage(); + return this.#networkConditionsMap.get(page) ?? null; } setCpuThrottlingRate(rate: number): void { - this.#cpuThrottlingRate = rate; + const page = this.getSelectedPage(); + this.#cpuThrottlingRateMap.set(page, rate); } getCpuThrottlingRate(): number { - return this.#cpuThrottlingRate; + const page = this.getSelectedPage(); + return this.#cpuThrottlingRateMap.get(page) ?? 1; } setIsRunningPerformanceTrace(x: boolean): void { diff --git a/src/WaitForHelper.ts b/src/WaitForHelper.ts index d5221bff..542e7b9b 100644 --- a/src/WaitForHelper.ts +++ b/src/WaitForHelper.ts @@ -27,6 +27,7 @@ export class WaitForHelper { * for the DOM to be stable before returning. */ async waitForStableDom(): Promise { + // TODO: use CPU mutplier const stableDomObserver = await this.#page.evaluateHandle(timeout => { let timeoutId: ReturnType; function callback() { @@ -76,6 +77,7 @@ export class WaitForHelper { } async waitForNavigationStarted() { + // TODO: CPU mutliper // Currently Puppeteer does not have API // For when a navigation is about to start const navigationStartedPromise = new Promise(resolve => { diff --git a/tests/tools/emulation.test.ts b/tests/tools/emulation.test.ts index 411c2ce1..43c9cc4d 100644 --- a/tests/tools/emulation.test.ts +++ b/tests/tools/emulation.test.ts @@ -57,6 +57,27 @@ describe('emulation', () => { assert.strictEqual(context.getNetworkConditions(), null); }); }); + + it('report correctly for the currently selected page', async () => { + await withBrowser(async (response, context) => { + await context.newPage(); + await emulateNetwork.handler( + { + params: { + throttlingOption: 'Slow 3G', + }, + }, + response, + context, + ); + + assert.strictEqual(context.getNetworkConditions(), 'Slow 3G'); + + context.setSelectedPageIdx(0); + + assert.strictEqual(context.getNetworkConditions(), null); + }); + }); }); describe('cpu', () => { @@ -92,5 +113,26 @@ describe('emulation', () => { assert.strictEqual(context.getCpuThrottlingRate(), 1); }); }); + + it('report correctly for the currently selected page', async () => { + await withBrowser(async (response, context) => { + await context.newPage(); + await emulateCpu.handler( + { + params: { + throttlingRate: 4, + }, + }, + response, + context, + ); + + assert.strictEqual(context.getCpuThrottlingRate(), 4); + + context.setSelectedPageIdx(0); + + assert.strictEqual(context.getCpuThrottlingRate(), 1); + }); + }); }); }); From f61a347b52b54a51c1c80eee74f59d70cb3d4952 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov Date: Mon, 22 Sep 2025 15:55:50 +0200 Subject: [PATCH 2/2] fix --- src/WaitForHelper.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/WaitForHelper.ts b/src/WaitForHelper.ts index 542e7b9b..d5221bff 100644 --- a/src/WaitForHelper.ts +++ b/src/WaitForHelper.ts @@ -27,7 +27,6 @@ export class WaitForHelper { * for the DOM to be stable before returning. */ async waitForStableDom(): Promise { - // TODO: use CPU mutplier const stableDomObserver = await this.#page.evaluateHandle(timeout => { let timeoutId: ReturnType; function callback() { @@ -77,7 +76,6 @@ export class WaitForHelper { } async waitForNavigationStarted() { - // TODO: CPU mutliper // Currently Puppeteer does not have API // For when a navigation is about to start const navigationStartedPromise = new Promise(resolve => {