|
| 1 | +import { PuppeteerAgent } from '@/puppeteer'; |
| 2 | +import { sleep } from '@midscene/core/utils'; |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { launchPage } from './utils'; |
| 5 | + |
| 6 | +vi.setConfig({ |
| 7 | + testTimeout: 120 * 1000, |
| 8 | +}); |
| 9 | + |
| 10 | +describe('puppeteer integration', () => { |
| 11 | + let resetFn: () => Promise<void>; |
| 12 | + afterEach(async () => { |
| 13 | + if (resetFn) { |
| 14 | + await resetFn(); |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + it('Sauce Demo, agent with yaml script', async () => { |
| 19 | + const { originPage, reset } = await launchPage('https://www.bing.com/'); |
| 20 | + resetFn = reset; |
| 21 | + const agent = new PuppeteerAgent(originPage); |
| 22 | + await sleep(3000); |
| 23 | + const { result } = await agent.runYaml( |
| 24 | + ` |
| 25 | +tasks: |
| 26 | + - name: search weather |
| 27 | + flow: |
| 28 | + - ai: input 'weather today' in input box, click search button |
| 29 | + - sleep: 3000 |
| 30 | +
|
| 31 | + - name: query weather |
| 32 | + flow: |
| 33 | + - aiQuery: "the result shows the weather info, {description: string}" |
| 34 | + name: weather |
| 35 | +`, |
| 36 | + ); |
| 37 | + |
| 38 | + expect(result.weather.description).toBeDefined(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('assertion failed', async () => { |
| 42 | + const { originPage, reset } = await launchPage('https://www.bing.com/'); |
| 43 | + resetFn = reset; |
| 44 | + const agent = new PuppeteerAgent(originPage); |
| 45 | + let errorMsg = ''; |
| 46 | + try { |
| 47 | + await agent.runYaml( |
| 48 | + ` |
| 49 | + tasks: |
| 50 | + - name: search weather |
| 51 | + flow: |
| 52 | + - aiAssert: the result shows food delivery service |
| 53 | + `, |
| 54 | + ); |
| 55 | + } catch (e: any) { |
| 56 | + errorMsg = e.message; |
| 57 | + } |
| 58 | + |
| 59 | + const multiLineErrorMsg = errorMsg.split('\n'); |
| 60 | + expect(multiLineErrorMsg.length).toBeGreaterThan(2); |
| 61 | + }); |
| 62 | + |
| 63 | + it('allow error in flow', async () => { |
| 64 | + const { originPage, reset } = await launchPage('https://www.bing.com/'); |
| 65 | + resetFn = reset; |
| 66 | + const agent = new PuppeteerAgent(originPage); |
| 67 | + const { result } = await agent.runYaml( |
| 68 | + ` |
| 69 | +tasks: |
| 70 | + - name: search weather |
| 71 | + flow: |
| 72 | + - ai: input 'weather today' in input box, click search button |
| 73 | + - sleep: 3000 |
| 74 | +
|
| 75 | + - name: query weather |
| 76 | + flow: |
| 77 | + - aiQuery: "the result shows the weather info, {description: string}" |
| 78 | + name: weather |
| 79 | +
|
| 80 | + - name: error |
| 81 | + continueOnError: true |
| 82 | + flow: |
| 83 | + - aiAssert: the result shows food delivery service |
| 84 | + `, |
| 85 | + ); |
| 86 | + |
| 87 | + expect(result.weather.description).toBeDefined(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments