Skip to content

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

chore: include intent in perform actions (#38532)

Pavel Feldmanb2508120be8b
diff --git a/package-lock.json b/package-lock.json
index 80c770a7e3..09707cfd57 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -18,7 +18,7 @@
"@eslint/compat": "^1.3.2",
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.34.0",
- "@lowire/loop": "^0.0.7",
+ "@lowire/loop": "^0.0.8",
"@modelcontextprotocol/sdk": "^1.17.5",
"@octokit/graphql-schema": "^15.26.0",
"@stylistic/eslint-plugin": "^5.2.3",
@@ -1064,9 +1064,9 @@
}
},
"node_modules/@lowire/loop": {
- "version": "0.0.7",
- "resolved": "https://registry.npmjs.org/@lowire/loop/-/loop-0.0.7.tgz",
- "integrity": "sha512-Hn/VU1v+Ld8m9y3AkFg3Naeht79Ub9dtefljGy5WEMZbrCJkONw4Q2GqihCWys8tcUQZUf9pUTyTIvC+A6HEQw==",
+ "version": "0.0.8",
+ "resolved": "https://registry.npmjs.org/@lowire/loop/-/loop-0.0.8.tgz",
+ "integrity": "sha512-DuyxRnPnu8Bwsfwt77tbZAgWGYKhEN/0xpsvHNin/62OEBiVx/nvSbj9NMFmeP83NoqCTG5n7JZCkA5GIw1pCA==",
"dev": true,
"license": "Apache-2.0",
"engines": {
diff --git a/package.json b/package.json
index 334ad20a59..d95607cc2a 100644
--- a/package.json
+++ b/package.json
@@ -56,7 +56,7 @@
"@eslint/compat": "^1.3.2",
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.34.0",
- "@lowire/loop": "^0.0.7",
+ "@lowire/loop": "^0.0.8",
"@modelcontextprotocol/sdk": "^1.17.5",
"@octokit/graphql-schema": "^15.26.0",
"@stylistic/eslint-plugin": "^5.2.3",
diff --git a/packages/playwright-core/ThirdPartyNotices.txt b/packages/playwright-core/ThirdPartyNotices.txt
index 4a0f753351..c6d3cb6724 100644
--- a/packages/playwright-core/ThirdPartyNotices.txt
+++ b/packages/playwright-core/ThirdPartyNotices.txt
@@ -4,7 +4,7 @@ THIRD-PARTY SOFTWARE NOTICES AND INFORMATION
This project incorporates components from the projects listed below. The original copyright notices and the licenses under which Microsoft received such components are set forth below. Microsoft reserves all rights not expressly granted herein, whether by implication, estoppel or otherwise.
-- @lowire/[email protected] (https://github.com/pavelfeldman/lowire)
+- @lowire/[email protected] (https://github.com/pavelfeldman/lowire)
- @modelcontextprotocol/[email protected] (https://github.com/modelcontextprotocol/typescript-sdk)
- [email protected] (https://github.com/jshttp/accepts)
- [email protected] (https://github.com/TooTallNate/proxy-agents)
@@ -135,7 +135,7 @@ This project incorporates components from the projects listed below. The origina
- [email protected] (https://github.com/StefanTerdell/zod-to-json-schema)
- [email protected] (https://github.com/colinhacks/zod)
-%% @lowire/[email protected] NOTICES AND INFORMATION BEGIN HERE
+%% @lowire/[email protected] NOTICES AND INFORMATION BEGIN HERE
=========================================
Apache License
Version 2.0, January 2004
@@ -339,7 +339,7 @@ Apache License
See the License for the specific language governing permissions and
limitations under the License.
=========================================
-END OF @lowire/[email protected] AND INFORMATION
+END OF @lowire/[email protected] AND INFORMATION
%% @modelcontextprotocol/[email protected] NOTICES AND INFORMATION BEGIN HERE
=========================================
diff --git a/packages/playwright-core/bundles/mcp/package-lock.json b/packages/playwright-core/bundles/mcp/package-lock.json
index bf5ee6bf3a..0e18bcbefe 100644
--- a/packages/playwright-core/bundles/mcp/package-lock.json
+++ b/packages/playwright-core/bundles/mcp/package-lock.json
@@ -8,16 +8,16 @@
"name": "mcp-bundle",
"version": "0.0.1",
"dependencies": {
- "@lowire/loop": "^0.0.7",
+ "@lowire/loop": "^0.0.8",
"@modelcontextprotocol/sdk": "^1.24.0",
"zod": "^3.25.76",
"zod-to-json-schema": "^3.24.6"
}
},
"node_modules/@lowire/loop": {
- "version": "0.0.7",
- "resolved": "https://registry.npmjs.org/@lowire/loop/-/loop-0.0.7.tgz",
- "integrity": "sha512-Hn/VU1v+Ld8m9y3AkFg3Naeht79Ub9dtefljGy5WEMZbrCJkONw4Q2GqihCWys8tcUQZUf9pUTyTIvC+A6HEQw==",
+ "version": "0.0.8",
+ "resolved": "https://registry.npmjs.org/@lowire/loop/-/loop-0.0.8.tgz",
+ "integrity": "sha512-DuyxRnPnu8Bwsfwt77tbZAgWGYKhEN/0xpsvHNin/62OEBiVx/nvSbj9NMFmeP83NoqCTG5n7JZCkA5GIw1pCA==",
"license": "Apache-2.0",
"engines": {
"node": ">=20"
diff --git a/packages/playwright-core/bundles/mcp/package.json b/packages/playwright-core/bundles/mcp/package.json
index eca0006903..7e6f08a8e8 100644
--- a/packages/playwright-core/bundles/mcp/package.json
+++ b/packages/playwright-core/bundles/mcp/package.json
@@ -4,7 +4,7 @@
"private": true,
"dependencies": {
"@modelcontextprotocol/sdk": "^1.24.0",
- "@lowire/loop": "^0.0.7",
+ "@lowire/loop": "^0.0.8",
"zod": "^3.25.76",
"zod-to-json-schema": "^3.24.6"
}
diff --git a/packages/playwright-core/src/server/agent/actions.ts b/packages/playwright-core/src/server/agent/actions.ts
index 2e0fbc5507..2c31cb0e62 100644
--- a/packages/playwright-core/src/server/agent/actions.ts
+++ b/packages/playwright-core/src/server/agent/actions.ts
@@ -80,4 +80,5 @@ export type ExpectValue = {
export type Action = ClickAction | DragAction | HoverAction | SelectOptionAction | PressAction | PressSequentiallyAction | FillAction | SetChecked | ExpectVisible | ExpectValue;
export type ActionWithCode = Action & {
code: string;
+ intent?: string;
};
diff --git a/packages/playwright-core/src/server/agent/agent.ts b/packages/playwright-core/src/server/agent/agent.ts
index d952e814e3..05ca068fd2 100644
--- a/packages/playwright-core/src/server/agent/agent.ts
+++ b/packages/playwright-core/src/server/agent/agent.ts
@@ -96,7 +96,7 @@ async function cachedPerform(context: Context, options: channels.PagePerformPara
return false;
const cache = await cachedActions(context.options.cacheFile);
- const cacheKey = options.key ?? options.task;
+ const cacheKey = (options.key ?? options.task).trim();
const entry = cache[cacheKey];
if (!entry) {
if (context.options.cacheMode === 'force')
@@ -114,7 +114,7 @@ async function updateCache(context: Context, options: channels.PagePerformParams
if (!cacheFile)
return;
const cache = await cachedActions(cacheFile);
- const cacheKey = options.key ?? options.task;
+ const cacheKey = (options.key ?? options.task).trim();
cache[cacheKey] = {
timestamp: Date.now(),
actions: context.actions,
diff --git a/packages/playwright-core/src/server/agent/backend.ts b/packages/playwright-core/src/server/agent/backend.ts
index 66428b0c70..bccd7035e4 100644
--- a/packages/playwright-core/src/server/agent/backend.ts
+++ b/packages/playwright-core/src/server/agent/backend.ts
@@ -15,7 +15,6 @@
*/
import toolDefinitions from './tools';
-import { ProgressController } from '../progress';
import { zodToJsonSchema } from '../../mcpBundle';
import type * as loopTypes from '@lowire/loop';
@@ -32,6 +31,7 @@ export function toolsForLoop(context: Context): { tools: loopTypes.Tool[], callT
});
const callTool: loopTypes.ToolCallback = async params => {
+ const intent = params.arguments._meta?.['dev.lowire/intent'];
const tool = toolDefinitions.find(t => t.schema.name === params.name);
if (!tool) {
return {
@@ -42,11 +42,8 @@ export function toolsForLoop(context: Context): { tools: loopTypes.Tool[], callT
};
}
- const progressController = new ProgressController();
try {
- return await progressController.run(async progress => {
- return await tool.handle(context, params.arguments);
- });
+ return await context.callTool(tool, params.arguments, { intent });
} catch (error) {
return {
content: [{ type: 'text', text: error.message }],
diff --git a/packages/playwright-core/src/server/agent/context.ts b/packages/playwright-core/src/server/agent/context.ts
index 2721dcaff1..42475eaae5 100644
--- a/packages/playwright-core/src/server/agent/context.ts
+++ b/packages/playwright-core/src/server/agent/context.ts
@@ -25,6 +25,7 @@ import type { Page } from '../page';
import type { Progress } from '../progress';
import type { BrowserContextOptions } from '../types';
import type { Language } from '../../utils/isomorphic/locatorGenerators.ts';
+import type { ToolDefinition } from './tools';
type AgentOptions = BrowserContextOptions['agent'];
@@ -34,6 +35,7 @@ export class Context {
readonly page: Page;
readonly actions: actions.ActionWithCode[] = [];
readonly sdkLanguage: Language;
+ private _callIntent: string | undefined;
constructor(progress: Progress, page: Page) {
this.progress = progress;
@@ -42,6 +44,15 @@ export class Context {
this.sdkLanguage = page.browserContext._browser.sdkLanguage();
}
+ async callTool(tool: ToolDefinition, params: any, options: { intent?: string }) {
+ this._callIntent = options.intent;
+ try {
+ return await tool.handle(this, params);
+ } finally {
+ this._callIntent = undefined;
+ }
+ }
+
async runActionAndWait(action: actions.Action) {
return await this.runActionsAndWait([action]);
}
@@ -52,7 +63,7 @@ export class Context {
for (const a of action) {
await runAction(this.progress, this.page, a, this.options?.secrets ?? []);
const code = await generateCode(this.sdkLanguage, a);
- this.actions.push({ ...a, code });
+ this.actions.push({ ...a, code, intent: this._callIntent });
}
});
return await this.snapshotResult();
diff --git a/packages/playwright-core/src/server/agent/tools.ts b/packages/playwright-core/src/server/agent/tools.ts
index 2b2ef07c15..4b85eda6fb 100644
--- a/packages/playwright-core/src/server/agent/tools.ts
+++ b/packages/playwright-core/src/server/agent/tools.ts
@@ -27,7 +27,7 @@ type ToolSchema<Input extends zod.Schema> = Omit<loopTypes.Tool, 'inputSchema'>
inputSchema: Input;
};
-type ToolDefinition<Input extends zod.Schema = zod.Schema> = {
+export type ToolDefinition<Input extends zod.Schema = zod.Schema> = {
schema: ToolSchema<Input>;
handle: (context: Context, params: zod.output<Input>) => Promise<loopTypes.ToolResult>;
};
@@ -327,4 +327,4 @@ export default [
expectVisible,
expectVisibleText,
expectValue,
-];
+] as ToolDefinition<any>[];
diff --git a/tests/library/perform-task.spec.ts-cache.json b/tests/library/perform-task.spec.ts-cache.json
index 29fe8fe9f5..528c77b738 100644
--- a/tests/library/perform-task.spec.ts-cache.json
+++ b/tests/library/perform-task.spec.ts-cache.json
@@ -1,76 +1,86 @@
{
+ "Enter x-secret-email into the email field": {
+ "timestamp": 1765482830501,
+ "actions": [
+ {
+ "method": "fill",
+ "selector": "internal:role=textbox[name=\"Email Address\"i]",
+ "text": "x-secret-email",
+ "code": "await page.getByRole('textbox', { name: 'Email Address' }).fill('x-secret-email');",
+ "intent": "Enter 'x-secret-email' into the email address field"
+ }
+ ]
+ },
"Fill out the form with the following details:\nName: John Smith\nAddress: 1045 La Avenida St, Mountain View, CA 94043\nEmail: [email protected]": {
- "timestamp": 1765479733829,
+ "timestamp": 1765482831906,
"actions": [
{
"method": "fill",
"selector": "internal:role=textbox[name=\"Full Name *\"i]",
"text": "John Smith",
- "code": "await page.getByRole('textbox', { name: 'Full Name *' }).fill('John Smith');"
+ "code": "await page.getByRole('textbox', { name: 'Full Name *' }).fill('John Smith');",
+ "intent": "Fill out the form with the provided details: Name (John Smith), Address (1045 La Avenida St, Mountain View, CA 94043), and Email ([email protected])"
},
{
"method": "fill",
"selector": "internal:role=textbox[name=\"Email Address *\"i]",
"text": "[email protected]",
- "code": "await page.getByRole('textbox', { name: 'Email Address *' }).fill('[email protected]');"
+ "code": "await page.getByRole('textbox', { name: 'Email Address *' }).fill('[email protected]');",
+ "intent": "Fill out the form with the provided details: Name (John Smith), Address (1045 La Avenida St, Mountain View, CA 94043), and Email ([email protected])"
},
{
"method": "fill",
"selector": "internal:role=textbox[name=\"Street Address *\"i]",
"text": "1045 La Avenida St",
- "code": "await page.getByRole('textbox', { name: 'Street Address *' }).fill('1045 La Avenida St');"
+ "code": "await page.getByRole('textbox', { name: 'Street Address *' }).fill('1045 La Avenida St');",
+ "intent": "Fill out the form with the provided details: Name (John Smith), Address (1045 La Avenida St, Mountain View, CA 94043), and Email ([email protected])"
},
{
"method": "fill",
"selector": "internal:role=textbox[name=\"City *\"i]",
"text": "Mountain View",
- "code": "await page.getByRole('textbox', { name: 'City *' }).fill('Mountain View');"
+ "code": "await page.getByRole('textbox', { name: 'City *' }).fill('Mountain View');",
+ "intent": "Fill out the form with the provided details: Name (John Smith), Address (1045 La Avenida St, Mountain View, CA 94043), and Email ([email protected])"
},
{
"method": "fill",
"selector": "internal:role=textbox[name=\"State/Province *\"i]",
"text": "CA",
- "code": "await page.getByRole('textbox', { name: 'State/Province *' }).fill('CA');"
+ "code": "await page.getByRole('textbox', { name: 'State/Province *' }).fill('CA');",
+ "intent": "Fill out the form with the provided details: Name (John Smith), Address (1045 La Avenida St, Mountain View, CA 94043), and Email ([email protected])"
},
{
"method": "fill",
"selector": "internal:role=textbox[name=\"ZIP/Postal Code *\"i]",
"text": "94043",
- "code": "await page.getByRole('textbox', { name: 'ZIP/Postal Code *' }).fill('94043');"
- }
- ]
- },
- "Enter x-secret-email into the email field": {
- "timestamp": 1765479741895,
- "actions": [
- {
- "method": "fill",
- "selector": "internal:role=textbox[name=\"Email Address\"i]",
- "text": "x-secret-email",
- "code": "await page.getByRole('textbox', { name: 'Email Address' }).fill('x-secret-email');"
+ "code": "await page.getByRole('textbox', { name: 'ZIP/Postal Code *' }).fill('94043');",
+ "intent": "Fill out the form with the provided details: Name (John Smith), Address (1045 La Avenida St, Mountain View, CA 94043), and Email ([email protected])"
}
]
},
- "\n - Enter \"bogus\" into the email field\n - Check that the value is in fact \"bogus\"\n - Check that the error message is displayed\n ": {
- "timestamp": 1765479938668,
+ "- Enter \"bogus\" into the email field\n - Check that the value is in fact \"bogus\"\n - Check that the error message is displayed": {
+ "timestamp": 1765482849524,
"actions": [
{
"method": "fill",
"selector": "internal:role=textbox[name=\"Email Address\"i]",
"text": "bogus",
- "code": "await page.getByRole('textbox', { name: 'Email Address' }).fill('bogus');"
+ "code": "await page.getByRole('textbox', { name: 'Email Address' }).fill('bogus');",
+ "intent": "Enter \"bogus\" into the email field"
},
{
"method": "expectValue",
"selector": "internal:role=textbox[name=\"Email Address\"i]",
"type": "textbox",
"value": "bogus",
- "code": "await expect(page.getByRole('textbox', { name: 'Email Address' })).toHaveValue('bogus');"
+ "code": "await expect(page.getByRole('textbox', { name: 'Email Address' })).toHaveValue('bogus');",
+ "intent": "Check that the email field value is \"bogus\""
},
{
"method": "expectVisible",
"selector": "internal:text=\"Error: Invalid email address\"i",
- "code": "await expect(page.getByText('Error: Invalid email address')).toBeVisible();"
+ "code": "await expect(page.getByText('Error: Invalid email address')).toBeVisible();",
+ "intent": "Check that the error message \"Error: Invalid email address\" is displayed"
}
]
}