Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: add perform secrets, form tool (#38467)
Pavel Feldman06e68bbd023a
diff --git a/docs/src/api/params.md b/docs/src/api/params.mdindex 8eb29d4df8..9220e12713 100644--- a/docs/src/api/params.md+++ b/docs/src/api/params.md@@ -373,10 +373,11 @@ Emulates consistent window screen size available inside web page via `window.scr## js-context-option-agent* langs: js- `agent` <[Object]>- - `provider` <[string]> LLM provider to use- - `model` <[string]> Model identifier within provider+ - `provider` <[string]> LLM provider to use.+ - `model` <[string]> Model identifier within provider.- `cacheFile` ?<[string]> Cache file to use/generate code for performed actions into. Cache is not used if not specified (default).- - `cacheMode` ?<['force'|'ignore'|'auto']> Cache control, defauls to 'auto'+ - `cacheMode` ?<['force'|'ignore'|'auto']> Cache control, defaults to 'auto'.+ - `secrets` ?<[Object]<[string], [string]>> Secrets to hide from the LLM.Agent settings for [`method: Page.perform`] and [`method: Page.extract`].diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.tsindex cd99f7dd5b..77196a4080 100644--- a/packages/playwright-client/types/types.d.ts+++ b/packages/playwright-client/types/types.d.ts@@ -22087,12 +22087,12 @@ export interface BrowserContextOptions {*/agent?: {/**- * LLM provider to use+ * LLM provider to use.*/provider: string;/**- * Model identifier within provider+ * Model identifier within provider.*/model: string;@@ -22102,9 +22102,14 @@ export interface BrowserContextOptions {cacheFile?: string;/**- * Cache control, defauls to 'auto'+ * Cache control, defaults to 'auto'.*/cacheMode?: 'force'|'ignore'|'auto';++ /**+ * Secrets to hide from the LLM.+ */+ secrets?: { [key: string]: string; };};/**diff --git a/packages/playwright-core/src/client/browserContext.ts b/packages/playwright-core/src/client/browserContext.tsindex 05178d2e71..83f83c8114 100644--- a/packages/playwright-core/src/client/browserContext.ts+++ b/packages/playwright-core/src/client/browserContext.ts@@ -558,6 +558,7 @@ export async function prepareBrowserContextParams(platform: Platform, options: Bnetwork.validateHeaders(options.extraHTTPHeaders);const contextParams: channels.BrowserNewContextParams = {...options,+ agent: toAgentProtocol(options.agent),viewport: options.viewport === null ? undefined : options.viewport,noDefaultViewport: options.viewport === null,extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined,@@ -581,6 +582,13 @@ export async function prepareBrowserContextParams(platform: Platform, options: Breturn contextParams;}+function toAgentProtocol(agent?: BrowserContextOptions['agent']): channels.BrowserNewContextParams['agent'] {+ if (!agent)+ return undefined;+ const secrets = agent.secrets ? Object.entries(agent.secrets).map(([name, value]) => ({ name, value })) : undefined;+ return { ...agent, secrets };+}+function toAcceptDownloadsProtocol(acceptDownloads?: boolean) {if (acceptDownloads === undefined)return undefined;diff --git a/packages/playwright-core/src/client/types.ts b/packages/playwright-core/src/client/types.tsindex 48a2d71cf8..265a1e8e59 100644--- a/packages/playwright-core/src/client/types.ts+++ b/packages/playwright-core/src/client/types.ts@@ -58,7 +58,10 @@ export type ClientCertificate = {passphrase?: string;};-export type BrowserContextOptions = Omit<channels.BrowserNewContextOptions, 'viewport' | 'noDefaultViewport' | 'extraHTTPHeaders' | 'clientCertificates' | 'storageState' | 'recordHar' | 'colorScheme' | 'reducedMotion' | 'forcedColors' | 'acceptDownloads' | 'contrast'> & {+export type AgentOptions = Omit<Exclude<channels.BrowserNewContextOptions['agent'], undefined>, 'secrets'> & { secrets?: Record<string, string> };++export type BrowserContextOptions = Omit<channels.BrowserNewContextOptions, 'viewport' | 'noDefaultViewport' | 'extraHTTPHeaders' | 'clientCertificates' | 'storageState' | 'recordHar' | 'colorScheme' | 'reducedMotion' | 'forcedColors' | 'acceptDownloads' | 'contrast' | 'agent'> & {+ agent?: AgentOptions;viewport?: Size | null;extraHTTPHeaders?: Headers;logger?: Logger;diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.tsindex 0cf1d9f62b..d10e8f420e 100644--- a/packages/playwright-core/src/protocol/validator.ts+++ b/packages/playwright-core/src/protocol/validator.ts@@ -607,6 +607,7 @@ scheme.BrowserTypeLaunchPersistentContextParams = tObject({model: tString,cacheFile: tOptional(tString),cacheMode: tOptional(tEnum(['ignore', 'force', 'auto'])),+ secrets: tOptional(tArray(tType('NameValue'))),})),userDataDir: tString,slowMo: tOptional(tFloat),@@ -705,6 +706,7 @@ scheme.BrowserNewContextParams = tObject({model: tString,cacheFile: tOptional(tString),cacheMode: tOptional(tEnum(['ignore', 'force', 'auto'])),+ secrets: tOptional(tArray(tType('NameValue'))),})),proxy: tOptional(tObject({server: tString,@@ -782,6 +784,7 @@ scheme.BrowserNewContextForReuseParams = tObject({model: tString,cacheFile: tOptional(tString),cacheMode: tOptional(tEnum(['ignore', 'force', 'auto'])),+ secrets: tOptional(tArray(tType('NameValue'))),})),proxy: tOptional(tObject({server: tString,@@ -904,6 +907,7 @@ scheme.BrowserContextInitializer = tObject({model: tString,cacheFile: tOptional(tString),cacheMode: tOptional(tEnum(['ignore', 'force', 'auto'])),+ secrets: tOptional(tArray(tType('NameValue'))),})),}),});@@ -2813,6 +2817,7 @@ scheme.AndroidDeviceLaunchBrowserParams = tObject({model: tString,cacheFile: tOptional(tString),cacheMode: tOptional(tEnum(['ignore', 'force', 'auto'])),+ secrets: tOptional(tArray(tType('NameValue'))),})),pkg: tOptional(tString),args: tOptional(tArray(tString)),diff --git a/packages/playwright-core/src/server/agent/actionRunner.ts b/packages/playwright-core/src/server/agent/actionRunner.tsindex fdb9f81d23..c1ee4d3227 100644--- a/packages/playwright-core/src/server/agent/actionRunner.ts+++ b/packages/playwright-core/src/server/agent/actionRunner.ts@@ -17,8 +17,9 @@import type * as actions from './actions';import type { Page } from '../page';import type { Progress } from '../progress';+import type { NameValue } from '@protocol/channels';-export async function runAction(progress: Progress, page: Page, action: actions.Action) {+export async function runAction(progress: Progress, page: Page, action: actions.Action, secrets: NameValue[]) {const frame = page.mainFrame();switch (action.method) {case 'click':@@ -31,21 +32,31 @@ export async function runAction(progress: Progress, page: Page, action: actions.await frame.hover(progress, action.selector, { ...action.options, ...strictTrue });break;case 'selectOption':- await frame.selectOption(progress, action.selector, [], action.values.map(a => ({ value: a })), { ...strictTrue });+ await frame.selectOption(progress, action.selector, [], action.labels.map(a => ({ label: a })), { ...strictTrue });break;case 'pressKey':await page.keyboard.press(progress, action.key);break;- case 'pressSequentially':- await frame.type(progress, action.selector, action.text, { ...strictTrue });+ case 'pressSequentially': {+ const secret = secrets?.find(s => s.name === action.text)?.value ?? action.text;+ await frame.type(progress, action.selector, secret, { ...strictTrue });if (action.submit)await page.keyboard.press(progress, 'Enter');break;- case 'fill':- await frame.fill(progress, action.selector, action.text, { ...strictTrue });+ }+ case 'fill': {+ const secret = secrets?.find(s => s.name === action.text)?.value ?? action.text;+ await frame.fill(progress, action.selector, secret, { ...strictTrue });if (action.submit)await page.keyboard.press(progress, 'Enter');break;+ }+ case 'setChecked':+ if (action.checked)+ await frame.check(progress, action.selector, { ...strictTrue });+ else+ await frame.uncheck(progress, action.selector, { ...strictTrue });+ break;}}diff --git a/packages/playwright-core/src/server/agent/actions.ts b/packages/playwright-core/src/server/agent/actions.tsindex 030801c0a0..1870dfbfe6 100644--- a/packages/playwright-core/src/server/agent/actions.ts+++ b/packages/playwright-core/src/server/agent/actions.ts@@ -37,7 +37,7 @@ export type HoverAction = {export type SelectOptionAction = {method: 'selectOption';selector: string;- values: string[];+ labels: string[];};export type PressAction = {@@ -59,4 +59,10 @@ export type FillAction = {submit?: boolean;};-export type Action = ClickAction | DragAction | HoverAction | SelectOptionAction | PressAction | PressSequentiallyAction | FillAction;+export type SetChecked = {+ method: 'setChecked';+ selector: string;+ checked: boolean;+};++export type Action = ClickAction | DragAction | HoverAction | SelectOptionAction | PressAction | PressSequentiallyAction | FillAction | SetChecked;diff --git a/packages/playwright-core/src/server/agent/agent.ts b/packages/playwright-core/src/server/agent/agent.tsindex fe8e2ff11b..4ab64460f8 100644--- a/packages/playwright-core/src/server/agent/agent.ts+++ b/packages/playwright-core/src/server/agent/agent.ts@@ -85,26 +85,25 @@ type CachedActions = Record<string, actions.Action[]>;const allCaches = new Map<string, CachedActions>();async function cachedPerform(context: Context, options: channels.PagePerformParams): Promise<boolean> {- const agentSettings = context.page.browserContext._options.agent;- if (!agentSettings?.cacheFile || agentSettings.cacheMode === 'ignore')+ if (!context.options?.cacheFile || context.options.cacheMode === 'ignore')return false;- const cache = await cachedActions(agentSettings.cacheFile);+ const cache = await cachedActions(context.options.cacheFile);const cacheKey = options.key ?? options.task;const actions = cache[cacheKey];if (!actions) {- if (agentSettings.cacheMode === 'force')+ if (context.options.cacheMode === 'force')throw new Error(`No cached actions for key "${cacheKey}", but cache mode is set to "force"`);return false;}for (const action of actions)- await runAction(context.progress, context.page, action);+ await runAction(context.progress, context.page, action, context.options.secrets ?? []);return true;}async function updateCache(context: Context, options: channels.PagePerformParams) {- const cacheFile = context.page.browserContext._options.agent?.cacheFile;+ const cacheFile = context.options?.cacheFile;if (!cacheFile)return;const cache = await cachedActions(cacheFile);diff --git a/packages/playwright-core/src/server/agent/context.ts b/packages/playwright-core/src/server/agent/context.tsindex a168cde5ed..0a96a46c72 100644--- a/packages/playwright-core/src/server/agent/context.ts+++ b/packages/playwright-core/src/server/agent/context.ts@@ -22,8 +22,12 @@ 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';++type AgentOptions = BrowserContextOptions['agent'];export class Context {+ readonly options: AgentOptions;readonly progress: Progress;readonly page: Page;readonly actions: actions.Action[] = [];@@ -31,11 +35,20 @@ export class Context {constructor(progress: Progress, page: Page) {this.progress = progress;this.page = page;+ this.options = page.browserContext._options.agent;+ }++ async runActionAndWait(action: actions.Action) {+ return await this.runActionsAndWait([action]);}- async runAction(action: actions.Action) {- await this.waitForCompletion(() => runAction(this.progress, this.page, action));- this.actions.push(action);+ async runActionsAndWait(action: actions.Action[]) {+ await this.waitForCompletion(async () => {+ for (const a of action) {+ await runAction(this.progress, this.page, a, this.options?.secrets ?? []);+ this.actions.push(a);+ }+ });return await this.snapshotResult();}@@ -71,7 +84,9 @@ export class Context {}async snapshotResult(): Promise<loopTypes.ToolResult> {- const { full } = await this.page.snapshotForAI(this.progress);+ let { full } = await this.page.snapshotForAI(this.progress);+ full = this._redactText(full);+const text = [`# Page snapshot\n${full}`];return {@@ -94,4 +109,18 @@ export class Context {}}));}++ private _redactText(text: string): string {+ const secrets = this.options?.secrets;+ if (!secrets)+ return text;++ const redactText = (text: string) => {+ for (const { name, value } of secrets)+ text = text.replaceAll(value, `<secret>${name}</secret>`);+ return text;+ };++ return redactText(text);+ }}diff --git a/packages/playwright-core/src/server/agent/tools.ts b/packages/playwright-core/src/server/agent/tools.tsindex b7db87a1d7..33125bc33b 100644--- a/packages/playwright-core/src/server/agent/tools.ts+++ b/packages/playwright-core/src/server/agent/tools.ts@@ -18,6 +18,7 @@ import { z } from '../../mcpBundle';import type zod from 'zod';import type * as loopTypes from '@lowire/loop';+import type * as actions from './actions';import type { Context } from './context';type ToolSchema<Input extends zod.Schema> = Omit<loopTypes.Tool, 'inputSchema'> & {@@ -68,7 +69,7 @@ const click = defineTool({handle: async (context, params) => {const [selector] = await context.refSelectors([params]);- return await context.runAction({+ return await context.runActionAndWait({method: 'click',selector,options: {@@ -99,7 +100,7 @@ const drag = defineTool({{ ref: params.endRef, element: params.endElement },]);- return await context.runAction({+ return await context.runActionAndWait({method: 'drag',sourceSelector,targetSelector@@ -121,7 +122,7 @@ const hover = defineTool({handle: async (context, params) => {const [selector] = await context.refSelectors([params]);- return await context.runAction({+ return await context.runActionAndWait({method: 'hover',selector,options: {@@ -145,10 +146,10 @@ const selectOption = defineTool({handle: async (context, params) => {const [selector] = await context.refSelectors([params]);- return await context.runAction({+ return await context.runActionAndWait({method: 'selectOption',selector,- values: params.values+ labels: params.values});},});@@ -164,7 +165,7 @@ const pressKey = defineTool({},handle: async (context, params) => {- return await context.runAction({+ return await context.runActionAndWait({method: 'pressKey',key: params.key});@@ -188,14 +189,14 @@ const type = defineTool({handle: async (context, params) => {const [selector] = await context.refSelectors([params]);if (params.slowly) {- return await context.runAction({+ return await context.runActionAndWait({method: 'pressSequentially',selector,text: params.text,submit: params.submit,});} else {- return await context.runAction({+ return await context.runActionAndWait({method: 'fill',selector,text: params.text,@@ -205,6 +206,49 @@ const type = defineTool({},});+const fillForm = defineTool({+ schema: {+ name: 'browser_fill_form',+ title: 'Fill form',+ description: 'Fill multiple form fields',+ inputSchema: z.object({+ fields: z.array(z.object({+ name: z.string().describe('Human-readable field name'),+ type: z.enum(['textbox', 'checkbox', 'radio', 'combobox', 'slider']).describe('Type of the field'),+ ref: z.string().describe('Exact target field reference from the page snapshot'),+ value: z.string().describe('Value to fill in the field. If the field is a checkbox, the value should be `true` or `false`. If the field is a combobox, the value should be the text of the option.'),+ })).describe('Fields to fill in'),+ }),+ },++ handle: async (context, params) => {+ const actions: actions.Action[] = [];+ for (const field of params.fields) {+ const [selector] = await context.refSelectors([{ ref: field.ref, element: field.name }]);+ if (field.type === 'textbox' || field.type === 'slider') {+ actions.push({+ method: 'fill',+ selector,+ text: field.value,+ });+ } else if (field.type === 'checkbox' || field.type === 'radio') {+ actions.push({+ method: 'setChecked',+ selector,+ checked: field.value === 'true',+ });+ } else if (field.type === 'combobox') {+ actions.push({+ method: 'selectOption',+ selector,+ labels: [field.value],+ });+ }+ }+ return await context.runActionsAndWait(actions);+ },+});+export default [snapshot,click,@@ -213,4 +257,5 @@ export default [selectOption,pressKey,type,+ fillForm,];diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.tsindex cd99f7dd5b..77196a4080 100644--- a/packages/playwright-core/types/types.d.ts+++ b/packages/playwright-core/types/types.d.ts@@ -22087,12 +22087,12 @@ export interface BrowserContextOptions {*/agent?: {/**- * LLM provider to use+ * LLM provider to use.*/provider: string;/**- * Model identifier within provider+ * Model identifier within provider.*/model: string;@@ -22102,9 +22102,14 @@ export interface BrowserContextOptions {cacheFile?: string;/**- * Cache control, defauls to 'auto'+ * Cache control, defaults to 'auto'.*/cacheMode?: 'force'|'ignore'|'auto';++ /**+ * Secrets to hide from the LLM.+ */+ secrets?: { [key: string]: string; };};/**diff --git a/packages/protocol/src/channels.d.ts b/packages/protocol/src/channels.d.tsindex e7244adb09..1c74085748 100644--- a/packages/protocol/src/channels.d.ts+++ b/packages/protocol/src/channels.d.ts@@ -1013,6 +1013,7 @@ export type BrowserTypeLaunchPersistentContextParams = {model: string,cacheFile?: string,cacheMode?: 'ignore' | 'force' | 'auto',+ secrets?: NameValue[],},userDataDir: string,slowMo?: number,@@ -1101,6 +1102,7 @@ export type BrowserTypeLaunchPersistentContextOptions = {model: string,cacheFile?: string,cacheMode?: 'ignore' | 'force' | 'auto',+ secrets?: NameValue[],},slowMo?: number,};@@ -1228,6 +1230,7 @@ export type BrowserNewContextParams = {model: string,cacheFile?: string,cacheMode?: 'ignore' | 'force' | 'auto',+ secrets?: NameValue[],},proxy?: {server: string,@@ -1302,6 +1305,7 @@ export type BrowserNewContextOptions = {model: string,cacheFile?: string,cacheMode?: 'ignore' | 'force' | 'auto',+ secrets?: NameValue[],},proxy?: {server: string,@@ -1379,6 +1383,7 @@ export type BrowserNewContextForReuseParams = {model: string,cacheFile?: string,cacheMode?: 'ignore' | 'force' | 'auto',+ secrets?: NameValue[],},proxy?: {server: string,@@ -1453,6 +1458,7 @@ export type BrowserNewContextForReuseOptions = {model: string,cacheFile?: string,cacheMode?: 'ignore' | 'force' | 'auto',+ secrets?: NameValue[],},proxy?: {server: string,@@ -1594,6 +1600,7 @@ export type BrowserContextInitializer = {model: string,cacheFile?: string,cacheMode?: 'ignore' | 'force' | 'auto',+ secrets?: NameValue[],},},};@@ -4906,6 +4913,7 @@ export type AndroidDeviceLaunchBrowserParams = {model: string,cacheFile?: string,cacheMode?: 'ignore' | 'force' | 'auto',+ secrets?: NameValue[],},pkg?: string,args?: string[],@@ -4978,6 +4986,7 @@ export type AndroidDeviceLaunchBrowserOptions = {model: string,cacheFile?: string,cacheMode?: 'ignore' | 'force' | 'auto',+ secrets?: NameValue[],},pkg?: string,args?: string[],diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.ymlindex f36d34abbf..07c3dfad14 100644--- a/packages/protocol/src/protocol.yml+++ b/packages/protocol/src/protocol.yml@@ -601,6 +601,9 @@ ContextOptions:- ignore- force- auto+ secrets:+ type: array?+ items: NameValueLocalUtils:type: interfacediff --git a/tests/page/agent-cache.json b/tests/library/agent-cache.jsonsimilarity index 53%rename from tests/page/agent-cache.jsonrename to tests/library/agent-cache.jsonindex b3ac1e1e08..3b7fde719e 100644--- a/tests/page/agent-cache.json+++ b/tests/library/agent-cache.json@@ -1,36 +1,4 @@{- "Fill out the form with the following details:\nName: John Doe\nAddress: 123 Main St, Anytown, XYZ state\nZip Code: 12345\nEmail: [email protected]": [- {- "method": "fill",- "selector": "internal:role=textbox[name=\"Full Name *\"i]",- "text": "John Doe"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"Email Address *\"i]",- "text": "[email protected]"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"Street Address *\"i]",- "text": "123 Main St"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"City *\"i]",- "text": "Anytown"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"State/Province *\"i]",- "text": "XYZ"- },- {- "method": "fill",- "selector": "internal:role=textbox[name=\"ZIP/Postal Code *\"i]",- "text": "12345"- }- ],"Fill out the form with the following details:\nName: John Smith\nAddress: 1045 La Avenida St, Mountain View, CA 94043\nEmail: [email protected]": [{"method": "fill",@@ -62,5 +30,12 @@"selector": "internal:role=textbox[name=\"ZIP/Postal Code *\"i]","text": "94043"}+ ],+ "Enter x-secret-email into the email field": [+ {+ "method": "fill",+ "selector": "internal:role=textbox[name=\"Email Address\"i]",+ "text": "x-secret-email"+ }]}\ No newline at end of filediff --git a/tests/page/perform-task.spec.ts b/tests/library/perform-task.spec.tssimilarity index 64%rename from tests/page/perform-task.spec.tsrename to tests/library/perform-task.spec.tsindex a28df7d411..87f8e82ad2 100644--- a/tests/page/perform-task.spec.ts+++ b/tests/library/perform-task.spec.ts@@ -14,9 +14,28 @@* limitations under the License.*/-import { test, expect } from './pageTest';+import path from 'path';import z from 'zod';+import { browserTest as test, expect } from '../config/browserTest';++test.use({+ contextOptions: async ({ contextOptions }, use, testInfo) => {+ await use({+ ...contextOptions,+ agent: {+ provider: 'github',+ model: 'claude-sonnet-4.5',+ cacheFile: path.join(testInfo.project.testDir, 'agent-cache.json'),+ cacheMode: process.env.CI ? 'force' : 'auto',+ secrets: {+ 'x-secret-email': '[email protected]',+ }+ },+ });+ }+});+test('page.perform', async ({ page, server }) => {await page.goto(server.PREFIX + '/evals/fill-form.html');await page.perform('Fill out the form with the following details:\n' +@@ -33,6 +52,14 @@ test('page.perform', async ({ page, server }) => {`);});+test('page.perform secret', async ({ page, server }) => {+ await page.setContent('<input type="email" name="email" placeholder="Email Address"/>');+ await page.perform('Enter x-secret-email into the email field');+ await expect(page.locator('body')).toMatchAriaSnapshot(`+ - textbox "Email Address": [email protected]+ `);+});+test.skip('extract task', async ({ page }) => {await page.goto('https://demo.playwright.dev/todomvc');await page.perform('Add "Buy groceries" todo');diff --git a/tests/library/playwright.config.ts b/tests/library/playwright.config.tsindex 1a958c93c5..5339d79701 100644--- a/tests/library/playwright.config.ts+++ b/tests/library/playwright.config.ts@@ -154,12 +154,6 @@ for (const browserName of browserNames) {testDir: path.join(testDir, 'page'),...projectTemplate,};- pageProject.use.agent = {- provider: 'github',- model: 'claude-sonnet-4.5',- cacheFile: path.join(testDir, 'page', 'agent-cache.json'),- cacheMode: process.env.CI ? 'force' : 'auto',- };config.projects.push(pageProject);}