diff --git a/src/McpContext.ts b/src/McpContext.ts index 198a91a59..ae0063428 100644 --- a/src/McpContext.ts +++ b/src/McpContext.ts @@ -284,7 +284,9 @@ export class McpContext implements Context { async restoreEmulation(page: McpPage) { const currentSetting = page.emulationSettings; - await this.emulate(currentSetting, page.pptrPage); + await this.emulate(currentSetting, page.pptrPage, { + skipNetworkReset: !currentSetting.networkConditions, + }); } async emulate( @@ -297,13 +299,18 @@ export class McpContext implements Context { viewport?: Viewport; }, targetPage?: Page, + internalOptions: { + skipNetworkReset?: boolean; + } = {}, ): Promise { const page = targetPage ?? this.getSelectedPptrPage(); const mcpPage = this.#getMcpPage(page); const newSettings: EmulationSettings = {...mcpPage.emulationSettings}; if (!options.networkConditions) { - await page.emulateNetworkConditions(null); + if (!internalOptions.skipNetworkReset) { + await page.emulateNetworkConditions(null); + } delete newSettings.networkConditions; } else if (options.networkConditions === 'Offline') { await page.emulateNetworkConditions({ diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.ts index 31a6c88b3..3987cb676 100644 --- a/tests/McpContext.test.ts +++ b/tests/McpContext.test.ts @@ -67,6 +67,20 @@ describe('McpContext', () => { }); }); + it('does not reset network during restore when no network emulation exists', async () => { + await withMcpContext(async (_response, context) => { + const page = context.getSelectedMcpPage(); + const emulateNetworkConditionsSpy = sinon.spy( + page.pptrPage, + 'emulateNetworkConditions', + ); + + await context.restoreEmulation(page); + + sinon.assert.notCalled(emulateNetworkConditionsSpy); + }); + }); + it('should call waitForEventsAfterAction with correct multipliers', async () => { await withMcpContext(async (_response, context) => { const page = await context.newPage();