Chrome DevTools for coding agents
test: fix potential flakiness in tests (#2396)
Alex Rudenkob4546ef86b86
diff --git a/src/McpContext.ts b/src/McpContext.tsindex 2601883a..1ae83fe9 100644--- a/src/McpContext.ts+++ b/src/McpContext.ts@@ -616,12 +616,25 @@ export class McpContext implements Context {}async installExtension(extensionPath: string): Promise<string> {- const id = await this.browser.installExtension(extensionPath);+ const id = await Promise.race([+ this.browser.installExtension(extensionPath),+ new Promise<string>((_, rej) =>+ setTimeout(() => rej(new Error('Timeout installing extension')), 30000),+ ),+ ]);return id;}async uninstallExtension(id: string): Promise<void> {- await this.browser.uninstallExtension(id);+ await Promise.race([+ this.browser.uninstallExtension(id),+ new Promise<void>((_, rej) =>+ setTimeout(+ () => rej(new Error('Timeout uninstalling extension')),+ 30000,+ ),+ ),+ ]);}async triggerExtensionAction(id: string): Promise<void> {diff --git a/src/bin/chrome-devtools-mcp-main.ts b/src/bin/chrome-devtools-mcp-main.tsindex 2fb50f16..5710fe21 100644--- a/src/bin/chrome-devtools-mcp-main.ts+++ b/src/bin/chrome-devtools-mcp-main.ts@@ -51,7 +51,7 @@ async function shutdown(reason: string): Promise<void> {setTimeout(() => {logger?.('Shutdown timeout exceeded, forcing exit');process.exit(0);- }, 10000).unref();+ }, 5000).unref();await closeBrowser();process.exit(0);}diff --git a/src/tools/performance.ts b/src/tools/performance.tsindex 9896967d..96c5a01d 100644--- a/src/tools/performance.ts+++ b/src/tools/performance.ts@@ -63,8 +63,9 @@ export const startTrace = definePageTool({if (request.params.reload) {// Before starting the recording, navigate to about:blank to clear out any state.+ // We use `load` because `networkidle0` is known to be flaky for about:blank in Puppeteer.await page.pptrPage.goto('about:blank', {- waitUntil: ['networkidle0'],+ waitUntil: 'load',});}diff --git a/tests/browser.test.ts b/tests/browser.test.tsindex 3c4ed597..69245781 100644--- a/tests/browser.test.ts+++ b/tests/browser.test.ts@@ -12,168 +12,236 @@ import {describe, it} from 'node:test';import {executablePath} from 'puppeteer';import {detectDisplay, ensureBrowserConnected, launch} from '../src/browser.js';+import type {Browser} from '../src/third_party/index.js';import {serverHooks} from './server.js';+async function safeClose(browser: Browser) {+ try {+ await Promise.race([+ browser.close(),+ new Promise((_, rej) =>+ setTimeout(() => rej(new Error('timeout')), 2000),+ ),+ ]);+ } catch {+ browser.process()?.kill('SIGKILL');+ }+}++async function runWithRetry(fn: () => Promise<void>) {+ let lastError: Error | undefined;+ for (let attempt = 1; attempt <= 3; attempt++) {+ try {+ await Promise.race([+ fn(),+ new Promise((_, reject) =>+ setTimeout(+ () => reject(new Error('Test execution timeout exceeded')),+ 20000,+ ),+ ),+ ]);+ return;+ } catch (e) {+ lastError = e as Error;+ await new Promise(r => setTimeout(r, 500));+ }+ }+ throw lastError;+}+describe('browser', () => {it('detects display does not crash', () => {detectDisplay();});it('cannot launch multiple times with the same profile', async () => {- const tmpDir = os.tmpdir();- const folderPath = path.join(tmpDir, `temp-folder-${crypto.randomUUID()}`);- const browser1 = await launch({- headless: true,- isolated: false,- userDataDir: folderPath,- executablePath: await executablePath(),- devtools: false,- });- try {+ await runWithRetry(async () => {+ const tmpDir = os.tmpdir();+ const folderPath = path.join(+ tmpDir,+ `temp-folder-${crypto.randomUUID()}`,+ );+ const browser1 = await launch({+ headless: true,+ isolated: false,+ userDataDir: folderPath,+ executablePath: await executablePath(),+ devtools: false,+ });try {- const browser2 = await launch({- headless: true,- isolated: false,- userDataDir: folderPath,- executablePath: await executablePath(),- devtools: false,- });- await browser2.close();- assert.fail('not reached');- } catch (err) {- assert.strictEqual(- err.message,- `The browser is already running for ${folderPath}. Use --isolated to run multiple browser instances.`,- );+ try {+ const browser2 = await launch({+ headless: true,+ isolated: false,+ userDataDir: folderPath,+ executablePath: await executablePath(),+ devtools: false,+ });+ await safeClose(browser2);+ assert.fail('not reached');+ } catch (err) {+ assert.strictEqual(+ (err as Error).message,+ `The browser is already running for ${folderPath}. Use --isolated to run multiple browser instances.`,+ );+ }+ } finally {+ await safeClose(browser1);}- } finally {- await browser1.close();- }+ });});it('launches with the initial viewport', async () => {- const tmpDir = os.tmpdir();- const folderPath = path.join(tmpDir, `temp-folder-${crypto.randomUUID()}`);- const browser = await launch({- headless: true,- isolated: false,- userDataDir: folderPath,- executablePath: await executablePath(),- viewport: {- width: 1501,- height: 801,- },- devtools: false,- });- try {- const [page] = await browser.pages();- const result = await page.evaluate(() => {- return {width: window.innerWidth, height: window.innerHeight};- });- assert.deepStrictEqual(result, {- width: 1501,- height: 801,+ await runWithRetry(async () => {+ const tmpDir = os.tmpdir();+ const folderPath = path.join(+ tmpDir,+ `temp-folder-${crypto.randomUUID()}`,+ );+ const browser = await launch({+ headless: true,+ isolated: false,+ userDataDir: folderPath,+ executablePath: await executablePath(),+ viewport: {+ width: 1501,+ height: 801,+ },+ devtools: false,});- } finally {- await browser.close();- }+ try {+ const [page] = await browser.pages();+ const result = await page.evaluate(() => {+ return {width: window.innerWidth, height: window.innerHeight};+ });+ assert.deepStrictEqual(result, {+ width: 1501,+ height: 801,+ });+ } finally {+ await safeClose(browser);+ }+ });});+it('connects to an existing browser with userDataDir', async () => {- const tmpDir = os.tmpdir();- const folderPath = path.join(tmpDir, `temp-folder-${crypto.randomUUID()}`);- const browser = await launch({- headless: true,- isolated: false,- userDataDir: folderPath,- executablePath: await executablePath(),- devtools: false,- chromeArgs: ['--remote-debugging-port=0'],- });- try {- const connectedBrowser = await ensureBrowserConnected({+ await runWithRetry(async () => {+ const tmpDir = os.tmpdir();+ const folderPath = path.join(+ tmpDir,+ `temp-folder-${crypto.randomUUID()}`,+ );+ const browser = await launch({+ headless: true,+ isolated: false,userDataDir: folderPath,+ executablePath: await executablePath(),devtools: false,+ chromeArgs: ['--remote-debugging-port=0'],});- assert.ok(connectedBrowser);- assert.ok(connectedBrowser.connected);- connectedBrowser.disconnect();- } finally {- await browser.close();- }+ try {+ const connectedBrowser = await ensureBrowserConnected({+ userDataDir: folderPath,+ devtools: false,+ });+ assert.ok(connectedBrowser);+ assert.ok(connectedBrowser.connected);+ connectedBrowser.disconnect();+ } finally {+ await safeClose(browser);+ }+ });});describe('Blocking', () => {const server = serverHooks();it('blocks URLs in blocklist', async () => {- server.addHtmlRoute('/allowed.html', '<html><body>Allowed</body></html>');- server.addHtmlRoute('/blocked.html', '<html><body>Blocked</body></html>');+ await runWithRetry(async () => {+ server.addHtmlRoute(+ '/allowed.html',+ '<html><body>Allowed</body></html>',+ );+ server.addHtmlRoute(+ '/blocked.html',+ '<html><body>Blocked</body></html>',+ );- const browser = await launch({- headless: true,- isolated: true,- executablePath: await executablePath(),- devtools: false,- blocklist: ['*://*:*/blocked.html'],+ const browser = await launch({+ headless: true,+ isolated: true,+ executablePath: await executablePath(),+ devtools: false,+ blocklist: ['*://*:*/blocked.html'],+ });+ try {+ const page = await browser.newPage();++ // Access allowed URL+ await page.goto(server.getRoute('/allowed.html'));+ const content = await page.evaluate(() => document.body.textContent);+ assert.strictEqual(content, 'Allowed');++ // Fetch of blocked URL from the page+ const fetchSucceeded = await page.evaluate(async url => {+ try {+ await fetch(url, {signal: AbortSignal.timeout(5000)});+ return true;+ } catch {+ return false;+ }+ }, server.getRoute('/blocked.html'));++ assert.strictEqual(fetchSucceeded, false);+ } finally {+ await safeClose(browser);+ }});- try {- const page = await browser.newPage();-- // Access allowed URL- await page.goto(server.getRoute('/allowed.html'));- const content = await page.evaluate(() => document.body.textContent);- assert.strictEqual(content, 'Allowed');-- // Fetch of blocked URL from the page- const fetchSucceeded = await page.evaluate(async url => {- try {- await fetch(url);- return true;- } catch {- return false;- }- }, server.getRoute('/blocked.html'));-- assert.strictEqual(fetchSucceeded, false);- } finally {- await browser.close();- }});it('blocks URLs not in allowlist', async () => {- server.addHtmlRoute('/allowed.html', '<html><body>Allowed</body></html>');- server.addHtmlRoute('/blocked.html', '<html><body>Blocked</body></html>');+ await runWithRetry(async () => {+ server.addHtmlRoute(+ '/allowed.html',+ '<html><body>Allowed</body></html>',+ );+ server.addHtmlRoute(+ '/blocked.html',+ '<html><body>Blocked</body></html>',+ );- const browser = await launch({- headless: true,- isolated: true,- executablePath: await executablePath(),- devtools: false,- allowlist: ['*://*:*/allowed.html'],+ const browser = await launch({+ headless: true,+ isolated: true,+ executablePath: await executablePath(),+ devtools: false,+ allowlist: ['*://*:*/allowed.html'],+ });+ try {+ const page = await browser.newPage();++ // Access allowed URL+ await page.goto(server.getRoute('/allowed.html'));+ const content = await page.evaluate(() => document.body.textContent);+ assert.strictEqual(content, 'Allowed');++ // Fetch of blocked URL from the page+ const fetchSucceeded = await page.evaluate(async url => {+ try {+ await fetch(url, {signal: AbortSignal.timeout(5000)});+ return true;+ } catch {+ return false;+ }+ }, server.getRoute('/blocked.html'));++ assert.strictEqual(fetchSucceeded, false);+ } finally {+ await safeClose(browser);+ }});- try {- const page = await browser.newPage();-- // Access allowed URL- await page.goto(server.getRoute('/allowed.html'));- const content = await page.evaluate(() => document.body.textContent);- assert.strictEqual(content, 'Allowed');-- // Fetch of blocked URL from the page- const fetchSucceeded = await page.evaluate(async url => {- try {- await fetch(url);- return true;- } catch {- return false;- }- }, server.getRoute('/blocked.html'));-- assert.strictEqual(fetchSucceeded, false);- } finally {- await browser.close();- }});});});diff --git a/tests/index.test.ts b/tests/index.test.tsindex 9488ae98..497e732d 100644--- a/tests/index.test.ts+++ b/tests/index.test.ts@@ -30,33 +30,52 @@ describe('e2e', () => {extraArgs: string[] = [],options: {capabilities?: ClientCapabilities} = {},) {- const transport = new StdioClientTransport({- command: 'node',- args: [- 'build/src/bin/chrome-devtools-mcp.js',- '--headless',- '--isolated',- '--executable-path',- await executablePath(),- ...extraArgs,- ],- env: {...process.env, CHROME_DEVTOOLS_MCP_NO_USAGE_STATISTICS: 'true'},- });- const client = new Client(- {- name: 'e2e-test',- version: '1.0.0',- },- {- capabilities: options.capabilities ?? {},- },- );+ let attempt = 1;+ while (attempt <= 3) {+ const transport = new StdioClientTransport({+ command: 'node',+ args: [+ 'build/src/bin/chrome-devtools-mcp.js',+ '--headless',+ '--isolated',+ '--executable-path',+ await executablePath(),+ ...extraArgs,+ ],+ env: {...process.env, CHROME_DEVTOOLS_MCP_NO_USAGE_STATISTICS: 'true'},+ });+ const client = new Client(+ {+ name: 'e2e-test',+ version: '1.0.0',+ },+ {+ capabilities: options.capabilities ?? {},+ },+ );- try {- await client.connect(transport);- await cb(client);- } finally {- await client.close();+ try {+ await client.connect(transport);+ await cb(client);+ return;+ } catch (error) {+ if (+ attempt === 3 ||+ !(error instanceof Error) ||+ (!error.message.includes('timed out') &&+ !error.message.includes('timeout'))+ ) {+ throw error;+ }+ attempt++;+ await new Promise(r => setTimeout(r, 1000));+ } finally {+ try {+ await client.close();+ } catch {+ // Ignore close errors+ }+ }}}it('calls a tool', async t => {diff --git a/tests/network_blocking.test.ts b/tests/network_blocking.test.tsindex fbc2279b..02ff8872 100644--- a/tests/network_blocking.test.ts+++ b/tests/network_blocking.test.ts@@ -59,7 +59,7 @@ describe('Network Blocking Integration', () => {params: {function: `async () => {try {- await fetch("${blockedUrl}");+ await fetch("${blockedUrl}", { signal: AbortSignal.timeout(5000) });return 'SUCCESS';} catch (err) {return err instanceof Error ? err.message : String(err);@@ -124,7 +124,7 @@ describe('Network Blocking Integration', () => {params: {function: `async () => {try {- await fetch("${blockedUrl}");+ await fetch("${blockedUrl}", { signal: AbortSignal.timeout(5000) });return 'SUCCESS';} catch (err) {return err instanceof Error ? err.message : String(err);@@ -177,7 +177,7 @@ describe('Network Blocking Integration', () => {params: {function: `async () => {try {- await fetch("${blockedUrl}");+ await fetch("${blockedUrl}", { signal: AbortSignal.timeout(5000) });return 'SUCCESS';} catch (err) {return err instanceof Error ? err.message : String(err);@@ -218,7 +218,7 @@ describe('Network Blocking Integration', () => {params: {function: `async () => {try {- await fetch("${blockedUrl}");+ await fetch("${blockedUrl}", { signal: AbortSignal.timeout(5000) });return 'SUCCESS';} catch (err) {return err instanceof Error ? err.message : String(err);diff --git a/tests/server.ts b/tests/server.tsindex 77369ce6..2adabc46 100644--- a/tests/server.ts+++ b/tests/server.ts@@ -87,10 +87,32 @@ export class TestServer {this.#routes = {};}- start(): Promise<void> {- return new Promise(res => {- this.#server.listen(this.#port, res);- });+ async start(): Promise<void> {+ let retries = 5;+ while (retries > 0) {+ try {+ await new Promise<void>((res, rej) => {+ this.#server.once('error', rej);+ this.#server.listen(this.#port, () => {+ this.#server.off('error', rej);+ res();+ });+ });+ return;+ } catch (err) {+ if (+ err instanceof Error &&+ 'code' in err &&+ err.code === 'EADDRINUSE'+ ) {+ retries--;+ this.#port = TestServer.randomPort();+ } else {+ throw err;+ }+ }+ }+ throw new Error('Failed to bind to a port after 5 retries');}stop(): Promise<void> {diff --git a/tests/shutdown.test.ts b/tests/shutdown.test.tsindex 23a22fbc..496234f4 100644--- a/tests/shutdown.test.ts+++ b/tests/shutdown.test.ts@@ -44,6 +44,10 @@ async function spawnServer(): Promise<Server> {child.stderr.on('data', () => {// discard});+ // Drain stdout to avoid pipe-buffer backpressure stalling the server during shutdown.+ child.stdout.on('data', () => {+ // discard+ });return child;}@@ -87,7 +91,9 @@ async function rpc(try {const parsed = JSON.parse(line) as {id?: number};if (parsed.id === id) {+ clearTimeout(timer);child.stdout.off('data', onData);+ child.off('exit', onExit);resolve(parsed);return;}@@ -96,8 +102,19 @@ async function rpc(}}};+ const timer = setTimeout(() => {+ child.stdout.off('data', onData);+ child.off('exit', onExit);+ reject(+ new Error(+ `RPC timeout: no response for method ${msg.method} within 60000ms`,+ ),+ );+ }, 60000);+child.stdout.on('data', onData);const onExit = () => {+ clearTimeout(timer);child.stdout.off('data', onData);reject(new Error('server exited before RPC response'));};@@ -120,23 +137,40 @@ async function initializeAndLaunchBrowser(child: Server): Promise<void> {},});notify(child, {method: 'notifications/initialized'});- // navigate_page forces a real Chrome launch — this is what reproduces+ // list_pages forces a real Chrome launch — this is what reproduces// the hang in #2116. Without an active Chrome subprocess, stdin EOF// would close the event loop on its own and shutdown would look fine// even with broken handlers.await rpc(child, {method: 'tools/call',params: {- name: 'navigate_page',- arguments: {url: 'about:blank'},+ name: 'list_pages',+ arguments: {},},});}+async function setupServerWithRetry(): Promise<Server> {+ let lastError: Error | undefined;+ for (let attempt = 1; attempt <= 3; attempt++) {+ const child = await spawnServer();+ try {+ await initializeAndLaunchBrowser(child);+ return child;+ } catch (e) {+ lastError = e as Error;+ // If setup failed (e.g., Chrome hung on launch), kill the child and try again.+ child.kill('SIGKILL');+ // Wait briefly for OS cleanup.+ await new Promise(r => setTimeout(r, 500));+ }+ }+ throw lastError;+}+describe('shutdown', () => {it('exits within budget on stdin EOF after Chrome launch', async () => {- const child = await spawnServer();- await initializeAndLaunchBrowser(child);+ const child = await setupServerWithRetry();child.stdin.end();const {elapsedMs} = await waitForExit(child, EXIT_TIMEOUT_MS);assert.ok(@@ -146,8 +180,7 @@ describe('shutdown', () => {});it('exits within budget on SIGTERM after Chrome launch', async () => {- const child = await spawnServer();- await initializeAndLaunchBrowser(child);+ const child = await setupServerWithRetry();child.kill('SIGTERM');const {elapsedMs} = await waitForExit(child, EXIT_TIMEOUT_MS);assert.ok(@@ -157,8 +190,7 @@ describe('shutdown', () => {});it('exits within budget on SIGINT after Chrome launch', async () => {- const child = await spawnServer();- await initializeAndLaunchBrowser(child);+ const child = await setupServerWithRetry();child.kill('SIGINT');const {elapsedMs} = await waitForExit(child, EXIT_TIMEOUT_MS);assert.ok(@@ -168,8 +200,7 @@ describe('shutdown', () => {});it('exits within budget on SIGHUP after Chrome launch', async () => {- const child = await spawnServer();- await initializeAndLaunchBrowser(child);+ const child = await setupServerWithRetry();child.kill('SIGHUP');const {elapsedMs} = await waitForExit(child, EXIT_TIMEOUT_MS);assert.ok(diff --git a/tests/tools/extensions.test.ts b/tests/tools/extensions.test.tsindex e98ea494..6f57d8ad 100644--- a/tests/tools/extensions.test.ts+++ b/tests/tools/extensions.test.ts@@ -58,15 +58,10 @@ describe('extension', () => {);const extensionId = extractExtensionId(response);- const page = context.getSelectedMcpPage().pptrPage;- await page.goto('chrome://extensions');-- const element = await page.waitForSelector(- `extensions-manager >>> extensions-item[id="${extensionId}"]`,- );+ let extensions = await context.listExtensions();assert.ok(- element,- `Extension with ID "${extensionId}" should be visible on chrome://extensions`,+ extensions.has(extensionId!),+ `Extension with ID "${extensionId}" should be installed`,);// Uninstall the extension@@ -82,15 +77,10 @@ describe('extension', () => {'Response should indicate uninstallation',);- await page.waitForSelector('extensions-manager');-- const elementAfterUninstall = await page.$(- `extensions-manager >>> extensions-item[id="${extensionId}"]`,- );- assert.strictEqual(- elementAfterUninstall,- null,- `Extension with ID "${extensionId}" should NOT be visible on chrome://extensions`,+ extensions = await context.listExtensions();+ assert.ok(+ !extensions.has(extensionId!),+ `Extension with ID "${extensionId}" should NOT be installed`,);});});diff --git a/tests/tools/pages.test.js.snapshot b/tests/tools/pages.test.js.snapshotindex 958c460b..7ed6a21a 100644--- a/tests/tools/pages.test.js.snapshot+++ b/tests/tools/pages.test.js.snapshot@@ -39,7 +39,7 @@ exports[`pages > navigate_page > when dialog is open 1`] = ``;exports[`pages > new_page with isolatedContext > when dialog is open 1`] = `-{"content":[{"type":"text","text":"# Open dialog\\nalert: test dialog.\\nCall handle_dialog to handle it before continuing.\\n## Pages\\n1: about:blank\\n2: about:blank [selected]"}],"structuredContent":{"dialog":{"type":"alert","message":"test dialog","defaultValue":""},"pages":[{"id":1,"url":"about:blank","title":"","selected":false},{"id":2,"url":"about:blank","title":"","selected":true}]}}+{"content":[{"type":"text","text":"# Open dialog\\nalert: test dialog.\\nCall handle_dialog to handle it before continuing.\\n## Pages\\n1: about:blank\\n2: data:text/html,<html></html> [selected]"}],"structuredContent":{"dialog":{"type":"alert","message":"test dialog","defaultValue":""},"pages":[{"id":1,"url":"about:blank","title":"","selected":false},{"id":2,"url":"data:text/html,<html></html>","title":"","selected":true}]}}`;exports[`pages > resize > when dialog is open 1`] = `diff --git a/tests/tools/pages.test.ts b/tests/tools/pages.test.tsindex 2dc01125..b8ba3ef2 100644--- a/tests/tools/pages.test.ts+++ b/tests/tools/pages.test.ts@@ -238,7 +238,7 @@ describe('pages', () => {context.getSelectedMcpPage(),);await newPage().handler(- {params: {url: 'about:blank'}},+ {params: {url: 'data:text/html,<html></html>'}},response,context,);@@ -260,7 +260,7 @@ describe('pages', () => {true,);await newPage().handler(- {params: {url: 'about:blank', background: true}},+ {params: {url: 'data:text/html,<html></html>', background: true}},response,context,);@@ -281,7 +281,12 @@ describe('pages', () => {it('creates a page in an isolated context', async () => {await withMcpContext(async (response, context) => {await newPage().handler(- {params: {url: 'about:blank', isolatedContext: 'session-a'}},+ {+ params: {+ url: 'data:text/html,<html></html>',+ isolatedContext: 'session-a',+ },+ },response,context,);@@ -294,14 +299,24 @@ describe('pages', () => {it('reuses the same context for the same isolatedContext name', async () => {await withMcpContext(async (response, context) => {await newPage().handler(- {params: {url: 'about:blank', isolatedContext: 'session-a'}},+ {+ params: {+ url: 'data:text/html,<html></html>',+ isolatedContext: 'session-a',+ },+ },response,context,);const mcpPage1 = context.getSelectedMcpPage();const page1 = mcpPage1.pptrPage;await newPage().handler(- {params: {url: 'about:blank', isolatedContext: 'session-a'}},+ {+ params: {+ url: 'data:text/html,<html></html>',+ isolatedContext: 'session-a',+ },+ },response,context,);@@ -317,14 +332,24 @@ describe('pages', () => {it('creates separate contexts for different isolatedContext names', async () => {await withMcpContext(async (response, context) => {await newPage().handler(- {params: {url: 'about:blank', isolatedContext: 'session-a'}},+ {+ params: {+ url: 'data:text/html,<html></html>',+ isolatedContext: 'session-a',+ },+ },response,context,);const mcpPageA = context.getSelectedMcpPage();const pageA = mcpPageA.pptrPage;await newPage().handler(- {params: {url: 'about:blank', isolatedContext: 'session-b'}},+ {+ params: {+ url: 'data:text/html,<html></html>',+ isolatedContext: 'session-b',+ },+ },response,context,);@@ -339,7 +364,12 @@ describe('pages', () => {it('includes isolatedContext in page listing', async () => {await withMcpContext(async (response, context) => {await newPage().handler(- {params: {url: 'about:blank', isolatedContext: 'session-a'}},+ {+ params: {+ url: 'data:text/html,<html></html>',+ isolatedContext: 'session-a',+ },+ },response,context,);@@ -357,7 +387,7 @@ describe('pages', () => {const mcpPage = context.getSelectedMcpPage();assert.strictEqual(mcpPage.isolatedContextName, undefined);await newPage().handler(- {params: {url: 'about:blank'}},+ {params: {url: 'data:text/html,<html></html>'}},response,context,);@@ -371,7 +401,12 @@ describe('pages', () => {it('closes an isolated page without errors', async () => {await withMcpContext(async (response, context) => {await newPage().handler(- {params: {url: 'about:blank', isolatedContext: 'session-a'}},+ {+ params: {+ url: 'data:text/html,<html></html>',+ isolatedContext: 'session-a',+ },+ },response,context,);@@ -399,7 +434,7 @@ describe('pages', () => {const dialog = await dialogPromise;await newPage().handler(- {params: {url: 'about:blank'}},+ {params: {url: 'data:text/html,<html></html>'}},response,context,);@@ -559,7 +594,12 @@ describe('pages', () => {await withMcpContext(async (response, context) => {// Create pages in separate isolated contexts.await newPage().handler(- {params: {url: 'about:blank', isolatedContext: 'ctx-a'}},+ {+ params: {+ url: 'data:text/html,<html></html>',+ isolatedContext: 'ctx-a',+ },+ },response,context,);@@ -567,7 +607,12 @@ describe('pages', () => {const pageAId = context.getSelectedMcpPage().id;await newPage().handler(- {params: {url: 'about:blank', isolatedContext: 'ctx-b'}},+ {+ params: {+ url: 'data:text/html,<html></html>',+ isolatedContext: 'ctx-b',+ },+ },response,context,);@@ -683,7 +728,7 @@ describe('pages', () => {await navigatePage().handler({params: {- url: 'about:blank',+ url: 'data:text/html,<html></html>',timeout: 12345,},page: context.getSelectedMcpPage(),diff --git a/tests/tools/performance.test.ts b/tests/tools/performance.test.tsindex 4bcddb57..9c66442f 100644--- a/tests/tools/performance.test.ts+++ b/tests/tools/performance.test.ts@@ -47,6 +47,8 @@ describe('performance', () => {await withMcpContext(async (response, context) => {context.setIsRunningPerformanceTrace(false);const selectedPage = context.getSelectedMcpPage().pptrPage;+ sinon.stub(selectedPage, 'url').callsFake(() => 'https://www.test.com');+ sinon.stub(selectedPage, 'goto').resolves(null);const startTracingStub = sinon.stub(selectedPage.tracing, 'start');await startTrace.handler({@@ -82,7 +84,7 @@ describe('performance', () => {);sinon.assert.calledOnce(startTracingStub);sinon.assert.calledWithExactly(gotoStub, 'about:blank', {- waitUntil: ['networkidle0'],+ waitUntil: 'load',});sinon.assert.calledWithExactly(gotoStub, 'https://www.test.com', {waitUntil: ['load'],diff --git a/tests/tools/webmcp.test.ts b/tests/tools/webmcp.test.tsindex b80cf6a1..9087bffc 100644--- a/tests/tools/webmcp.test.ts+++ b/tests/tools/webmcp.test.ts@@ -17,7 +17,10 @@ describe('webmcp', () => {it('list webmcp tools in navigate_page response', async () => {await withMcpContext(async (response, context) => {await navigatePage().handler(- {params: {url: 'about:blank'}, page: context.getSelectedMcpPage()},+ {+ params: {url: 'data:text/html,<html></html>'},+ page: context.getSelectedMcpPage(),+ },response,context,);diff --git a/tests/utils.ts b/tests/utils.tsindex f2f12eee..61b75992 100644--- a/tests/utils.ts+++ b/tests/utils.ts@@ -78,37 +78,79 @@ export async function withBrowser(allowedUrlPattern?: string[];} = {},) {- const launchOptions: LaunchOptions = {- executablePath:- options.executablePath ?? process.env.PUPPETEER_EXECUTABLE_PATH,- headless: !options.debug,- defaultViewport: null,- devtools: options.autoOpenDevTools ?? false,- pipe: true,- handleDevToolsAsPage: true,- args: [...(options.args || []), '--screen-info={3840x2160}'],- enableExtensions: true,- blocklist: options.blockedUrlPattern,- allowlist: options.allowedUrlPattern,- };- const key = JSON.stringify(launchOptions);+ let attempt = 1;+ while (attempt <= 3) {+ const launchOptions: LaunchOptions = {+ executablePath:+ options.executablePath ?? process.env.PUPPETEER_EXECUTABLE_PATH,+ headless: !options.debug,+ defaultViewport: null,+ devtools: options.autoOpenDevTools ?? false,+ pipe: true,+ handleDevToolsAsPage: true,+ args: [...(options.args || []), '--screen-info={3840x2160}'],+ enableExtensions: true,+ blocklist: options.blockedUrlPattern,+ allowlist: options.allowedUrlPattern,+ };+ const key = JSON.stringify(launchOptions);++ let browser = browsers.get(key);+ if (!browser) {+ browser = await puppeteer.launch(launchOptions);+ browsers.set(key, browser);+ }- let browser = browsers.get(key);- if (!browser) {- browser = await puppeteer.launch(launchOptions);- browsers.set(key, browser);- }- const newPage = await browser.newPage();- // Close other pages.- await Promise.all(- (await browser.pages()).map(async page => {- if (page !== newPage) {- await page.close();+ try {+ await Promise.race([+ (async () => {+ const newPage = await browser.newPage();+ // Close other pages.+ await Promise.all(+ (await browser.pages()).map(async page => {+ if (page !== newPage) {+ await page.close();+ }+ }),+ );++ await cb(browser, newPage);+ })(),+ new Promise((_, reject) =>+ setTimeout(+ () => reject(new Error('withBrowser timeout exceeded')),+ 60000,+ ),+ ),+ ]);+ return;+ } catch (error) {+ browsers.delete(key);+ try {+ await Promise.race([+ browser.close(),+ new Promise((_, reject) =>+ setTimeout(() => reject(new Error('browser.close timeout')), 2000),+ ),+ ]);+ } catch {+ browser.process()?.kill('SIGKILL');}- }),- );- await cb(browser, newPage);+ const isRetryable =+ error instanceof Error &&+ (error.message === 'withBrowser timeout exceeded' ||+ error.message.includes('closed') ||+ error.message.includes('crash') ||+ error.message.includes('hang'));++ if (attempt === 3 || !isRetryable) {+ throw error;+ }+ attempt++;+ await new Promise(r => setTimeout(r, 1000));+ }+ }}export async function withMcpContext(