-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathdemo.ts
More file actions
62 lines (50 loc) · 1.91 KB
/
demo.ts
File metadata and controls
62 lines (50 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import puppeteer from 'puppeteer';
import { PuppeteerAgent } from '@midscene/web/puppeteer';
import 'dotenv/config'; // read environment variables from .env file
const sleep = (ms: number | undefined) => new Promise((r) => setTimeout(r, ms));
Promise.resolve(
(async () => {
const browser = await puppeteer.launch({
headless: !!process.env.CI, // 'false' means we can see the browser window, the CI environment only supports headless mode.
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
await page.setViewport({
width: 1280,
height: 768,
deviceScaleFactor: 0
});
await page.goto('https://www.bing.com');
await sleep(5000);
// 👀 init Midscene agent
const agent = new PuppeteerAgent(page);
// 👀 type keywords, perform a search
await agent.aiAct('type "Headphones Price" in search box, hit Enter');
// 👀 wait for the loading
await agent.aiWaitFor('there is at least one headphone item on page');
// or you may use a plain sleep:
// await sleep(5000);
// 👀 understand the page content, find the items
const items = await agent.aiQuery<
Array<{ itemTitle: string; price: number }>
>(
'{itemTitle: string, price: Number}[], find item in list and corresponding price'
);
console.log('headphones in stock', items);
const isMoreThan1000 = await agent.aiBoolean(
'Is the price of the first headphones more than 1000?'
);
console.log('isMoreThan1000', isMoreThan1000);
const price = await agent.aiNumber(
'What is the price of the first headphone?'
);
console.log('price', price);
const name = await agent.aiString(
'What is the name of the first headphone?'
);
console.log('name', name);
// 👀 click on the first item
await agent.aiTap('the first item in the list');
await browser.close();
})()
);