2025-02-10 20:00:14 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-02-13 15:53:53 +08:00
|
|
|
it('input and clear text', async () => {
|
|
|
|
const { originPage, reset } = await launchPage('https://www.google.com/');
|
|
|
|
resetFn = reset;
|
|
|
|
const agent = new PuppeteerAgent(originPage);
|
|
|
|
await agent.aiAction('Enter "happy birthday" and select Delete all');
|
|
|
|
});
|
|
|
|
|
2025-02-10 20:00:14 +08:00
|
|
|
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(
|
|
|
|
`
|
2025-02-13 15:53:53 +08:00
|
|
|
tasks:
|
|
|
|
- name: search weather
|
|
|
|
flow:
|
|
|
|
- ai: input 'weather today' in input box, click search button
|
|
|
|
- sleep: 3000
|
2025-02-10 20:00:14 +08:00
|
|
|
|
2025-02-13 15:53:53 +08:00
|
|
|
- name: query weather
|
|
|
|
flow:
|
|
|
|
- aiQuery: "the result shows the weather info, {description: string}"
|
|
|
|
name: weather
|
|
|
|
`,
|
2025-02-10 20:00:14 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
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(
|
|
|
|
`
|
2025-02-13 15:53:53 +08:00
|
|
|
tasks:
|
|
|
|
- name: search weather
|
|
|
|
flow:
|
|
|
|
- aiAssert: the result shows food delivery service
|
|
|
|
`,
|
2025-02-10 20:00:14 +08:00
|
|
|
);
|
|
|
|
} 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(
|
|
|
|
`
|
2025-02-13 15:53:53 +08:00
|
|
|
tasks:
|
|
|
|
- name: search weather
|
|
|
|
flow:
|
|
|
|
- ai: input 'weather today' in input box, click search button
|
|
|
|
- sleep: 3000
|
2025-02-10 20:00:14 +08:00
|
|
|
|
2025-02-13 15:53:53 +08:00
|
|
|
- name: query weather
|
|
|
|
flow:
|
|
|
|
- aiQuery: "the result shows the weather info, {description: string}"
|
|
|
|
name: weather
|
2025-02-10 20:00:14 +08:00
|
|
|
|
2025-02-13 15:53:53 +08:00
|
|
|
- name: error
|
|
|
|
continueOnError: true
|
|
|
|
flow:
|
|
|
|
- aiAssert: the result shows food delivery service
|
|
|
|
`,
|
2025-02-10 20:00:14 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.weather.description).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|