From 4008a80049161deec1f2f1a758265378e97278f5 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 7 Jul 2026 18:05:31 +0200 Subject: [PATCH] fix(cli): don't crash the response when navigating closes the last tab Navigating the only tab to a chrome:// page like chrome://extensions/ closes that tab, leaving zero open tabs. The response builder then rendered the Page section unconditionally and crashed with a TypeError reading 'url' on an undefined tab, leaving the session unusable. The "no open tabs" state already exists (browser_close renders it), so guard the Page section on tabHeaders.length. Fixes: https://github.com/microsoft/playwright-cli/issues/433 --- packages/playwright-core/src/tools/backend/response.ts | 3 ++- tests/mcp/cli-navigation.spec.ts | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/playwright-core/src/tools/backend/response.ts b/packages/playwright-core/src/tools/backend/response.ts index 431d4c28ab127..78a2980970b19 100644 --- a/packages/playwright-core/src/tools/backend/response.ts +++ b/packages/playwright-core/src/tools/backend/response.ts @@ -275,7 +275,8 @@ export class Response { if (this._includeSnapshot !== 'none' || tabHeaders.some(header => header.changed)) { if (tabHeaders.length !== 1) addSection('Open tabs', renderTabsMarkdown(tabHeaders)); - addSection('Page', renderTabMarkdown(tabHeaders.find(h => h.current) ?? tabHeaders[0])); + if (tabHeaders.length) + addSection('Page', renderTabMarkdown(tabHeaders.find(h => h.current) ?? tabHeaders[0])); } // Handle modal states. diff --git a/tests/mcp/cli-navigation.spec.ts b/tests/mcp/cli-navigation.spec.ts index 74f606fadf051..3d07790288a86 100644 --- a/tests/mcp/cli-navigation.spec.ts +++ b/tests/mcp/cli-navigation.spec.ts @@ -52,3 +52,10 @@ test('run-code', async ({ cli, server }) => { const { output } = await cli('run-code', '() => page.title()'); expect(output).toContain('"Title"'); }); + +test('goto chrome:// page that closes the tab does not crash the response', async ({ cli, server, mcpBrowser }) => { + test.skip(mcpBrowser !== 'chromium' && mcpBrowser !== 'chrome', 'chrome:// pages are chromium-specific'); + await cli('open', server.HELLO_WORLD); + const { output } = await cli('goto', 'chrome://extensions/'); + expect(output).toContain('No open tabs. Navigate to a URL to create one.'); +});