1- import { AndroidAgent , AndroidDevice , getConnectedDevices } from '@midscene/android' ;
2- import "dotenv/config" ; // read environment variables from .env file
3-
4- const sleep = ( ms ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
1+ import {
2+ AndroidAgent ,
3+ AndroidDevice ,
4+ getConnectedDevices ,
5+ } from '@midscene/android' ;
6+ import 'dotenv/config' ; // read environment variables from .env file
7+
8+ const sleep = ( ms : number | undefined ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
59Promise . resolve (
610 ( async ( ) => {
711 const devices = await getConnectedDevices ( ) ;
812 const page = new AndroidDevice ( devices [ 0 ] . udid ) ;
913
1014 // 👀 init Midscene agent
11- const agent = new AndroidAgent ( page , {
15+ const agent = new AndroidAgent ( page , {
1216 aiActContext :
1317 'If any location, permission, user agreement, etc. popup, click agree. If login page pops up, close it.' ,
1418 } ) ;
@@ -18,34 +22,40 @@ Promise.resolve(
1822 await sleep ( 5000 ) ;
1923
2024 // 👀 type keywords, perform a search
21- await agent . aiAct (
22- 'type "Headphones" in search box, click search button'
23- ) ;
25+ await agent . aiAct ( 'type "Headphones" in search box, click search button' ) ;
2426
2527 // 👀 wait for the loading
26- await agent . aiWaitFor ( " there is at least one headphone item on page" ) ;
28+ await agent . aiWaitFor ( ' there is at least one headphone item on page' ) ;
2729 // or you may use a plain sleep:
2830 // await sleep(5000);
2931
3032 // 👀 understand the page content, find the items
3133 const items = await agent . aiQuery (
32- " {itemTitle: string, price: Number}[], find item in list and corresponding price"
34+ ' {itemTitle: string, price: Number}[], find item in list and corresponding price'
3335 ) ;
34- console . log ( " headphones in stock" , items ) ;
36+ console . log ( ' headphones in stock' , items ) ;
3537
36- const isMoreThan1000 = await agent . aiBoolean ( "Is the price of the headphones more than 1000?" ) ;
37- console . log ( "isMoreThan1000" , isMoreThan1000 ) ;
38+ const isMoreThan1000 = await agent . aiBoolean (
39+ 'Is the price of the headphones more than 1000?'
40+ ) ;
41+ console . log ( 'isMoreThan1000' , isMoreThan1000 ) ;
3842
39- const price = await agent . aiNumber ( "What is the price of the first headphone?" ) ;
40- console . log ( "price" , price ) ;
43+ const price = await agent . aiNumber (
44+ 'What is the price of the first headphone?'
45+ ) ;
46+ console . log ( 'price' , price ) ;
4147
42- const name = await agent . aiString ( "What is the name of the first headphone?" ) ;
43- console . log ( "name" , name ) ;
48+ const name = await agent . aiString (
49+ 'What is the name of the first headphone?'
50+ ) ;
51+ console . log ( 'name' , name ) ;
4452
45- const location = await agent . aiLocate ( "What is the location of the first headphone?" ) ;
46- console . log ( "location" , location ) ;
53+ const location = await agent . aiLocate (
54+ 'What is the location of the first headphone?'
55+ ) ;
56+ console . log ( 'location' , location ) ;
4757
4858 // 👀 assert by AI
49- await agent . aiAssert ( " There is a category filter on the left" ) ;
59+ await agent . aiAssert ( ' There is a category filter on the left' ) ;
5060 } ) ( )
5161) ;
0 commit comments