From 65c63e6c48e743b4e51a3da9a9b887e7411b51c7 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Mon, 2 Nov 2026 12:13:58 -0800 Subject: [PATCH] fix(extension): report lost tab instead of crashing Fixes: https://github.com/microsoft/playwright/issues/43230 --- packages/extension/README.md | 3 ++ .../src/tools/backend/context.ts | 1 + .../src/tools/backend/response.ts | 10 ++++- tests/extension/extension-fixtures.ts | 9 ++-- tests/extension/scheme-detach.spec.ts | 43 +++++++++++++++++++ 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/packages/extension/README.md b/packages/extension/README.md index 15404f3fdc8b2..1695c909c5f0a 100644 --- a/packages/extension/README.md +++ b/packages/extension/README.md @@ -95,3 +95,6 @@ If the extension is installed in several Chrome profiles, the connection is made The `PLAYWRIGHT_MCP_PROFILE_DIR_NAME` environment variable can be used instead of the option. The authentication token is specific to the profile, so use the one displayed in that profile. +## Troubleshooting + +Chrome does not allow extensions to debug a page that contains a frame with a restricted URL, for example a page of another extension (`chrome-extension://`), a `chrome://` page, or a URL with a custom scheme. When such a frame loads, Chrome detaches the Playwright debugger. If the frame stays in the page, the tab is no longer available to the client. This often happens when another extension adds its own frame to every page. Disable that extension on the sites you automate, set its **Site access** to **On click**, or use a profile without it. Then navigate again. diff --git a/packages/playwright-core/src/tools/backend/context.ts b/packages/playwright-core/src/tools/backend/context.ts index aa13b7aad2b8c..e2306ff9c73a1 100644 --- a/packages/playwright-core/src/tools/backend/context.ts +++ b/packages/playwright-core/src/tools/backend/context.ts @@ -43,6 +43,7 @@ export type ContextConfig = { capabilities?: ToolCapability[]; codegen?: 'typescript' | 'python' | 'java' | 'csharp' | 'none'; console?: { level?: 'error' | 'warning' | 'info' | 'debug' }; + extension?: boolean; imageResponses?: 'allow' | 'omit' | 'only'; filePaths?: 'relative' | 'absolute'; network?: { diff --git a/packages/playwright-core/src/tools/backend/response.ts b/packages/playwright-core/src/tools/backend/response.ts index a54e6c2fb467c..fc644cbcc8a74 100644 --- a/packages/playwright-core/src/tools/backend/response.ts +++ b/packages/playwright-core/src/tools/backend/response.ts @@ -295,7 +295,15 @@ 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])); + const currentTab = tabHeaders.find(h => h.current) ?? tabHeaders[0]; + if (currentTab) { + addSection('Page', renderTabMarkdown(currentTab)); + } else if (this._includeSnapshot !== 'none' && !this._errors.length) { + const error = ['No open tabs. The page was closed before a snapshot could be captured.']; + if (this._context.config.extension) + error.push(`Chrome may have detached the debugger because the page contains a frame that extensions are not allowed to debug, for example a frame added by another extension. If so, set that extension's site access to "On click" and navigate again.`); + addSection('Error', error); + } } // Handle modal states. diff --git a/tests/extension/extension-fixtures.ts b/tests/extension/extension-fixtures.ts index 760c78fdaf8f3..76787c2977074 100644 --- a/tests/extension/extension-fixtures.ts +++ b/tests/extension/extension-fixtures.ts @@ -28,7 +28,7 @@ import type { StartClient } from '../mcp/fixtures'; export type BrowserWithExtension = { userDataDir: string; - launch: (mode?: 'disable-extension') => Promise; + launch: (mode?: 'disable-extension', additionalExtensions?: string[]) => Promise; }; export type CliResult = { @@ -65,7 +65,8 @@ export const test = base.extend({ await fs.mkdir(path.join(userDataDir, 'Default', 'Extensions', extensionId), { recursive: true }); await use({ userDataDir, - launch: async (mode?: 'disable-extension') => { + launch: async (mode?: 'disable-extension', additionalExtensions: string[] = []) => { + const extensions = [pathToExtension, ...additionalExtensions].join(','); browserContext = await chromium.launchPersistentContext(userDataDir, { channel: mcpBrowser, // Opening the browser singleton only works in headed. @@ -73,8 +74,8 @@ export const test = base.extend({ // Automation disables singleton browser process behavior, which is necessary for the extension. ignoreDefaultArgs: ['--enable-automation'], args: mode === 'disable-extension' ? [] : [ - `--disable-extensions-except=${pathToExtension}`, - `--load-extension=${pathToExtension}`, + `--disable-extensions-except=${extensions}`, + `--load-extension=${extensions}`, ], }); diff --git a/tests/extension/scheme-detach.spec.ts b/tests/extension/scheme-detach.spec.ts index 5099a5d4587d4..d8a6a3a23ce3f 100644 --- a/tests/extension/scheme-detach.spec.ts +++ b/tests/extension/scheme-detach.spec.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +import fs from 'fs/promises'; +import path from 'path'; import { test, expect, extensionId, connectWithToken } from './extension-fixtures'; import type { BrowserContext } from 'playwright'; @@ -98,3 +100,44 @@ test('recovers and can screenshot after the debugger is detached', { code: expect.stringContaining('page.screenshot'), }); }); + +test('reports an error instead of crashing when another extension frame detaches the debugger', { + annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43230' }, +}, async ({ browserWithExtension, startClient, server }, testInfo) => { + const injector = testInfo.outputPath('frame-injector'); + await fs.mkdir(injector); + await fs.writeFile(path.join(injector, 'manifest.json'), JSON.stringify({ + manifest_version: 3, + name: 'Frame injector', + version: '1.0', + content_scripts: [{ matches: ['http://*/*'], js: ['content.js'], run_at: 'document_idle' }], + web_accessible_resources: [{ resources: ['frame.html'], matches: [''] }], + })); + await fs.writeFile(path.join(injector, 'content.js'), ` + if (location.pathname === '/hello-world') { + const frame = document.createElement('iframe'); + frame.src = chrome.runtime.getURL('frame.html'); + document.documentElement.appendChild(frame); + } + `); + await fs.writeFile(path.join(injector, 'frame.html'), 'Foreign frame'); + + const browserContext = await browserWithExtension.launch(undefined, [injector]); + const { client } = await connectWithToken(browserContext, startClient, browserWithExtension.userDataDir); + const getDetach = await watchDetach(browserContext); + + const response = await client.callTool({ name: 'browser_navigate', arguments: { url: server.HELLO_WORLD } }); + expect(response).toHaveResponse({ + isError: true, + error: expect.stringContaining('frame that extensions are not allowed to debug'), + }); + expect(JSON.stringify(response)).not.toContain('TypeError'); + await expect.poll(getDetach).toEqual(expect.arrayContaining([ + expect.objectContaining({ reason: 'target_closed' }), + ])); + + server.setContent('/recovered', 'Recovered', 'text/html'); + expect(await client.callTool({ name: 'browser_navigate', arguments: { url: server.PREFIX + '/recovered' } })).toHaveResponse({ + snapshot: expect.stringContaining('Recovered'), + }); +});