2024-12-19 10:44:08 +08:00
import path from 'node:path' ;
2024-08-20 07:41:08 +08:00
import { PuppeteerAgent } from '@/puppeteer' ;
2025-01-24 15:02:50 +08:00
import { sleep } from '@midscene/core/utils' ;
2024-08-20 07:41:08 +08:00
import { describe , expect , it , vi } from 'vitest' ;
import { launchPage } from './utils' ;
describe (
'puppeteer integration' ,
( ) = > {
it ( 'Sauce Demo by Swag Lab' , async ( ) = > {
2024-09-29 17:16:07 +08:00
const { originPage , reset } = await launchPage (
'https://www.saucedemo.com/' ,
) ;
2025-01-10 10:55:41 +08:00
const onTaskStartTip = vi . fn ( ) ;
2024-11-07 21:06:01 +08:00
const mid = new PuppeteerAgent ( originPage , {
cacheId : 'puppeteer(Sauce Demo by Swag Lab)' ,
2025-01-10 10:55:41 +08:00
onTaskStartTip ,
2024-11-07 21:06:01 +08:00
} ) ;
2024-08-20 07:41:08 +08:00
await mid . aiAction (
2024-12-08 20:12:17 +08:00
'type "standard_user" in user name input, type "secret_sauce" in password, click "Login", sleep 1s' ,
2024-08-20 07:41:08 +08:00
) ;
2025-01-10 10:55:41 +08:00
expect ( onTaskStartTip . mock . calls . length ) . toBeGreaterThan ( 1 ) ;
2024-12-16 15:04:21 +08:00
2024-08-21 14:43:35 +08:00
await expect ( async ( ) = > {
await mid . aiWaitFor ( 'there is a cookie prompt in the UI' , {
timeoutMs : 10 * 1000 ,
} ) ;
} ) . rejects . toThrowError ( ) ;
2024-08-20 07:41:08 +08:00
// find the items
const items = await mid . aiQuery (
'"{name: string, price: number, actionBtnName: string}[], return item name, price and the action button name on the lower right corner of each item (like "Remove")' ,
) ;
console . log ( 'item list' , items ) ;
expect ( items . length ) . toBeGreaterThanOrEqual ( 2 ) ;
await mid . aiAssert ( 'The price of "Sauce Labs Onesie" is 7.99' ) ;
2024-08-22 18:12:01 +08:00
await reset ( ) ;
2024-08-20 07:41:08 +08:00
} ) ;
it ( 'extract the Github service status' , async ( ) = > {
2024-09-29 17:16:07 +08:00
const { originPage , reset } = await launchPage (
'https://www.githubstatus.com/' ,
) ;
const mid = new PuppeteerAgent ( originPage ) ;
2024-08-20 07:41:08 +08:00
const result = await mid . aiQuery (
'this is a service status page. Extract all status data with this scheme: {[serviceName]: [statusText]}' ,
) ;
console . log ( 'Github service status' , result ) ;
expect ( async ( ) = > {
// there is no food delivery service on Github
await mid . aiAssert (
'there is a "food delivery" service on page and is in normal state' ,
) ;
} ) ;
2024-08-22 18:12:01 +08:00
await reset ( ) ;
2024-08-20 07:41:08 +08:00
} ) ;
2024-09-09 15:57:36 +08:00
it ( 'find widgets in antd' , async ( ) = > {
2024-09-29 17:16:07 +08:00
const { originPage , reset } = await launchPage (
2024-09-09 15:57:36 +08:00
'https://ant.design/components/form-cn/' ,
) ;
2024-09-29 17:16:07 +08:00
const mid = new PuppeteerAgent ( originPage ) ;
2024-09-09 15:57:36 +08:00
2024-11-20 16:00:34 +08:00
// await mid.aiAction('If pop-ups are displayed click seven days out alert');
2025-01-24 15:02:50 +08:00
await sleep ( 8000 ) ;
2024-12-08 20:12:17 +08:00
await mid . aiAction (
'Click the password input in the demo section on page, type "abc"' ,
) ;
2024-10-17 10:44:30 +08:00
2024-12-08 20:12:17 +08:00
await mid . aiAction (
'click the "icon" on the categories on the left, sleep 5s, in the newly loaded page, type "pause" in the icon search box(it shows "search icon here")' ,
) ;
2024-09-09 15:57:36 +08:00
2024-12-08 20:12:17 +08:00
const names = await mid . aiQuery (
'find all component names in the page, return in string[]' ,
2024-09-09 15:57:36 +08:00
) ;
2024-12-08 20:12:17 +08:00
expect ( names . length ) . toBeGreaterThan ( 5 ) ;
2024-09-09 18:07:22 +08:00
await reset ( ) ;
2024-09-09 15:57:36 +08:00
} ) ;
2024-10-12 12:09:25 +08:00
2024-12-19 10:44:08 +08:00
it ( 'search engine' , async ( ) = > {
2025-01-21 19:21:09 +08:00
const { originPage , reset } = await launchPage ( 'https://www.baidu.com/' ) ;
2024-10-12 12:09:25 +08:00
const mid = new PuppeteerAgent ( originPage ) ;
2025-01-21 19:21:09 +08:00
await mid . aiAction ( 'type "AI 101" in search box' ) ;
2024-10-12 12:09:25 +08:00
await mid . aiAction (
2025-01-21 19:21:09 +08:00
'type "Hello world" in search box, hit Enter, wait 2s, click the second result, wait 4s' ,
2024-10-12 12:09:25 +08:00
) ;
2025-01-21 19:21:09 +08:00
await mid . aiWaitFor ( 'there are some search results about "Hello world"' ) ;
2024-10-12 12:09:25 +08:00
await reset ( ) ;
} ) ;
2024-12-19 10:44:08 +08:00
it ( 'scroll' , async ( ) = > {
const htmlPath = path . join ( __dirname , 'scroll.html' ) ;
const { originPage , reset } = await launchPage ( ` file:// ${ htmlPath } ` ) ;
const mid = new PuppeteerAgent ( originPage ) ;
await mid . aiAction (
'find the "Vertical 2" element, scroll down 200px, find the "Horizontal 2" element, scroll right 100px' ,
) ;
await mid . aiAssert (
'the "Horizontal 2", "Horizontal 4" and "Vertical 5" elements are visible' ,
) ;
await reset ( ) ;
} ) ;
it . skip ( 'Playground' , async ( ) = > {
const { originPage , reset } = await launchPage ( 'https://www.baidu.com/' ) ;
const mid = new PuppeteerAgent ( originPage ) ;
// await mid.aiAction('Close the cookie prompt');
await mid . aiAction (
'Type "AI 101" in search box, hit Enter, wait 2s. If there is a cookie prompt, close it' ,
) ;
await reset ( ) ;
} ) ;
2024-08-20 07:41:08 +08:00
} ,
{
2024-08-28 19:31:59 +08:00
timeout : 180 * 1000 ,
2024-08-20 07:41:08 +08:00
} ,
) ;