Skip to content

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

chore: perform testing framework (#38785)

Pavel Feldmane43a6a4048c7
diff --git a/packages/playwright-core/src/client/page.ts b/packages/playwright-core/src/client/page.ts
index 4f4c7b0355..f464643d1f 100644
--- a/packages/playwright-core/src/client/page.ts
+++ b/packages/playwright-core/src/client/page.ts
@@ -852,6 +852,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
api: options.provider?.api,
apiEndpoint: options.provider?.apiEndpoint,
apiKey: options.provider?.apiKey,
+ apiCacheFile: (options.provider as any)?._apiCacheFile,
model: options.provider?.model,
cacheFile: options.cache?.cacheFile,
cacheOutFile: options.cache?.cacheOutFile,
diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts
index 5106ff9df8..2f9161ad51 100644
--- a/packages/playwright-core/src/protocol/validator.ts
+++ b/packages/playwright-core/src/protocol/validator.ts
@@ -1491,6 +1491,7 @@ scheme.PageAgentParams = tObject({
api: tOptional(tString),
apiKey: tOptional(tString),
apiEndpoint: tOptional(tString),
+ apiCacheFile: tOptional(tString),
cacheFile: tOptional(tString),
cacheOutFile: tOptional(tString),
maxTurns: tOptional(tInt),
diff --git a/packages/playwright-core/src/server/agent/context.ts b/packages/playwright-core/src/server/agent/context.ts
index 2a9c93b606..302b729341 100644
--- a/packages/playwright-core/src/server/agent/context.ts
+++ b/packages/playwright-core/src/server/agent/context.ts
@@ -111,8 +111,9 @@ export class Context {
else
promises.push(request.response());
}
+
await progress.race(promises, { timeout: 5000 });
- if (requests.length)
+ if (!promises.length)
await progress.wait(500);
return result;
diff --git a/packages/playwright-core/src/server/agent/pageAgent.ts b/packages/playwright-core/src/server/agent/pageAgent.ts
index 7de4f38fff..20571dbe13 100644
--- a/packages/playwright-core/src/server/agent/pageAgent.ts
+++ b/packages/playwright-core/src/server/agent/pageAgent.ts
@@ -15,6 +15,7 @@
*/
import fs from 'fs';
+import path from 'path';
import { toolsForLoop } from './tool';
import { debug } from '../../utilsBundle';
@@ -96,6 +97,10 @@ async function runLoop(progress: Progress, context: Context, toolDefinitions: To
const { tools, callTool, reportedResult } = toolsForLoop(progress, context, toolDefinitions, { resultSchema });
const secrets = Object.fromEntries((context.agentParams.secrets || [])?.map(s => ([s.name, s.value])));
+ const apiCacheTextBefore = context.agentParams.apiCacheFile ?
+ await fs.promises.readFile(context.agentParams.apiCacheFile, 'utf-8').catch(() => '{}') : '{}';
+ const apiCacheBefore = JSON.parse(apiCacheTextBefore);
+
const loop = new Loop({
api: context.agentParams.api as any,
apiEndpoint: context.agentParams.apiEndpoint,
@@ -108,6 +113,7 @@ async function runLoop(progress: Progress, context: Context, toolDefinitions: To
callTool,
tools,
secrets,
+ cache: apiCacheBefore,
...context.events,
});
@@ -132,6 +138,16 @@ async function runLoop(progress: Progress, context: Context, toolDefinitions: To
await loop.run(task.join('\n'), { signal: progress.signal });
+ if (context.agentParams.apiCacheFile) {
+ const apiCacheAfter = { ...apiCacheBefore, ...loop.cache() };
+ const sortedCache = Object.fromEntries(Object.entries(apiCacheAfter).sort(([a], [b]) => a.localeCompare(b)));
+ const apiCacheTextAfter = JSON.stringify(sortedCache, undefined, 2);
+ if (apiCacheTextAfter !== apiCacheTextBefore) {
+ await fs.promises.mkdir(path.dirname(context.agentParams.apiCacheFile), { recursive: true });
+ await fs.promises.writeFile(context.agentParams.apiCacheFile, apiCacheTextAfter);
+ }
+ }
+
return { result: resultSchema ? reportedResult() : undefined };
}
diff --git a/packages/playwright-core/src/server/progress.ts b/packages/playwright-core/src/server/progress.ts
index 8274edb3f6..39155d6767 100644
--- a/packages/playwright-core/src/server/progress.ts
+++ b/packages/playwright-core/src/server/progress.ts
@@ -74,6 +74,8 @@ export class ProgressController {
metadata: this.metadata,
race: <T>(promise: Promise<T> | Promise<T>[], options?: { timeout?: number }) => {
const promises = Array.isArray(promise) ? promise : [promise];
+ if (!promises.length)
+ return Promise.resolve();
const mt = monotonicTime();
const dl = options?.timeout ? mt + options.timeout : 0;
const timerPromise = dl && (!deadline || dl < deadline) ? new Promise<void>(f => setTimeout(f, dl - mt)) : null;
diff --git a/packages/protocol/src/channels.d.ts b/packages/protocol/src/channels.d.ts
index d967dd43cf..600a3ab63e 100644
--- a/packages/protocol/src/channels.d.ts
+++ b/packages/protocol/src/channels.d.ts
@@ -2586,6 +2586,7 @@ export type PageAgentParams = {
api?: string,
apiKey?: string,
apiEndpoint?: string,
+ apiCacheFile?: string,
cacheFile?: string,
cacheOutFile?: string,
maxTurns?: number,
@@ -2598,6 +2599,7 @@ export type PageAgentOptions = {
api?: string,
apiKey?: string,
apiEndpoint?: string,
+ apiCacheFile?: string,
cacheFile?: string,
cacheOutFile?: string,
maxTurns?: number,
diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml
index 80611cb16f..0d11c2d567 100644
--- a/packages/protocol/src/protocol.yml
+++ b/packages/protocol/src/protocol.yml
@@ -2019,6 +2019,7 @@ Page:
api: string?
apiKey: string?
apiEndpoint: string?
+ apiCacheFile: string?
cacheFile: string?
cacheOutFile: string?
maxTurns: int?
diff --git a/tests/library/__llm_cache__/library-agent-perform-click-a-button.json b/tests/library/__llm_cache__/library-agent-perform-click-a-button.json
new file mode 100644
index 0000000000..26f5b7cdc4
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-perform-click-a-button.json
@@ -0,0 +1,27 @@
+{
+ "0706eb777330056e04cc3abb4715ed3fa1c9d4d5": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I'll click the Submit button for you."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_click",
+ "arguments": {
+ "element": "Submit button",
+ "ref": "e2",
+ "_is_done": true
+ },
+ "id": "toolu_01W54FxjcWYV856RHP3V2xdj"
+ }
+ ]
+ },
+ "usage": {
+ "input": 2893,
+ "output": 101
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/__llm_cache__/library-agent-perform-expect-value.json b/tests/library/__llm_cache__/library-agent-perform-expect-value.json
new file mode 100644
index 0000000000..c4cd9ce537
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-perform-expect-value.json
@@ -0,0 +1,51 @@
+{
+ "77c0828e4bffb298a3850504550ffcac23e6692e": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I'll help you complete this task. Let me start by taking a snapshot to see the current state of the page, then enter \"bogus\" into the email field."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_type",
+ "arguments": {
+ "element": "textbox \"Email Address\"",
+ "ref": "e2",
+ "text": "bogus",
+ "_is_done": false
+ },
+ "id": "toolu_01FfLGgBK1CHZ8e54gQMukA6"
+ }
+ ]
+ },
+ "usage": {
+ "input": 2926,
+ "output": 149
+ }
+ },
+ "ca3ff77a849d4eacf88d299dc8727d63724a68aa": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "Perfect! I have successfully completed all three parts of the task:\n\n1. ✅ **Entered \"bogus\" into the email field** - The text has been typed into the textbox\n2. ✅ **Verified the value is \"bogus\"** - The page snapshot shows the textbox contains \"bogus\"\n3. ✅ **Confirmed error message is displayed** - The error message \"Error: Invalid email address\" is now visible on the page\n\nThe task is complete."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_snapshot",
+ "arguments": {
+ "_is_done": true
+ },
+ "id": "toolu_018wjD4H2S5QHQ3rpE2sR7bM"
+ }
+ ]
+ },
+ "usage": {
+ "input": 3138,
+ "output": 167
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/__llm_cache__/library-agent-perform-extract-task.json b/tests/library/__llm_cache__/library-agent-perform-extract-task.json
new file mode 100644
index 0000000000..74f9d15fbb
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-perform-extract-task.json
@@ -0,0 +1,35 @@
+{
+ "b5f1ee58f1051afb059b3b33e8be41576a1324a4": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I'll extract the todo items and their statuses from the page snapshot."
+ },
+ {
+ "type": "tool_call",
+ "name": "report_result",
+ "arguments": {
+ "items": [
+ {
+ "title": "Buy groceries",
+ "completed": true
+ },
+ {
+ "title": "Buy milk",
+ "completed": false
+ }
+ ],
+ "_is_done": true
+ },
+ "id": "toolu_01WvevmDTBxmKg79Ji6vQvpH"
+ }
+ ]
+ },
+ "usage": {
+ "input": 888,
+ "output": 133
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/__llm_cache__/library-agent-perform-perform-history.json b/tests/library/__llm_cache__/library-agent-perform-perform-history.json
new file mode 100644
index 0000000000..00a85f8150
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-perform-perform-history.json
@@ -0,0 +1,52 @@
+{
+ "4f9987bc22c6035501274532494c185d1270aba2": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I'll click the Fox button again as requested."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_click",
+ "arguments": {
+ "element": "Fox button",
+ "ref": "e3",
+ "_is_done": true
+ },
+ "id": "toolu_013sypFp7tiRCLaR3i3oWrei"
+ }
+ ]
+ },
+ "usage": {
+ "input": 2946,
+ "output": 102
+ }
+ },
+ "79e2d58996075c4ebe8eb9e557bd0cdb8b04211a": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I'll click the Fox button for you."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_click",
+ "arguments": {
+ "element": "Fox button",
+ "ref": "e3",
+ "_is_done": true
+ },
+ "id": "toolu_01E6xBUYzQEdRrkWoobbUbFq"
+ }
+ ]
+ },
+ "usage": {
+ "input": 2933,
+ "output": 101
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/__llm_cache__/library-agent-perform-perform-run-timeout.json b/tests/library/__llm_cache__/library-agent-perform-perform-run-timeout.json
new file mode 100644
index 0000000000..836cd0bc34
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-perform-perform-run-timeout.json
@@ -0,0 +1,27 @@
+{
+ "c2f54e956e3131255f03c5484a637b580f7d695f": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I'll click the Fox button for you."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_click",
+ "arguments": {
+ "element": "Fox button",
+ "ref": "e3",
+ "_is_done": true
+ },
+ "id": "toolu_01GipHDfLSETD8VyJt6D6ng5"
+ }
+ ]
+ },
+ "usage": {
+ "input": 2919,
+ "output": 101
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/__llm_cache__/library-agent-perform-retrieve-a-secret.json b/tests/library/__llm_cache__/library-agent-perform-retrieve-a-secret.json
new file mode 100644
index 0000000000..f60e4427dd
--- /dev/null
+++ b/tests/library/__llm_cache__/library-agent-perform-retrieve-a-secret.json
@@ -0,0 +1,28 @@
+{
+ "c1d41afc231b64caf136132dd841ca8f45876674": {
+ "result": {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "I'll enter the x-secret-email value into the email field."
+ },
+ {
+ "type": "tool_call",
+ "name": "browser_type",
+ "arguments": {
+ "element": "Email Address textbox",
+ "ref": "e2",
+ "text": "%x-secret-email%",
+ "_is_done": true
+ },
+ "id": "toolu_018PD6jsENphY4ERa1XRF4ZN"
+ }
+ ]
+ },
+ "usage": {
+ "input": 2901,
+ "output": 132
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/library/agent-helpers.ts b/tests/library/agent-helpers.ts
new file mode 100644
index 0000000000..42d11e58a0
--- /dev/null
+++ b/tests/library/agent-helpers.ts
@@ -0,0 +1,72 @@
+/**
+ * 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 fs from 'fs';
+import path from 'path';
+
+import { browserTest as test } from '../config/browserTest';
+import type { BrowserContext, Page, PageAgent } from '@playwright/test';
+
+function cacheFile() {
+ return test.info().outputPath('agent-cache.json');
+}
+
+export async function cacheObject() {
+ return JSON.parse(await fs.promises.readFile(cacheFile(), 'utf8'));
+}
+
+export async function generateAgent(context: BrowserContext, options: { secrets?: Record<string, string> } = {}) {
+ const apiCacheFile = path.join(__dirname, '__llm_cache__', sanitizeFileName(test.info().titlePath.join(' ')) + '.json');
+
+ const page = await context.newPage();
+ const agent = await page.agent({
+ provider: {
+ api: 'anthropic' as const,
+ apiKey: process.env.AZURE_SONNET_API_KEY ?? 'dummy',
+ apiEndpoint: process.env.AZURE_SONNET_ENDPOINT ?? 'dummy',
+ model: 'claude-sonnet-4-5',
+ ...{ _apiCacheFile: apiCacheFile }
+ },
+ ...options,
+ cache: {
+ cacheFile: cacheFile(),
+ }
+ });
+ return { page, agent };
+}
+
+export async function runAgent(context: BrowserContext, options: { secrets?: Record<string, string> } = {}) {
+ const page = await context.newPage();
+ const agent = await page.agent({
+ cache: { cacheFile: cacheFile() }
+ });
+ return { page, agent };
+}
+
+export async function run(context: BrowserContext, callback: (page: Page, agent: PageAgent) => Promise<void>, options: { secrets?: Record<string, string> } = {}) {
+ {
+ const { page, agent } = await generateAgent(context, options);
+ await callback(page, agent);
+ }
+ {
+ const { page, agent } = await runAgent(context, options);
+ await callback(page, agent);
+ }
+}
+
+function sanitizeFileName(name: string): string {
+ return name.replace('.spec.ts', '').replace(/[^a-zA-Z0-9_]+/g, '-');
+}
diff --git a/tests/library/agent-perform.spec.ts b/tests/library/agent-perform.spec.ts
new file mode 100644
index 0000000000..c6020f58b7
--- /dev/null
+++ b/tests/library/agent-perform.spec.ts
@@ -0,0 +1,156 @@
+/**
+ * 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 { z } from 'zod';
+
+import { browserTest as test, expect } from '../config/browserTest';
+import { run, generateAgent, cacheObject, runAgent } from './agent-helpers';
+
+// LOWIRE_NO_CACHE=1 to generate api caches
+// LOWIRE_FORCE_CACHE=1 to force api caches
+
+test('click a button', async ({ context }) => {
+ await run(context, async (page, agent) => {
+ let clicked = 0;
+ await page.exposeFunction('clicked', () => ++clicked);
+ await page.setContent(`<button onclick="clicked()">Submit</button>`);
+ await agent.perform('click the Submit button');
+ expect(clicked).toBe(1);
+ });
+
+ expect(await cacheObject()).toEqual({
+ 'click the Submit button': {
+ actions: [{
+ code: `await page.getByRole('button', { name: 'Submit' }).click();`,
+ method: 'click',
+ selector: `internal:role=button[name=\"Submit\"i]`,
+ }],
+ },
+ });
+});
+
+test('retrieve a secret', async ({ context }) => {
+ await run(context, async (page, agent) => {
+ await page.setContent('<input type="email" name="email" placeholder="Email Address"/>');
+ await agent.perform('Enter x-secret-email into the email field');
+ await expect(page.locator('body')).toMatchAriaSnapshot(`
+ - textbox "Email Address": [email protected]
+ `);
+ }, { secrets: { 'x-secret-email': '[email protected]' } });
+
+ expect(await cacheObject()).toEqual({
+ 'Enter x-secret-email into the email field': {
+ actions: [{
+ code: `await page.getByRole('textbox', { name: 'Email Address' }).fill('[email protected]');`,
+ method: 'fill',
+ selector: `internal:role=textbox[name=\"Email Address\"i]`,
+ }],
+ },
+ });
+});
+
+test('extract task', async ({ context }) => {
+ const { page, agent } = await generateAgent(context);
+ await page.setContent(`
+ <ul>
+ <li>Buy groceries [DONE]</li>
+ <li>Buy milk [PENDING]</li>
+ </ul>
+ `);
+ const { result } = await agent.extract('List todos with their statuses', z.object({
+ items: z.object({
+ title: z.string(),
+ completed: z.boolean(),
+ }).array(),
+ }));
+
+ expect(result.items).toEqual([
+ { title: 'Buy groceries', completed: true },
+ { title: 'Buy milk', completed: false }
+ ]);
+});
+
+test('expect value', async ({ context }) => {
+ const task = `
+ - Enter "bogus" into the email field
+ - Check that the value is in fact "bogus"
+ - Check that the error message is displayed
+`;
+
+ await run(context, async (page, agent) => {
+ await page.setContent(`
+ <script>
+ function onInput(event) {
+ if (!event.target.value.match(/^[^@]+@[^@]+$/))
+ document.getElementById('error').style.display = 'block';
+ else
+ document.getElementById('error').style.display = 'none';
+ }
+ </script>
+ <input type="email" name="email" placeholder="Email Address" oninput="onInput(event);"/>
+ <div id="error" style="color: red; display: none;">Error: Invalid email address</div>
+ `);
+ await agent.perform(task);
+ });
+
+ const expectations = {};
+ expectations[task.trim()] = {
+ actions: [{
+ code: `await page.getByRole('textbox', { name: 'Email Address' }).fill('bogus');`,
+ method: 'fill',
+ selector: `internal:role=textbox[name=\"Email Address\"i]`,
+ text: 'bogus',
+ }],
+ };
+ expect(await cacheObject()).toEqual(expectations);
+});
+
+test('perform history', async ({ context }) => {
+ await run(context, async (page, agent) => {
+ let clicked = 0;
+ await page.exposeFunction('clicked', () => clicked++);
+ await page.setContent(`
+ <button>Wolf</button>
+ <button onclick="clicked()">Fox</button>
+ <button>Rabbit</button>
+ `);
+ await agent.perform('click the Fox button');
+ await agent.perform('click the Fox button again');
+ expect(clicked).toBe(2);
+ });
+});
+
+test('perform run timeout', async ({ context }) => {
+ {
+ const { page, agent } = await generateAgent(context);
+ await page.setContent(`
+ <button>Wolf</button>
+ <button>Fox</button>
+ `);
+ await agent.perform('click the Fox button');
+ }
+ {
+ const { page, agent } = await runAgent(context);
+ await page.setContent(`
+ <button>Wolf</button>
+ <button>Rabbit</button>
+ `);
+ const error = await agent.perform('click the Fox button', { timeout: 3000 }).catch(e => e);
+ expect(error.message).toContain('Timeout 3000ms exceeded.');
+ expect(error.message).toContain(`waiting for getByRole('button', { name: 'Fox' })`);
+ }
+});
diff --git a/tests/library/perform-task.spec.ts b/tests/library/perform-task.spec.ts
deleted file mode 100644
index 3ff85854d9..0000000000
--- a/tests/library/perform-task.spec.ts
+++ /dev/null
@@ -1,121 +0,0 @@
-/**
- * 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 z from 'zod';
-
-import { browserTest as test, expect } from '../config/browserTest';
-
-test.use({
- agentOptions: {
- api: 'anthropic',
- apiKey: process.env.AZURE_SONNET_API_KEY!,
- apiEndpoint: process.env.AZURE_SONNET_ENDPOINT!,
- model: 'claude-sonnet-4-5',
- secrets: {
- 'x-secret-email': '[email protected]',
- }
- }
-});
-
-test('page.perform', async ({ page, agent, server }) => {
- await page.goto(server.PREFIX + '/evals/fill-form.html');
- agent.on('turn', turn => {
- // For debugging purposes it is on for now.
- console.log('agentturn', turn);
- });
- await agent.perform('Fill out the form with the following details:\n' +
- 'Name: John Smith\n' +
- 'Address: 1045 La Avenida St, Mountain View, CA 94043\n' +
- 'Email: [email protected]');
- await expect(page.locator('body')).toMatchAriaSnapshot(`
- - textbox "Full Name *": John Smith
- - textbox "Email Address *": [email protected]
- - textbox "Street Address *": 1045 La Avenida St
- - textbox "City *": Mountain View
- - textbox "State/Province *": CA
- - textbox "ZIP/Postal Code *": 94043
- `);
-});
-
-test('page.perform secret', async ({ page, agent }) => {
- await page.setContent('<input type="email" name="email" placeholder="Email Address"/>');
- await agent.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, agent }) => {
- await page.goto('https://demo.playwright.dev/todomvc');
- await agent.perform('Add "Buy groceries" todo');
- console.log(await agent.extract('List todos with their statuses', z.object({
- items: z.object({
- title: z.string(),
- completed: z.boolean()
- }).array()
- })));
-});
-
-test('page.perform expect value', async ({ page, agent }) => {
- await page.setContent(`
- <script>
- function onInput(event) {
- if (!event.target.value.match(/^[^@]+@[^@]+$/))
- document.getElementById('error').style.display = 'block';
- else
- document.getElementById('error').style.display = 'none';
- }
- </script>
- <input type="email" name="email" placeholder="Email Address" oninput="onInput(event);"/>
- <div id="error" style="color: red; display: none;">Error: Invalid email address</div>
- `);
- await agent.perform(`
- - Enter "bogus" into the email field
- - Check that the value is in fact "bogus"
- - Check that the error message is displayed
- `);
-});
-
-test('page.perform history', async ({ page, agent }) => {
- let clicked = 0;
- await page.exposeFunction('clicked', () => clicked++);
- await page.setContent(`
- <button>Wolf</button>
- <button onclick="clicked()">Fox</button>
- <button>Rabbit</button>
- `);
- await agent.perform('click the Fox button');
- await agent.perform('click the Fox button again');
- expect(clicked).toBe(2);
-});
-
-test('page.perform error', async ({ page, agent, agentOptions }, testInfo) => {
- if (testInfo.config.runAgents !== 'none') {
- await page.setContent(`
- <button>Wolf</button>
- <button>Fox</button>
- `);
- await agent.perform('click the Fox button');
- } else {
- await page.setContent(`
- <button>Wolf</button>
- <button>Rabbit</button>
- `);
- const error = await agent.perform('click the Fox button', { timeout: 3000 }).catch(e => e);
- expect(error.message).toContain('Timeout 3000ms exceeded.');
- expect(error.message).toContain(`waiting for getByRole('button', { name: 'Fox' })`);
- }
-});