Skip to content

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

chore: do not advertise as testing in cli/mcp (#40359)

Pavel Feldmand14974527597
diff --git a/packages/playwright-core/src/tools/mcp/browserFactory.ts b/packages/playwright-core/src/tools/mcp/browserFactory.ts
index cc048c9bff..9119c22aee 100644
--- a/packages/playwright-core/src/tools/mcp/browserFactory.ts
+++ b/packages/playwright-core/src/tools/mcp/browserFactory.ts
@@ -92,7 +92,6 @@ function browserInfo(browser: playwrightTypes.Browser, config: FullConfig): Brow
async function createIsolatedBrowser(config: FullConfig, clientInfo: ClientInfo): Promise<playwrightTypes.Browser> {
testDebug('create browser (isolated)');
- await injectCdpPort(config.browser);
const browserType = playwright[config.browser.browserName];
const tracesDir = await computeTracesDir(config, clientInfo);
const browser = await browserType.launch({
@@ -145,7 +144,6 @@ async function createRemoteBrowser(config: FullConfig): Promise<BrowserWithInfo>
async function createPersistentBrowser(config: FullConfig, clientInfo: ClientInfo): Promise<playwrightTypes.Browser> {
testDebug('create browser (persistent)');
- await injectCdpPort(config.browser);
const userDataDir = config.browser.userDataDir ?? await createUserDataDir(config, clientInfo);
const tracesDir = await computeTracesDir(config, clientInfo);
@@ -194,24 +192,6 @@ async function createUserDataDir(config: FullConfig, clientInfo: ClientInfo) {
return result;
}
-async function injectCdpPort(browserConfig: FullConfig['browser']) {
- if (browserConfig.browserName === 'chromium') {
- browserConfig.launchOptions.args = browserConfig.launchOptions.args ?? [];
- browserConfig.launchOptions.args?.push(`--remote-debugging-port=${await findFreePort()}`);
- }
-}
-
-async function findFreePort(): Promise<number> {
- return new Promise((resolve, reject) => {
- const server = net.createServer();
- server.listen(0, '127.0.0.1', () => {
- const { port } = server.address() as net.AddressInfo;
- server.close(() => resolve(port));
- });
- server.on('error', reject);
- });
-}
-
function createHash(data: string): string {
return crypto.createHash('sha256').update(data).digest('hex').slice(0, 7);
}
diff --git a/packages/playwright-core/src/tools/mcp/config.ts b/packages/playwright-core/src/tools/mcp/config.ts
index 9f24c2cbae..8e53d184f1 100644
--- a/packages/playwright-core/src/tools/mcp/config.ts
+++ b/packages/playwright-core/src/tools/mcp/config.ts
@@ -215,6 +215,13 @@ async function validateBrowserConfig(browser: MergedConfig['browser']): Promise<
else
browser.contextOptions.viewport = null;
}
+
+ if (browserName === 'chromium') {
+ browser.launchOptions.args = browser.launchOptions.args ?? [];
+ if (!browser.launchOptions.args.some(a => a.includes('--disable-blink-features')))
+ browser.launchOptions.args.push(`--disable-blink-features=AutomationControlled`);
+ }
+
return { ...browser, browserName };
}
diff --git a/tests/mcp/launch.spec.ts b/tests/mcp/launch.spec.ts
index 4019b530e7..031581f6fa 100644
--- a/tests/mcp/launch.spec.ts
+++ b/tests/mcp/launch.spec.ts
@@ -221,14 +221,6 @@ exit 1
isError: true,
error: 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({
- isError: true,
- error: expect.stringContaining(`Failed to launch the browser process.`),
- });
- }
expect.soft(result).toHaveResponse({
isError: true,
error: expect.not.stringContaining(`Browser is already in use`),
diff --git a/tests/mcp/webdriver.spec.ts b/tests/mcp/webdriver.spec.ts
new file mode 100644
index 0000000000..99496cb187
--- /dev/null
+++ b/tests/mcp/webdriver.spec.ts
@@ -0,0 +1,40 @@
+/**
+ * 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 './fixtures';
+
+test('do not falsely advertise user agent as a test driver', async ({ client, server, mcpBrowser }) => {
+ test.skip(mcpBrowser === 'firefox');
+ test.skip(mcpBrowser === 'webkit');
+ server.setRoute('/', (req, res) => {
+ res.writeHead(200, { 'Content-Type': 'text/html' });
+ res.end(`
+ <body></body>
+ <script>
+ document.body.textContent = 'webdriver: ' + navigator.webdriver;
+ </script>
+ `);
+ });
+
+ expect(await client.callTool({
+ name: 'browser_navigate',
+ arguments: {
+ url: server.PREFIX,
+ },
+ })).toHaveResponse({
+ snapshot: expect.stringContaining(`webdriver: false`),
+ });
+});