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
3 changes: 3 additions & 0 deletions packages/playwright-core/src/server/agent/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ export type ExpectValue = {
};

export type Action = ClickAction | DragAction | HoverAction | SelectOptionAction | PressAction | PressSequentiallyAction | FillAction | SetChecked | ExpectVisible | ExpectValue;
export type ActionWithCode = Action & {
code: string;
};
77 changes: 77 additions & 0 deletions packages/playwright-core/src/server/agent/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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 { asLocator } from '../../utils/isomorphic/locatorGenerators';
import { escapeWithQuotes, formatObjectOrVoid } from '../../utils/isomorphic/stringUtils';

import type * as actions from './actions';
import type { Language } from '../../utils/isomorphic/locatorGenerators';

export async function generateCode(sdkLanguage: Language, action: actions.Action) {
switch (action.method) {
case 'click': {
const locator = asLocator(sdkLanguage, action.selector);
return `await page.${locator}.click(${formatObjectOrVoid(action.options)});`;
}
case 'drag': {
const sourceLocator = asLocator(sdkLanguage, action.sourceSelector);
const targetLocator = asLocator(sdkLanguage, action.targetSelector);
return `await page.${sourceLocator}.dragAndDrop(${targetLocator});`;
}
case 'hover': {
const locator = asLocator(sdkLanguage, action.selector);
return `await page.${locator}.hover(${formatObjectOrVoid(action.options)});`;
}
case 'pressKey': {
return `await page.keyboard.press(${escapeWithQuotes(action.key, '\'')});`;
}
case 'selectOption': {
const locator = asLocator(sdkLanguage, action.selector);
return `await page.${locator}.selectOption(${action.labels.length === 1 ? escapeWithQuotes(action.labels[0]) : '[' + action.labels.map(label => escapeWithQuotes(label)).join(', ') + ']'});`;
}
case 'pressSequentially': {
const locator = asLocator(sdkLanguage, action.selector);
const code = [`await page.${locator}.pressSequentially(${escapeWithQuotes(action.text)});`];
if (action.submit)
code.push(`await page.keyboard.press('Enter');`);
return code.join('\n');
}
case 'fill': {
const locator = asLocator(sdkLanguage, action.selector);
const code = [`await page.${locator}.fill(${escapeWithQuotes(action.text)});`];
if (action.submit)
code.push(`await page.keyboard.press('Enter');`);
return code.join('\n');
}
case 'setChecked': {
const locator = asLocator(sdkLanguage, action.selector);
if (action.checked)
return `await page.${locator}.check();`;
else
return `await page.${locator}.uncheck();`;
}
case 'expectVisible': {
const locator = asLocator(sdkLanguage, action.selector);
return `await expect(page.${locator}).toBeVisible();`;
}
case 'expectValue': {
const locator = asLocator(sdkLanguage, action.selector);
return `await expect(page.${locator}).toHaveValue(${escapeWithQuotes(action.value)});`;
}
}
// @ts-expect-error
throw new Error('Unknown action ' + action.method);
}
9 changes: 7 additions & 2 deletions packages/playwright-core/src/server/agent/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@

import { BrowserContext } from '../browserContext';
import { runAction } from './actionRunner';
import { generateCode } from './codegen';

import type { Request } from '../network';
import type * as loopTypes from '@lowire/loop';
import type * as actions from './actions';
import type { Page } from '../page';
import type { Progress } from '../progress';
import type { BrowserContextOptions } from '../types';
import type { Language } from '../../utils/isomorphic/locatorGenerators.ts';

type AgentOptions = BrowserContextOptions['agent'];

export class Context {
readonly options: AgentOptions;
readonly progress: Progress;
readonly page: Page;
readonly actions: actions.Action[] = [];
readonly actions: actions.ActionWithCode[] = [];
readonly sdkLanguage: Language;

constructor(progress: Progress, page: Page) {
this.progress = progress;
this.page = page;
this.options = page.browserContext._options.agent;
this.sdkLanguage = page.browserContext._browser.sdkLanguage();
}

async runActionAndWait(action: actions.Action) {
Expand All @@ -47,7 +51,8 @@ export class Context {
await this.waitForCompletion(async () => {
for (const a of action) {
await runAction(this.progress, this.page, a, this.options?.secrets ?? []);
this.actions.push(a);
const code = await generateCode(this.sdkLanguage, a);
this.actions.push({ ...a, code });
}
});
return await this.snapshotResult();
Expand Down
24 changes: 1 addition & 23 deletions packages/playwright-core/src/server/codegen/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { sanitizeDeviceOptions, toClickOptionsForSourceCode, toKeyboardModifiers, toSignalMap } from './language';
import { asLocator, escapeWithQuotes } from '../../utils';
import { asLocator, escapeWithQuotes, formatObject, formatObjectOrVoid } from '../../utils';
import { deviceDescriptors } from '../deviceDescriptors';

import type { Language, LanguageGenerator, LanguageGeneratorOptions } from './types';
Expand Down Expand Up @@ -190,28 +190,6 @@ function formatOptions(value: any, hasArguments: boolean): string {
return (hasArguments ? ', ' : '') + formatObject(value);
}

function formatObject(value: any, indent = ' '): string {
if (typeof value === 'string')
return quote(value);
if (Array.isArray(value))
return `[${value.map(o => formatObject(o)).join(', ')}]`;
if (typeof value === 'object') {
const keys = Object.keys(value).filter(key => value[key] !== undefined).sort();
if (!keys.length)
return '{}';
const tokens: string[] = [];
for (const key of keys)
tokens.push(`${key}: ${formatObject(value[key])}`);
return `{\n${indent}${tokens.join(`,\n${indent}`)}\n}`;
}
return String(value);
}

function formatObjectOrVoid(value: any, indent = ' '): string {
const result = formatObject(value, indent);
return result === '{}' ? '' : result;
}

function formatContextOptions(options: BrowserContextOptions, deviceName: string | undefined, isTest: boolean): string {
const device = deviceName && deviceDescriptors[deviceName];
// recordHAR is replaced with routeFromHAR in the generated code.
Expand Down
24 changes: 24 additions & 0 deletions packages/playwright-core/src/utils/isomorphic/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ export function toSnakeCase(name: string): string {
return name.replace(/([a-z0-9])([A-Z])/g, '$1_$2').replace(/([A-Z])([A-Z][a-z])/g, '$1_$2').toLowerCase();
}

export function formatObject(value: any, indent = ' ', mode: 'multiline' | 'oneline' = 'multiline'): string {
if (typeof value === 'string')
return escapeWithQuotes(value, '\'');
if (Array.isArray(value))
return `[${value.map(o => formatObject(o)).join(', ')}]`;
if (typeof value === 'object') {
const keys = Object.keys(value).filter(key => value[key] !== undefined).sort();
if (!keys.length)
return '{}';
const tokens: string[] = [];
for (const key of keys)
tokens.push(`${key}: ${formatObject(value[key])}`);
if (mode === 'multiline')
return `{\n${tokens.join(`,\n${indent}`)}\n}`;
return `{ ${tokens.join(', ')} }`;
}
return String(value);
}

export function formatObjectOrVoid(value: any, indent = ' '): string {
const result = formatObject(value, indent);
return result === '{}' ? '' : result;
}

export function quoteCSSAttributeValue(text: string): string {
return `"${text.replace(/["\\]/g, char => '\\' + char)}"`;
}
Expand Down
55 changes: 0 additions & 55 deletions packages/playwright/src/mcp/browser/codegen.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/playwright/src/mcp/browser/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import fs from 'fs';

import { debug } from 'playwright-core/lib/utilsBundle';
import { escapeWithQuotes } from 'playwright-core/lib/utils';
import { selectors } from 'playwright-core';

import { logUnhandledError } from '../log';
import { Tab } from './tab';
import { outputFile } from './config';
import * as codegen from './codegen';
import { dateAsFileName } from './tools/utils';

import type * as playwright from '../../../types/test';
Expand Down Expand Up @@ -256,7 +256,7 @@ export class Context {

lookupSecret(secretName: string): { value: string, code: string } {
if (!this.config.secrets?.[secretName])
return { value: secretName, code: codegen.quote(secretName) };
return { value: secretName, code: escapeWithQuotes(secretName, '\'') };
return {
value: this.config.secrets[secretName]!,
code: `process.env['${secretName}']`,
Expand Down
7 changes: 4 additions & 3 deletions packages/playwright/src/mcp/browser/tools/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

import { z } from 'playwright-core/lib/mcpBundle';
import { escapeWithQuotes } from 'playwright-core/lib/utils';

import { defineTabTool } from './tool';
import * as javascript from '../codegen';

import type { Tab } from '../tab';

Expand All @@ -42,9 +43,9 @@ const evaluate = defineTabTool({
let locator: Awaited<ReturnType<Tab['refLocator']>> | undefined;
if (params.ref && params.element) {
locator = await tab.refLocator({ ref: params.ref, element: params.element });
response.addCode(`await page.${locator.resolved}.evaluate(${javascript.quote(params.function)});`);
response.addCode(`await page.${locator.resolved}.evaluate(${escapeWithQuotes(params.function)});`);
} else {
response.addCode(`await page.evaluate(${javascript.quote(params.function)});`);
response.addCode(`await page.evaluate(${escapeWithQuotes(params.function)});`);
}

await tab.waitForCompletion(async () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/playwright/src/mcp/browser/tools/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

import { z } from 'playwright-core/lib/mcpBundle';
import { escapeWithQuotes } from 'playwright-core/lib/utils';

import { defineTabTool } from './tool';
import * as codegen from '../codegen';

const fillForm = defineTabTool({
capability: 'core',
Expand Down Expand Up @@ -49,7 +50,7 @@ const fillForm = defineTabTool({
response.addCode(`${locatorSource}.setChecked(${field.value});`);
} else if (field.type === 'combobox') {
await locator.selectOption({ label: field.value });
response.addCode(`${locatorSource}.selectOption(${codegen.quote(field.value)});`);
response.addCode(`${locatorSource}.selectOption(${escapeWithQuotes(field.value)});`);
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions packages/playwright/src/mcp/browser/tools/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

import { z } from 'playwright-core/lib/mcpBundle';
import { formatObject } from 'playwright-core/lib/utils';

import { defineTabTool } from './tool';
import * as javascript from '../codegen';
import { dateAsFileName } from './utils';

const pdfSchema = z.object({
Expand All @@ -36,7 +37,7 @@ const pdf = defineTabTool({

handle: async (tab, params, response) => {
const fileName = await response.addFile(params.filename ?? dateAsFileName('pdf'), { origin: 'llm', reason: 'Page saved as PDF' });
response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
response.addCode(`await page.pdf(${formatObject({ path: fileName })});`);
await tab.page.pdf({ path: fileName });
},
});
Expand Down
6 changes: 3 additions & 3 deletions packages/playwright/src/mcp/browser/tools/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import fs from 'fs';

import { mkdirIfNeeded, scaleImageToSize } from 'playwright-core/lib/utils';
import { jpegjs, PNG } from 'playwright-core/lib/utilsBundle';
import { formatObject } from 'playwright-core/lib/utils';

import { z } from 'playwright-core/lib/mcpBundle';
import { defineTabTool } from './tool';
import * as javascript from '../codegen';
import { dateAsFileName } from './utils';

import type * as playwright from 'playwright-core';
Expand Down Expand Up @@ -67,9 +67,9 @@ const screenshot = defineTabTool({
const ref = params.ref ? await tab.refLocator({ element: params.element || '', ref: params.ref }) : null;

if (ref)
response.addCode(`await page.${ref.resolved}.screenshot(${javascript.formatObject(options)});`);
response.addCode(`await page.${ref.resolved}.screenshot(${formatObject(options)});`);
else
response.addCode(`await page.screenshot(${javascript.formatObject(options)});`);
response.addCode(`await page.screenshot(${formatObject(options)});`);

const buffer = ref ? await ref.locator.screenshot(options) : await tab.page.screenshot(options);

Expand Down
7 changes: 4 additions & 3 deletions packages/playwright/src/mcp/browser/tools/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import fs from 'fs';

import { z } from 'playwright-core/lib/mcpBundle';
import { formatObject } from 'playwright-core/lib/utils';

import { defineTabTool, defineTool } from './tool';
import * as javascript from '../codegen';

const snapshot = defineTool({
capability: 'core',
Expand Down Expand Up @@ -73,7 +74,7 @@ const click = defineTabTool({
button: params.button,
modifiers: params.modifiers,
};
const formatted = javascript.formatObject(options, ' ', 'oneline');
const formatted = formatObject(options, ' ', 'oneline');
const optionsAttr = formatted !== '{}' ? formatted : '';

if (params.doubleClick)
Expand Down Expand Up @@ -161,7 +162,7 @@ const selectOption = defineTabTool({
response.setIncludeSnapshot();

const { locator, resolved } = await tab.refLocator(params);
response.addCode(`await page.${resolved}.selectOption(${javascript.formatObject(params.values)});`);
response.addCode(`await page.${resolved}.selectOption(${formatObject(params.values)});`);

await tab.waitForCompletion(async () => {
await locator.selectOption(params.values);
Expand Down
Loading
Loading