Skip to content

Commit 03ca920

Browse files
authored
feat(web-integration): add more method for playwright fixture (#1024)
* feat(web-integration): wrap agent methods with test.step for enhanced UI visibility * Revert "feat(web-integration): wrap agent methods with test.step for enhanced UI visibility" This reverts commit 13de6f4. * feat(web-integration): add more method for playwright fixture
1 parent 6db23bc commit 03ca920

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

apps/site/docs/en/integrate-with-playwright.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export const test = base.extend<PlayWrightAiFixtureType>(
183183
- `aiInput` - Input operation
184184
- `aiKeyboardPress` - Keyboard operation
185185
- `aiScroll` - Scroll operation
186+
- `aiRightClick` - Right-click operation
186187

187188
#### Query
188189

@@ -197,6 +198,12 @@ export const test = base.extend<PlayWrightAiFixtureType>(
197198
- `aiAssert` - AI Assertion
198199
- `aiWaitFor` - AI Wait
199200
- `aiLocate` - Locate Element
201+
- `runYaml` - Execute YAML automation script
202+
- `setAIActionContext` - Set AI action context, send background knowledge to AI model when calling `agent.aiAction()`
203+
- `evaluateJavaScript` - Execute JavaScript in page context
204+
- `logScreenshot` - Log screenshot with description in report file
205+
- `freezePageContext` - Freeze page context
206+
- `unfreezePageContext` - Unfreeze page context
200207

201208
Besides the exposed shortcut methods, if you need to call other [API](./api.mdx) provided by the agent, you can use `agentForPage` to get the `PageAgent` instance, and use `PageAgent` to call the API for interaction:
202209

@@ -230,6 +237,8 @@ test('search headphone on ebay', async ({
230237
aiTap,
231238
aiScroll,
232239
aiWaitFor,
240+
aiRightClick,
241+
logScreenshot,
233242
}) => {
234243
// Use aiInput to enter search keyword
235244
await aiInput('Headphones', 'search box');
@@ -259,6 +268,11 @@ test('search headphone on ebay', async ({
259268

260269
// Use aiAssert to verify filter functionality
261270
await aiAssert('category filter exists on the left side');
271+
272+
// Use logScreenshot to capture the current state
273+
await logScreenshot('Search Results', {
274+
content: 'Final search results for headphones',
275+
});
262276
});
263277
```
264278

apps/site/docs/zh/integrate-with-playwright.mdx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export const test = base.extend<PlayWrightAiFixtureType>(
183183
- `aiInput` - 输入操作
184184
- `aiKeyboardPress` - 键盘操作
185185
- `aiScroll` - 滚动操作
186+
- `aiRightClick` - 右键点击操作
186187

187188
#### 查询
188189

@@ -197,6 +198,12 @@ export const test = base.extend<PlayWrightAiFixtureType>(
197198
- `aiAssert` - 断言
198199
- `aiWaitFor` - 等待
199200
- `aiLocate` - 定位元素
201+
- `runYaml` - 执行 YAML 自动化脚本
202+
- `setAIActionContext` - 设置 AI 动作上下文,在调用 `agent.aiAction()` 时,发送给 AI 模型的背景知识
203+
- `evaluateJavaScript` - 在页面上下文中执行 JavaScript
204+
- `logScreenshot` - 在报告文件中记录当前截图,并添加描述
205+
- `freezePageContext` - 冻结页面上下文
206+
- `unfreezePageContext` - 解冻页面上下文
200207

201208
除了上述暴露的快捷方法之外,如果还需要调用其它 agent 提供的 [API](./api.mdx),请使用 `agentForPage` 获取 `PageAgent` 实例,使用 `PageAgent` 调用 API 进行交互:
202209

@@ -210,8 +217,6 @@ test('case demo', async ({ agentForPage, page }) => {
210217
});
211218
```
212219

213-
214-
215220
#### 示例代码
216221

217222
```typescript title="./e2e/ebay-search.spec.ts"
@@ -232,6 +237,8 @@ test('search headphone on ebay', async ({
232237
aiTap,
233238
aiScroll,
234239
aiWaitFor,
240+
aiRightClick,
241+
logScreenshot,
235242
}) => {
236243
// 使用 aiInput 输入搜索关键词
237244
await aiInput('Headphones', '搜索框');
@@ -262,8 +269,11 @@ test('search headphone on ebay', async ({
262269

263270
// 使用 aiAssert 验证筛选功能
264271
await aiAssert('界面左侧有类目筛选功能');
272+
273+
// 使用 logScreenshot 记录当前状态
274+
await logScreenshot('搜索结果', { content: '耳机搜索的最终结果' });
265275
});
266-
````
276+
```
267277

268278
更多 Agent 的 API 讲解请参考 [API 参考](./api.mdx)
269279

packages/web-integration/src/playwright/ai-fixture.ts

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ export const PlaywrightAiFixture = (options?: {
108108
| 'aiNumber'
109109
| 'aiString'
110110
| 'aiBoolean'
111-
| 'aiAsk';
111+
| 'aiAsk'
112+
| 'runYaml'
113+
| 'setAIActionContext'
114+
| 'evaluateJavaScript'
115+
| 'logScreenshot'
116+
| 'freezePageContext'
117+
| 'unfreezePageContext';
112118
}) {
113119
const { page, testInfo, use, aiActionType } = options;
114120
const agent = createOrReuseAgentForPage(page, testInfo, {
@@ -373,6 +379,78 @@ export const PlaywrightAiFixture = (options?: {
373379
aiActionType: 'aiAsk',
374380
});
375381
},
382+
runYaml: async (
383+
{ page }: { page: OriginPlaywrightPage },
384+
use: any,
385+
testInfo: TestInfo,
386+
) => {
387+
await generateAiFunction({
388+
page,
389+
testInfo,
390+
use,
391+
aiActionType: 'runYaml',
392+
});
393+
},
394+
setAIActionContext: async (
395+
{ page }: { page: OriginPlaywrightPage },
396+
use: any,
397+
testInfo: TestInfo,
398+
) => {
399+
await generateAiFunction({
400+
page,
401+
testInfo,
402+
use,
403+
aiActionType: 'setAIActionContext',
404+
});
405+
},
406+
evaluateJavaScript: async (
407+
{ page }: { page: OriginPlaywrightPage },
408+
use: any,
409+
testInfo: TestInfo,
410+
) => {
411+
await generateAiFunction({
412+
page,
413+
testInfo,
414+
use,
415+
aiActionType: 'evaluateJavaScript',
416+
});
417+
},
418+
logScreenshot: async (
419+
{ page }: { page: OriginPlaywrightPage },
420+
use: any,
421+
testInfo: TestInfo,
422+
) => {
423+
await generateAiFunction({
424+
page,
425+
testInfo,
426+
use,
427+
aiActionType: 'logScreenshot',
428+
});
429+
},
430+
freezePageContext: async (
431+
{ page }: { page: OriginPlaywrightPage },
432+
use: any,
433+
testInfo: TestInfo,
434+
) => {
435+
await generateAiFunction({
436+
page,
437+
testInfo,
438+
use,
439+
aiActionType: 'freezePageContext',
440+
});
441+
},
442+
unfreezePageContext: async (
443+
{ page }: { page: OriginPlaywrightPage },
444+
use: any,
445+
testInfo: TestInfo,
446+
) => {
447+
await generateAiFunction({
448+
page,
449+
testInfo,
450+
use,
451+
aiActionType: 'unfreezePageContext',
452+
});
453+
},
376454
};
377455
};
378456

@@ -420,4 +498,22 @@ export type PlayWrightAiFixtureType = {
420498
aiAsk: (
421499
...args: Parameters<PageAgent['aiAsk']>
422500
) => ReturnType<PageAgent['aiAsk']>;
501+
runYaml: (
502+
...args: Parameters<PageAgent['runYaml']>
503+
) => ReturnType<PageAgent['runYaml']>;
504+
setAIActionContext: (
505+
...args: Parameters<PageAgent['setAIActionContext']>
506+
) => ReturnType<PageAgent['setAIActionContext']>;
507+
evaluateJavaScript: (
508+
...args: Parameters<PageAgent['evaluateJavaScript']>
509+
) => ReturnType<PageAgent['evaluateJavaScript']>;
510+
logScreenshot: (
511+
...args: Parameters<PageAgent['logScreenshot']>
512+
) => ReturnType<PageAgent['logScreenshot']>;
513+
freezePageContext: (
514+
...args: Parameters<PageAgent['freezePageContext']>
515+
) => ReturnType<PageAgent['freezePageContext']>;
516+
unfreezePageContext: (
517+
...args: Parameters<PageAgent['unfreezePageContext']>
518+
) => ReturnType<PageAgent['unfreezePageContext']>;
423519
};

0 commit comments

Comments
 (0)