Skip to content

Commit 342d6aa

Browse files
authored
chore: move to page.agent({ params }) api (#38735)
1 parent 391081e commit 342d6aa

File tree

50 files changed

+1036
-1484
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1036
-1484
lines changed

docs/src/api/class-browser.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,6 @@ await browser.CloseAsync();
267267
### option: Browser.newContext.storageStatePath = %%-csharp-java-context-option-storage-state-path-%%
268268
* since: v1.9
269269

270-
### option: Browser.newContext.agent = %%-context-option-agent-%%
271-
* since: v1.58
272-
273270
## async method: Browser.newPage
274271
* since: v1.8
275272
- returns: <[Page]>

docs/src/api/class-page.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -544,17 +544,6 @@ sequence of events is `request`, `response` and `requestfinished`.
544544
Emitted when [response] status and headers are received for a request. For a successful response, the sequence of events
545545
is `request`, `response` and `requestfinished`.
546546

547-
## event: Page.agentTurn
548-
* since: v1.58
549-
- argument: <[Object]>
550-
- `role` <[string]>
551-
- `message` <[string]>
552-
- `usage` ?<[Object]>
553-
- `inputTokens` <[int]>
554-
- `outputTokens` <[int]>
555-
556-
Emitted when the agent makes a turn.
557-
558547
## event: Page.webSocket
559548
* since: v1.9
560549
- argument: <[WebSocket]>
@@ -720,9 +709,42 @@ current working directory.
720709

721710
Raw CSS content to be injected into frame.
722711

723-
## property: Page.agent
712+
## async method: Page.agent
724713
* since: v1.58
725-
- type: <[PageAgent]>
714+
- returns: <[PageAgent]>
715+
716+
Initialize page agent with the llm provider and cache.
717+
718+
### option: Page.agent.cache
719+
* since: v1.58
720+
- `cache` <[Object]>
721+
- `cacheFile` ?<[string]> Cache file to use/generate code for performed actions into. Cache is not used if not specified (default).
722+
- `cacheOutFile` ?<[string]> When specified, generated entries are written into the `cacheOutFile` instead of updating the `cacheFile`.
723+
724+
### option: Page.agent.maxTurns
725+
* since: v1.58
726+
- `maxTurns` <[int]>
727+
728+
Maximum number of agentic turns to take per call. Defaults to 10.
729+
730+
### option: Page.agent.maxTokens
731+
* since: v1.58
732+
- `maxTokens` ?<[int]>
733+
734+
### option: Page.agent.provider
735+
* since: v1.58
736+
- `provider` <[Object]>
737+
- `api` <[PageAgentAPI]<"openai"|"openai-compatible"|"anthropic"|"google">> API to use.
738+
- `apiEndpoint` ?<[string]> Endpoint to use if different from default.
739+
- `apiKey` <[string]> API key for the LLM provider.
740+
- `model` <[string]> Model identifier within the provider. Required in non-cache mode.
741+
742+
### option: Page.agent.secrets
743+
* since: v1.58
744+
- `secrets` ?<[Object]<[string], [string]>>
745+
746+
Secrets to hide from the LLM.
747+
726748

727749
## async method: Page.bringToFront
728750
* since: v1.8

docs/src/api/class-pageagent.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
# class: PageAgent
22
* since: v1.58
33

4+
## event: PageAgent.turn
5+
* since: v1.58
6+
- argument: <[Object]>
7+
- `role` <[string]>
8+
- `message` <[string]>
9+
- `usage` ?<[Object]>
10+
- `inputTokens` <[int]>
11+
- `outputTokens` <[int]>
12+
13+
Emitted when the agent makes a turn.
14+
15+
## async method: PageAgent.dispose
16+
* since: v1.58
17+
18+
Dispose this agent.
419

520
## async method: PageAgent.expect
621
* since: v1.58
@@ -10,7 +25,7 @@ Expect certain condition to be met.
1025
**Usage**
1126

1227
```js
13-
await page.agent.expect('"0 items" to be reported');
28+
await agent.expect('"0 items" to be reported');
1429
```
1530

1631
### param: PageAgent.expect.expectation
@@ -36,7 +51,7 @@ Extract information from the page using the agentic loop, return it in a given Z
3651
**Usage**
3752

3853
```js
39-
await page.agent.extract('List of items in the cart', z.object({
54+
await agent.extract('List of items in the cart', z.object({
4055
title: z.string().describe('Item title to extract'),
4156
price: z.string().describe('Item price to extract'),
4257
}).array());
@@ -69,7 +84,7 @@ Perform action using agentic loop.
6984
**Usage**
7085

7186
```js
72-
await page.agent.perform('Click submit button');
87+
await agent.perform('Click submit button');
7388
```
7489

7590
### param: PageAgent.perform.task

docs/src/api/params.md

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -370,40 +370,6 @@ It makes the execution of the tests non-deterministic.
370370
Emulates consistent window screen size available inside web page via `window.screen`. Is only used when the
371371
[`option: viewport`] is set.
372372

373-
## context-option-agent
374-
- `agent` <[Object]>
375-
- `api` ?<[string]> API to use, `openapi`, `google` or `anthropic`. Required in non-cache mode.
376-
- `apiEndpoint` ?<[string]> Endpoint to use if different from default.
377-
- `apiKey` ?<[string]> API key for the LLM provider.
378-
- `model` ?<[string]> Model identifier within the provider. Required in non-cache mode.
379-
- `cacheFile` ?<[string]> Cache file to use/generate code for performed actions into. Cache is not used if not specified (default).
380-
- `cacheOutFile` ?<[string]> When specified, generated entries are written into the `cacheOutFile` instead of updating the `cacheFile`.
381-
- `secrets` ?<[Object]<[string], [string]>> Secrets to hide from the LLM.
382-
- `maxTurns` ?<[int]> Maximum number of agentic turns to take per call. Defaults to 10.
383-
- `maxTokens` ?<[int]> Maximum number of tokens to consume per call. The agentic loop will stop after input + output tokens exceed this value. Defaults on unlimited.
384-
385-
Agent settings for [`property: Page.agent`].
386-
387-
## page-agent-api
388-
* since: v1.58
389-
- `api` <[string]>
390-
391-
API to use, `openapi`, `google` or `anthropic`. Required in non-cache mode.
392-
393-
## page-agent-api-endpoint
394-
* since: v1.58
395-
- `apiEndpoint` <[string]>
396-
397-
Endpoint to use if different from default.
398-
399-
## page-agent-api-key
400-
* since: v1.58
401-
- `apiKey` <[string]>
402-
403-
API key for the LLM provider.
404-
405-
API version if relevant.
406-
407373
## page-agent-cache-key
408374
* since: v1.58
409375
- `cacheKey` <[string]>
@@ -425,9 +391,6 @@ Defaults to context-wide value specified in `agent` property.
425391
Maximum number of agentic turns during this call, defaults to context-wide value specified in `agent` property.
426392

427393
## page-agent-call-options-v1.58
428-
- %%-page-agent-api-%%
429-
- %%-page-agent-api-key-%%
430-
- %%-page-agent-api-endpoint-%%
431394
- %%-page-agent-cache-key-%%
432395
- %%-page-agent-max-tokens-%%
433396
- %%-page-agent-max-turns-%%

docs/src/test-api/class-fixtures.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Given the test above, Playwright Test will set up the `page` fixture before runn
1818

1919
Playwright Test comes with builtin fixtures listed below, and you can add your own fixtures as well. Playwright Test also [provides options][TestOptions] to configure [`property: Fixtures.browser`], [`property: Fixtures.context`] and [`property: Fixtures.page`].
2020

21+
## property: Fixtures.agent
22+
* since: v1.58
23+
- type: <[PageAgent]>
24+
2125
## property: Fixtures.browser
2226
* since: v1.10
2327
- type: <[Browser]>

docs/src/test-api/class-fullconfig.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Base directory for all relative paths used in the reporters.
108108
* since: v1.58
109109
- type: <['RunAgentsMode]<"all"|"missing"|"none">>
110110

111-
Whether to run LLM agent for [`property: Page.agent`]:
111+
Whether to run LLM agent for [PageAgent]:
112112
* "all" disregards existing cache and performs all actions via LLM
113113
* "missing" only performs actions that don't have generated cache actions
114114
* "none" does not talk to LLM at all, relies on the cached actions (default)

docs/src/test-api/class-testconfig.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ export default defineConfig({
518518
* since: v1.58
519519
- type: ?<['RunAgentsMode]<"all"|"missing"|"none">>
520520

521-
Whether to run LLM agent for [`property: Page.agent`]:
521+
Whether to run LLM agent for [PageAgent]:
522522
* "all" disregards existing cache and performs all actions via LLM
523523
* "missing" only performs actions that don't have generated cache actions
524524
* "none" does not talk to LLM at all, relies on the cached actions (default)

docs/src/test-api/class-testoptions.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ export default defineConfig({
4646
});
4747
```
4848

49-
## property: TestOptions.agent
49+
## property: TestOptions.agentOptions
5050
* since: v1.58
5151
- type: <[Object]>
52-
- `provider` ?<[string]> LLM provider to use. Required in non-cache mode.
52+
- `api` ?<[string]> LLM provider to use. Required in non-cache mode.
53+
- `apiKey` ?<[string]> Key for the LLM provider.
54+
- `apiEndpoint` ?<[string]> LLM provider endpoint.
5355
- `model` ?<[string]> Model identifier within the provider. Required in non-cache mode.
5456
- `cachePathTemplate` ?<[string]> Cache file template to use/generate code for performed actions into.
5557
- `maxTurns` ?<[int]> Maximum number of agentic turns to take per call. Defaults to 10.

examples/todomvc/tests/fixtures.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { test as baseTest } from '@playwright/test';
55
export { expect } from '@playwright/test';
66

77
export const test = baseTest.extend({
8+
agentOptions: {
9+
api: 'anthropic',
10+
apiKey: process.env.AZURE_SONNET_API_KEY!,
11+
apiEndpoint: process.env.AZURE_SONNET_ENDPOINT!,
12+
model: 'claude-sonnet-4-5',
13+
},
814
page: async ({ page }, use) => {
915
await page.goto('https://demo.playwright.dev/todomvc');
1016
await use(page);
Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
import { test } from '../../fixtures';
22

3-
test.use({
4-
agent: {
5-
api: 'anthropic',
6-
apiKey: process.env.AZURE_SONNET_API_KEY!,
7-
apiEndpoint: process.env.AZURE_SONNET_ENDPOINT!,
8-
model: 'claude-sonnet-4-5',
9-
}
10-
});
11-
12-
test('should complete multiple todos', async ({ page }) => {
13-
await page.agent.expect(`The page loads with an empty todo list`);
3+
test('should complete multiple todos', async ({ agent }) => {
4+
await agent.expect(`The page loads with an empty todo list`);
145

15-
await page.agent.perform(`Add three todos: 'Buy milk', 'Walk dog', 'Finish report'`);
16-
await page.agent.expect(`All three todos are visible`);
17-
await page.agent.expect(`Counter shows '3 items left'`);
6+
await agent.perform(`Add three todos: 'Buy milk', 'Walk dog', 'Finish report'`);
7+
await agent.expect(`All three todos are visible`);
8+
await agent.expect(`Counter shows '3 items left'`);
189

19-
await page.agent.perform(`Complete the first todo by clicking its checkbox`);
20-
await page.agent.expect(`First todo is marked as complete`);
21-
await page.agent.expect(`Counter shows '2 items left'`);
10+
await agent.perform(`Complete the first todo by clicking its checkbox`);
11+
await agent.expect(`First todo is marked as complete`);
12+
await agent.expect(`Counter shows '2 items left'`);
2213

23-
await page.agent.perform(`Complete the third todo by clicking its checkbox`);
24-
await page.agent.expect(`Third todo is marked as complete`);
25-
await page.agent.expect(`Counter shows '1 item left'`);
26-
await page.agent.expect(`The 'Clear completed' button appears`);
14+
await agent.perform(`Complete the third todo by clicking its checkbox`);
15+
await agent.expect(`Third todo is marked as complete`);
16+
await agent.expect(`Counter shows '1 item left'`);
17+
await agent.expect(`The 'Clear completed' button appears`);
2718
});

0 commit comments

Comments
 (0)