-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.js
49 lines (36 loc) · 1.96 KB
/
demo.js
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
const { Device, AndroidDevice, findWebViewContexts, openWebView, pipeline } = require('.' /* '@onslip/automation' */);
const { createWriteStream } = require('fs');
const { Readable } = require('stream');
const PROXY_PORT = 8885;
async function main(prog, deviceId) {
const device = await Device.findDevice(deviceId);
if (!device) {
const devices = await Device.findDevices();
throw `Usage: ${prog} ${devices.map((d) => d.id).join('|') || '<device>'}`;
}
console.log(`Checking for debuggable web view on device ${device}`);
const [ webviewId ] = await device.findWebViews();
console.log(`Found web view ${webviewId}; opening proxy port ${PROXY_PORT}`);
const options = await device.bindWebView(webviewId, PROXY_PORT);
console.log(`Looking for contexts`);
const [ context ] = await findWebViewContexts(options);
console.info(`Opening CDP connection via port ${options.port} to context ${context.id} <${context.url}>`);
const page = await openWebView({...options, ctxId: context.id });
console.info(`Starting automation of ${webviewId} on device ${device}`);
page.setDebug(true);
try {
const lollipop = parseInt(await device.osVersion()) >= 5;
const logLines = device instanceof AndroidDevice
? await device.collectLogs({ clear: !lollipop, historic: !lollipop, filterspecs: ['*:D'] })
: await device.collectLogs();
const divs = await page.locator('div').count();
console.log(`There are ${divs} DIV elements in the web view!`);
await page.locator('body').screenshot({ path: `${deviceId}.png` });
console.log(`Saved a screenshot to ${deviceId}.png`);
await pipeline(Readable.from(await logLines()), createWriteStream(`${deviceId}.log`));
console.log(`Saved device logs to ${deviceId}.log`);
} finally {
await page.close();
}
}
main(...process.argv.slice(1)).catch((err) => (console.error(err), 70)).then(process.exit);