Chrome DevTools for coding agents
fix(network): reset page network emulation before applying URL restrictions (#3312)
Nikolay Vitkovf25f3df2abfa
diff --git a/src/McpContext.ts b/src/McpContext.tsindex 19103716..fa15ff67 100644--- a/src/McpContext.ts+++ b/src/McpContext.ts@@ -251,17 +251,17 @@ export class McpContext implements Context, BrowserlessContext {*/async syncNetworkRestrictions(): Promise<void> {const {blockedUrlPattern, allowedUrlPattern} = this.#config.resolved;+ if (blockedUrlPattern || allowedUrlPattern) {+ for (const mcpPage of this.#allMcpPages()) {+ await mcpPage.clearNetworkConditions();+ }+ }if (this.#browser) {await this.#browser.restrictNetwork({blocklist: blockedUrlPattern,allowlist: allowedUrlPattern,});}- if (blockedUrlPattern || allowedUrlPattern) {- for (const mcpPage of this.#allMcpPages()) {- mcpPage.clearNetworkConditions();- }- }}/**diff --git a/src/McpPage.ts b/src/McpPage.tsindex bb8ff94a..4d74d91d 100644--- a/src/McpPage.ts+++ b/src/McpPage.ts@@ -1337,10 +1337,11 @@ export class McpPage implements ContextPage {}}- clearNetworkConditions(): void {+ async clearNetworkConditions(): Promise<void> {if (!this.emulationSettings.networkConditions) {return;}+ await this.#pptrPage?.emulateNetworkConditions(null);delete this.emulationSettings.networkConditions;this.updateTimeouts();}diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.tsindex 5ddd2b78..9ed5c124 100644--- a/tests/McpContext.test.ts+++ b/tests/McpContext.test.ts@@ -1747,11 +1747,15 @@ describe('McpContext', () => {await updateConfig(config, context, {blockedUrlPattern: ['https://blocked.example.com/*'],});+ sinon.assert.calledOnceWithExactly(clearNetworkConditionsSpy);sinon.assert.calledOnceWithExactly(browser.restrictNetwork, {blocklist: ['https://blocked.example.com/*'],allowlist: undefined,});- sinon.assert.calledOnceWithExactly(clearNetworkConditionsSpy);+ sinon.assert.callOrder(+ clearNetworkConditionsSpy,+ browser.restrictNetwork,+ );browser.restrictNetwork.resetHistory();clearNetworkConditionsSpy.resetHistory();@@ -1765,11 +1769,15 @@ describe('McpContext', () => {blockedUrlPattern: undefined,allowedUrlPattern: ['https://allowed.example.com/*'],});+ sinon.assert.calledOnceWithExactly(clearNetworkConditionsSpy);sinon.assert.calledOnceWithExactly(browser.restrictNetwork, {blocklist: undefined,allowlist: ['https://allowed.example.com/*'],});- sinon.assert.calledOnceWithExactly(clearNetworkConditionsSpy);+ sinon.assert.callOrder(+ clearNetworkConditionsSpy,+ browser.restrictNetwork,+ );browser.restrictNetwork.resetHistory();clearNetworkConditionsSpy.resetHistory();diff --git a/tests/McpPage.test.ts b/tests/McpPage.test.tsindex 4a6d8088..67867d82 100644--- a/tests/McpPage.test.ts+++ b/tests/McpPage.test.ts@@ -1015,6 +1015,7 @@ describe('McpPage', () => {networkConditions: 'Slow 3G',cpuThrottlingRate: 2,});+ pptrPage.emulateNetworkConditions.resetHistory();pptrPage.setDefaultTimeout.resetHistory();pptrPage.setDefaultNavigationTimeout.resetHistory();config.commit(@@ -1023,10 +1024,14 @@ describe('McpPage', () => {}),);- mcpPage.clearNetworkConditions();+ await mcpPage.clearNetworkConditions();assert.strictEqual(mcpPage.networkConditions, null);assert.strictEqual(mcpPage.cpuThrottlingRate, 2);+ sinon.assert.calledOnceWithExactly(+ pptrPage.emulateNetworkConditions,+ null,+ );sinon.assert.calledOnceWithExactly(pptrPage.setDefaultTimeout, 10_000);sinon.assert.calledOnceWithExactly(pptrPage.setDefaultNavigationTimeout,@@ -1040,8 +1045,9 @@ describe('McpPage', () => {pptrPage.setDefaultTimeout.resetHistory();pptrPage.setDefaultNavigationTimeout.resetHistory();- mcpPage.clearNetworkConditions();+ await mcpPage.clearNetworkConditions();+ sinon.assert.notCalled(pptrPage.emulateNetworkConditions);sinon.assert.notCalled(pptrPage.setDefaultTimeout);sinon.assert.notCalled(pptrPage.setDefaultNavigationTimeout);});