Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(mcp): misleading error message when browser fails to launch (#38767)

Yury Semikhatsky475da4a64986
diff --git a/packages/playwright-core/src/server/browserType.ts b/packages/playwright-core/src/server/browserType.ts
index 8bb1fb4bb2..3182f671b9 100644
--- a/packages/playwright-core/src/server/browserType.ts
+++ b/packages/playwright-core/src/server/browserType.ts
@@ -264,6 +264,8 @@ export abstract class BrowserType extends SdkObject {
this.waitForReadyState(options, browserLogsCollector),
exitPromise.then(() => ({ wsEndpoint: undefined })),
]);
+ if (exitPromise.isDone())
+ throw new Error(`Failed to launch the browser process.`);
if (options.cdpPort !== undefined || !this.supportsPipeTransport()) {
transport = await WebSocketTransport.connect(progress, wsEndpoint!);
} else {
diff --git a/packages/playwright/src/mcp/browser/browserContextFactory.ts b/packages/playwright/src/mcp/browser/browserContextFactory.ts
index b064f49785..0158b4e841 100644
--- a/packages/playwright/src/mcp/browser/browserContextFactory.ts
+++ b/packages/playwright/src/mcp/browser/browserContextFactory.ts
@@ -224,7 +224,9 @@ class PersistentContextFactory implements BrowserContextFactory {
} catch (error: any) {
if (error.message.includes('Executable doesn\'t exist'))
throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
- if (error.message.includes('ProcessSingleton') || error.message.includes('Invalid URL')) {
+ if (error.message.includes('ProcessSingleton') ||
+ // On Windows the process exits silently with code 21 when the profile is in use.
+ error.message.includes('exitCode=21')) {
// User data directory is already in use, try again.
await new Promise(resolve => setTimeout(resolve, 1000));
continue;
diff --git a/tests/mcp/launch.spec.ts b/tests/mcp/launch.spec.ts
index f371035351..c008c72e86 100644
--- a/tests/mcp/launch.spec.ts
+++ b/tests/mcp/launch.spec.ts
@@ -175,3 +175,37 @@ test('isolated context with storage state', async ({ startClient, server }, test
pageState: expect.stringContaining(`Storage: session-value`),
});
});
+
+test('proper launch error message for broken browser and persistent context', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright-mcp/issues/1305' }
+}, async ({ startClient, server, mcpBrowser }, testInfo) => {
+ test.skip(process.platform === 'win32', 'Skipping on Windows because we need /bin/sh.');
+ const scriptPath = testInfo.outputPath('launcher.sh');
+ const scriptContent = `#!/bin/sh
+echo "Bogus browser script"
+exit 1
+`;
+ await fs.promises.writeFile(scriptPath, scriptContent, { mode: 0o755 });
+
+ const { client } = await startClient({
+ args: [`--executable-path=${scriptPath}`],
+ });
+ const result = await client.callTool({
+ name: 'browser_navigate',
+ arguments: { url: server.PREFIX },
+ });
+ expect.soft(result).toHaveResponse({
+ isError: true,
+ result: expect.stringContaining(`Bogus browser script`),
+ });
+ // Chromium waits for the CDP endpoint, so we know if the process failed to launch
+ // before connecting.
+ if (mcpBrowser === 'chromium') {
+ expect.soft(result).toHaveResponse({
+ result: expect.stringContaining(`Failed to launch the browser process.`),
+ });
+ }
+ expect.soft(result).toHaveResponse({
+ result: expect.not.stringContaining(`Browser is already in use`),
+ });
+});