Skip to content
Closed
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
9 changes: 7 additions & 2 deletions packages/playwright/src/mcp/browser/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { requireOrImport } from '../../transform/transform';
import type { Context } from './context';
import type { Page } from '../../../../playwright-core/src/client/page';
import type { Locator } from '../../../../playwright-core/src/client/locator';
import type { Response } from './response';

export const TabEvents = {
modalState: 'modalState'
Expand Down Expand Up @@ -337,9 +338,13 @@ export class Tab extends EventEmitter<TabEventsInterface> {
]);
}

async waitForCompletion(callback: () => Promise<void>) {
async waitForCompletion(response: Response, callback: () => Promise<void>) {
await this._initializedPromise;
await this._raceAgainstModalStates(() => waitForCompletion(this, callback));
await this._raceAgainstModalStates(async () => {
const { trivial } = await waitForCompletion(this, callback);
if (!trivial)
response.setIncludeSnapshot();
});
}

async refLocator(params: { element?: string, ref: string }): Promise<{ locator: Locator, resolved: string }> {
Expand Down
5 changes: 4 additions & 1 deletion packages/playwright/src/mcp/browser/tools/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ const resize = defineTabTool({

handle: async (tab, params, response) => {
response.addCode(`await page.setViewportSize({ width: ${params.width}, height: ${params.height} });`);
await tab.page.setViewportSize({ width: params.width, height: params.height });

await tab.waitForCompletion(response, async () => {
await tab.page.setViewportSize({ width: params.width, height: params.height });
});
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/src/mcp/browser/tools/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const handleDialog = defineTabTool({
throw new Error('No dialog visible');

tab.clearModalState(dialogState);
await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
if (params.accept)
await dialogState.dialog.accept(params.promptText);
else
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/src/mcp/browser/tools/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const evaluate = defineTabTool({
response.addCode(`await page.evaluate(${escapeWithQuotes(params.function)});`);
}

await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
const receiver = locator?.locator ?? tab.page;
const result = await receiver._evaluateFunction(params.function);
const text = JSON.stringify(result, null, 2) || 'undefined';
Expand Down
4 changes: 1 addition & 3 deletions packages/playwright/src/mcp/browser/tools/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ export const uploadFile = defineTabTool({
},

handle: async (tab, params, response) => {
response.setIncludeSnapshot();

const modalState = tab.modalStates().find(state => state.type === 'fileChooser');
if (!modalState)
throw new Error('No file chooser visible');

response.addCode(`await fileChooser.setFiles(${JSON.stringify(params.paths)})`);

tab.clearModalState(modalState);
await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
if (params.paths)
await modalState.fileChooser.setFiles(params.paths);
});
Expand Down
13 changes: 7 additions & 6 deletions packages/playwright/src/mcp/browser/tools/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ const pressSequentially = defineTabTool({
await tab.page.keyboard.type(params.text);
if (params.submit) {
response.addCode(`await page.keyboard.press('Enter');`);
response.setIncludeSnapshot();
await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
await tab.page.keyboard.press('Enter');
});
}
Expand All @@ -87,9 +86,8 @@ const type = defineTabTool({
const { locator, resolved } = await tab.refLocator(params);
const secret = tab.context.lookupSecret(params.text);

await tab.waitForCompletion(async () => {
const body = async () => {
if (params.slowly) {
response.setIncludeSnapshot();
response.addCode(`await page.${resolved}.pressSequentially(${secret.code});`);
await locator.pressSequentially(secret.value);
} else {
Expand All @@ -98,11 +96,14 @@ const type = defineTabTool({
}

if (params.submit) {
response.setIncludeSnapshot();
response.addCode(`await page.${resolved}.press('Enter');`);
await locator.press('Enter');
}
});
};
if (params.submit)
await tab.waitForCompletion(response, body);
else
await body();
},
});

Expand Down
13 changes: 3 additions & 10 deletions packages/playwright/src/mcp/browser/tools/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ const mouseMove = defineTabTool({
handle: async (tab, params, response) => {
response.addCode(`// Move mouse to (${params.x}, ${params.y})`);
response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);

await tab.waitForCompletion(async () => {
await tab.page.mouse.move(params.x, params.y);
});
await tab.page.mouse.move(params.x, params.y);
},
});

Expand Down Expand Up @@ -114,14 +111,12 @@ const mouseClick = defineTabTool({
},

handle: async (tab, params, response) => {
response.setIncludeSnapshot();

response.addCode(`// Click mouse at coordinates (${params.x}, ${params.y})`);
response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
response.addCode(`await page.mouse.down();`);
response.addCode(`await page.mouse.up();`);

await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
await tab.page.mouse.move(params.x, params.y);
await tab.page.mouse.down();
await tab.page.mouse.up();
Expand All @@ -145,15 +140,13 @@ const mouseDrag = defineTabTool({
},

handle: async (tab, params, response) => {
response.setIncludeSnapshot();

response.addCode(`// Drag mouse from (${params.startX}, ${params.startY}) to (${params.endX}, ${params.endY})`);
response.addCode(`await page.mouse.move(${params.startX}, ${params.startY});`);
response.addCode(`await page.mouse.down();`);
response.addCode(`await page.mouse.move(${params.endX}, ${params.endY});`);
response.addCode(`await page.mouse.up();`);

await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
await tab.page.mouse.move(params.startX, params.startY);
await tab.page.mouse.down();
await tab.page.mouse.move(params.endX, params.endY);
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/src/mcp/browser/tools/runCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const runCode = defineTabTool({
__end__,
};
vm.createContext(context);
await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
const snippet = `(async () => {
try {
const result = await (${params.code})(page);
Expand Down
16 changes: 4 additions & 12 deletions packages/playwright/src/mcp/browser/tools/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ const click = defineTabTool({
},

handle: async (tab, params, response) => {
response.setIncludeSnapshot();

const { locator, resolved } = await tab.refLocator(params);
const options = {
button: params.button,
Expand All @@ -74,7 +72,7 @@ const click = defineTabTool({
else
response.addCode(`await page.${resolved}.click(${optionsAttr});`);

await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
if (params.doubleClick)
await locator.dblclick(options);
else
Expand All @@ -99,14 +97,12 @@ const drag = defineTabTool({
},

handle: async (tab, params, response) => {
response.setIncludeSnapshot();

const [start, end] = await tab.refLocators([
{ ref: params.startRef, element: params.startElement },
{ ref: params.endRef, element: params.endElement },
]);

await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
await start.locator.dragTo(end.locator);
});

Expand All @@ -125,12 +121,10 @@ const hover = defineTabTool({
},

handle: async (tab, params, response) => {
response.setIncludeSnapshot();

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

await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
await locator.hover();
});
},
Expand All @@ -151,12 +145,10 @@ const selectOption = defineTabTool({
},

handle: async (tab, params, response) => {
response.setIncludeSnapshot();

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

await tab.waitForCompletion(async () => {
await tab.waitForCompletion(response, async () => {
await locator.selectOption(params.values);
});
},
Expand Down
14 changes: 8 additions & 6 deletions packages/playwright/src/mcp/browser/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type * as playwright from 'playwright-core';
import type { Tab } from '../tab';

export async function waitForCompletion<R>(tab: Tab, callback: () => Promise<R>): Promise<R> {
export async function waitForCompletion<R>(tab: Tab, callback: () => Promise<R>): Promise<{ result: R, trivial: boolean }> {
const requests: playwright.Request[] = [];

const requestListener = (request: playwright.Request) => requests.push(request);
Expand All @@ -37,7 +37,7 @@ export async function waitForCompletion<R>(tab: Tab, callback: () => Promise<R>)
const requestedNavigation = requests.some(request => request.isNavigationRequest());
if (requestedNavigation) {
await tab.page.mainFrame().waitForLoadState('load', { timeout: 10000 }).catch(() => {});
return result;
return { result, trivial: false };
}

const promises: Promise<any>[] = [];
Expand All @@ -47,12 +47,14 @@ export async function waitForCompletion<R>(tab: Tab, callback: () => Promise<R>)
else
promises.push(request.response().catch(() => {}));
}
const timeout = new Promise<void>(resolve => setTimeout(resolve, 5000));
await Promise.race([Promise.all(promises), timeout]);
if (requests.length)

if (requests.length) {
const timeout = new Promise<void>(resolve => setTimeout(resolve, 5000));
await Promise.race([Promise.all(promises), timeout]);
await tab.waitForTimeout(500);
}

return result;
return { result, trivial: true };
}

export async function callOnPageNoTrace<T>(page: playwright.Page, callback: (page: playwright.Page) => Promise<T>): Promise<T> {
Expand Down
Loading