Skip to content

Chrome DevTools for coding agents

fix: honor background when new_page uses isolatedContext (#2658)

baishiwen9f215c82d3f41
diff --git a/src/McpContext.ts b/src/McpContext.ts
index d043fff2..bdb220b7 100644
--- a/src/McpContext.ts
+++ b/src/McpContext.ts
@@ -344,7 +344,7 @@ export class McpContext implements Context {
ctx = await this.browser.createBrowserContext();
this.#isolatedContexts.set(isolatedContextName, ctx);
}
- page = await ctx.newPage();
+ page = await ctx.newPage({background});
} else {
page = await this.browser.newPage({background});
}
diff --git a/tests/tools/pages.test.ts b/tests/tools/pages.test.ts
index c8acd25f..b03b349b 100644
--- a/tests/tools/pages.test.ts
+++ b/tests/tools/pages.test.ts
@@ -430,6 +430,38 @@ describe('pages', () => {
});
});
+ it('keeps focus when background is true with isolatedContext', async () => {
+ await withMcpContext(async (response, context) => {
+ const originalPage = context.getPageById(1);
+ assert.strictEqual(originalPage, context.getSelectedMcpPage());
+ // Ensure original page has focus
+ await originalPage.pptrPage.bringToFront();
+ assert.strictEqual(
+ await originalPage.pptrPage.evaluate(() => document.hasFocus()),
+ true,
+ );
+ await newPage().handler(
+ {
+ params: {
+ url: 'data:text/html,<html></html>',
+ background: true,
+ isolatedContext: 'session-a',
+ },
+ },
+ response,
+ context,
+ );
+ // New page should be selected but original should retain focus
+ const mcpPage = context.getSelectedMcpPage();
+ assert.strictEqual(mcpPage.isolatedContextName, 'session-a');
+ assert.strictEqual(
+ await originalPage.pptrPage.evaluate(() => document.hasFocus()),
+ true,
+ );
+ assert.ok(response.includePages);
+ });
+ });
+
it('reuses the same context for the same isolatedContext name', async () => {
await withMcpContext(async (response, context) => {
await newPage().handler(