Skip to content

Commit

Permalink
fix: type error
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyutaotao committed Feb 10, 2025
1 parent 4b92dce commit bdfa23e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/web-integration/src/common/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ export class PageAgent<PageType extends WebPage = WebPage> {
);
}

async runYaml(yamlScriptContent: string) {
async runYaml(yamlScriptContent: string): Promise<{
result: Record<string, any>;
}> {
const script = parseYamlScript(yamlScriptContent, 'yaml', true);
const player = new ScriptPlayer(script, async (target) => {
return { agent: this, freeFn: [] };
Expand Down
89 changes: 89 additions & 0 deletions packages/web-integration/tests/ai/web/puppeteer/agent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { PuppeteerAgent } from '@/puppeteer';
import { sleep } from '@midscene/core/utils';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { launchPage } from './utils';

vi.setConfig({
testTimeout: 120 * 1000,
});

describe('puppeteer integration', () => {
let resetFn: () => Promise<void>;
afterEach(async () => {
if (resetFn) {
await resetFn();
}
});

it('Sauce Demo, agent with yaml script', async () => {
const { originPage, reset } = await launchPage('https://www.bing.com/');
resetFn = reset;
const agent = new PuppeteerAgent(originPage);
await sleep(3000);
const { result } = await agent.runYaml(
`
tasks:
- name: search weather
flow:
- ai: input 'weather today' in input box, click search button
- sleep: 3000
- name: query weather
flow:
- aiQuery: "the result shows the weather info, {description: string}"
name: weather
`,
);

expect(result.weather.description).toBeDefined();
});

it('assertion failed', async () => {
const { originPage, reset } = await launchPage('https://www.bing.com/');
resetFn = reset;
const agent = new PuppeteerAgent(originPage);
let errorMsg = '';
try {
await agent.runYaml(
`
tasks:
- name: search weather
flow:
- aiAssert: the result shows food delivery service
`,
);
} catch (e: any) {
errorMsg = e.message;
}

const multiLineErrorMsg = errorMsg.split('\n');
expect(multiLineErrorMsg.length).toBeGreaterThan(2);
});

it('allow error in flow', async () => {
const { originPage, reset } = await launchPage('https://www.bing.com/');
resetFn = reset;
const agent = new PuppeteerAgent(originPage);
const { result } = await agent.runYaml(
`
tasks:
- name: search weather
flow:
- ai: input 'weather today' in input box, click search button
- sleep: 3000
- name: query weather
flow:
- aiQuery: "the result shows the weather info, {description: string}"
name: weather
- name: error
continueOnError: true
flow:
- aiAssert: the result shows food delivery service
`,
);

expect(result.weather.description).toBeDefined();
});
});

0 comments on commit bdfa23e

Please sign in to comment.