Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/playwright-core/src/tools/mcp/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ export type CLIOptions = {
viewportSize?: ViewportSize;
};

export const defaultCodegenLanguage: 'typescript' | 'python' | 'java' | 'csharp' =
process.env.PW_LANG_NAME === 'python' || process.env.PW_LANG_NAME === 'java' || process.env.PW_LANG_NAME === 'csharp'
? process.env.PW_LANG_NAME
: 'typescript';

const defaultConfig: MergedConfig = {
browser: {
launchOptions: {},
contextOptions: {},
},
codegen: defaultCodegenLanguage,
timeouts: {
action: 5000,
navigation: 60000,
Expand Down Expand Up @@ -406,6 +412,8 @@ export function configFromEnv(env?: NodeJS.ProcessEnv): Config & { configFile?:
options.cdpEndpoint = envToString(e.PLAYWRIGHT_MCP_CDP_ENDPOINT);
options.cdpHeader = headerParser(envToString(e.PLAYWRIGHT_MCP_CDP_HEADERS));
options.cdpTimeout = numberParser(e.PLAYWRIGHT_MCP_CDP_TIMEOUT);
if (e.PLAYWRIGHT_MCP_CODEGEN)
options.codegen = enumParser<'typescript' | 'python' | 'java' | 'csharp' | 'none'>('--codegen', ['none', 'typescript', 'python', 'java', 'csharp'], e.PLAYWRIGHT_MCP_CODEGEN);
options.config = envToString(e.PLAYWRIGHT_MCP_CONFIG);
if (e.PLAYWRIGHT_MCP_CONSOLE_LEVEL)
options.consoleLevel = enumParser<'error' | 'warning' | 'info' | 'debug'>('--console-level', ['error', 'warning', 'info', 'debug'], e.PLAYWRIGHT_MCP_CONSOLE_LEVEL);
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/tools/mcp/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { Option as ProgramOption } from 'commander';
import * as mcpServer from '../utils/mcp/server';
import { commaSeparatedList, dotenvFileLoader, enumParser, headerParser, numberParser, resolutionParser, resolveCLIConfigForMCP, semicolonSeparatedList } from './config';
import { commaSeparatedList, defaultCodegenLanguage, dotenvFileLoader, enumParser, headerParser, numberParser, resolutionParser, resolveCLIConfigForMCP, semicolonSeparatedList } from './config';
import { setupExitWatchdog } from './watchdog';
import { createBrowserWithInfo } from './browserFactory';
import { BrowserBackend } from '../backend/browserBackend';
Expand All @@ -42,7 +42,7 @@ export function decorateMCPCommand(command: Command) {
.option('--cdp-endpoint <endpoint>', 'CDP endpoint to connect to.')
.option('--cdp-header <headers...>', 'CDP headers to send with the connect request, multiple can be specified.', headerParser)
.option('--cdp-timeout <timeout>', 'timeout in milliseconds for connecting to CDP endpoint, defaults to 30000ms', numberParser)
.option('--codegen <lang>', 'specify the language to use for code generation, possible values: "typescript", "python", "java", "csharp", "none". Default is "typescript".', enumParser.bind(null, '--codegen', ['none', 'typescript', 'python', 'java', 'csharp']))
.option('--codegen <lang>', `specify the language to use for code generation, possible values: "typescript", "python", "java", "csharp", "none". Default is "${defaultCodegenLanguage}".`, enumParser.bind(null, '--codegen', ['none', 'typescript', 'python', 'java', 'csharp']))
.option('--config <path>', 'path to the configuration file.')
.option('--console-level <level>', 'level of console messages to return: "error", "warning", "info", "debug". Each level includes the messages of more severe levels.', enumParser.bind(null, '--console-level', ['error', 'warning', 'info', 'debug']))
.option('--device <device>', 'device to emulate, for example: "iPhone 15"')
Expand Down
14 changes: 14 additions & 0 deletions tests/mcp/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ async function navigateToForm(client: Client, server: any) {
});
}

test('codegen defaults to PW_LANG_NAME', async ({ startClient, server }) => {
const { client } = await startClient({ env: { PW_LANG_NAME: 'python' } });
expect(await navigateToForm(client, server)).toHaveResponse({
code: `page.goto("${server.PREFIX}")`,
});

expect(await client.callTool({
name: 'browser_click',
arguments: { element: 'Submit button', target: 'e2' },
})).toHaveResponse({
code: `page.get_by_role("button", name="Submit").click()`,
});
});

test('codegen python', async ({ startClient, server }) => {
const { client } = await startClient({ args: ['--codegen=python'] });
expect(await navigateToForm(client, server)).toHaveResponse({
Expand Down
Loading