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/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); + }); + }); }); });