Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(cli): support attaching to a Playwright browser server endpoint (#41203)
Yury Semikhatsky2e36f24228e5
diff --git a/packages/playwright-core/src/tools/cli-client/program.ts b/packages/playwright-core/src/tools/cli-client/program.tsindex 1d9dc5d657..0b87a30bae 100644--- a/packages/playwright-core/src/tools/cli-client/program.ts+++ b/packages/playwright-core/src/tools/cli-client/program.ts@@ -168,7 +168,7 @@ export async function program(options?: { embedderVersion?: string}) {}const cdpChannel = typeof args.cdp === 'string' && isKnownChannel(args.cdp) ? args.cdp : undefined;- const targetName = attachTarget ?? cdpChannel ?? extensionChannel ?? args.cdp as string;+ const targetName = attachTarget ?? cdpChannel ?? extensionChannel ?? args.endpoint as string ?? args.cdp as string;if (!targetName)output.errorAttachNoTarget();const attachSessionName = explicitSessionName(args.session as string) ?? attachTarget ?? cdpChannel ?? extensionChannel ?? sessionName;diff --git a/packages/playwright-core/src/tools/mcp/browserFactory.ts b/packages/playwright-core/src/tools/mcp/browserFactory.tsindex e6b808459c..70cf0878e5 100644--- a/packages/playwright-core/src/tools/mcp/browserFactory.ts+++ b/packages/playwright-core/src/tools/mcp/browserFactory.ts@@ -147,6 +147,10 @@ async function createRemoteBrowser(config: FullConfig): Promise<BrowserWithInfo>// Use connectToBrowser instead of playwright[browserName].connect because we don't have browserName.const browser = await connectToBrowser(playwrightObject, remoteOptions);browser._connectToBrowserType(playwrightObject[browser._browserName], {}, undefined);+ // A browser started via `launchServer` exposes no contexts until one is+ // created, so create one when attaching to such a server.+ if (!browser.contexts().length)+ await browser.newContext(config.browser.contextOptions);return { browser, browserInfo: browserInfo(browser, config), canBind: false, ownership: 'attached' };}diff --git a/tests/mcp/cli-endpoint.spec.ts b/tests/mcp/cli-endpoint.spec.tsnew file mode 100644index 0000000000..0fa4519e32--- /dev/null+++ b/tests/mcp/cli-endpoint.spec.ts@@ -0,0 +1,31 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { test, expect } from './cli-fixtures';++test('attach --endpoint keeps the default session', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/41154' } }, async ({ wsEndpoint, cli, server }) => {+ const { exitCode, error } = await cli('attach', `--endpoint=${wsEndpoint}`);+ expect(error).not.toContain('no target specified');+ expect(exitCode).toBe(0);++ const { output: listOutput } = await cli('list');+ expect(listOutput).toContain('- default:');+ expect(listOutput).toContain('(attached)');++ await cli('goto', server.HELLO_WORLD);+ const { inlineSnapshot } = await cli('snapshot');+ expect(inlineSnapshot).toContain('Hello, world!');+});