-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_robot_browser.js
More file actions
executable file
·108 lines (96 loc) · 3.4 KB
/
Copy pathstart_robot_browser.js
File metadata and controls
executable file
·108 lines (96 loc) · 3.4 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env node
const { firefox } = require("playwright");
const chalk = require("chalk");
const logId = "start_robot_browser.js";
// You may want to change this to test that the
// robot is using certificates that are valid for its real hostname
let robotHostname = "localhost"; // or NGROK_URL
if (process.argv.length > 2) {
robotHostname = process.argv[2];
}
const listenConsole = async (page) => {
// make args accessible
const describe = (jsHandle) => {
return jsHandle.evaluate((obj) => {
// serialize |obj| however you want
return obj;
// return `OBJ: ${typeof obj}, ${obj}`;
}, jsHandle);
};
const colors = {
LOG: chalk.grey, // (text: any) => text,
ERR: chalk.red,
WAR: chalk.yellow,
INF: chalk.cyan,
};
// listen to browser console
page.on("console", async (msg) => {
const args = await Promise.all(msg.args().map((arg) => describe(arg)));
// make ability to paint different console[types]
const type = msg.type().substr(0, 3).toUpperCase();
const color = colors[type] || chalk.blue;
let text = "";
let objs = [];
for (let i = 0; i < args.length; ++i) {
if (typeof args[i] !== "object") {
text += `${args[i]} `;
} else {
objs.push(args[i]);
}
}
console.log(color(`CONSOLE.${type}: ${text} `));
for (let i = 0; i < objs.length; ++i) {
console.log(objs[i]);
}
});
};
(async () => {
const navigation_timeout_ms = 30000; //30 seconds (default is 30 seconds)
const min_idle_time = 1000;
var try_again = true;
var num_tries = 0;
var max_tries = 50; // -1 means try forever
///////////////////////////////////////////////
// sleep code from
// https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
///////////////////////////////////////////////
const browser = await firefox.launch({
headless: true, // default is true
handleSIGINT: false,
defaultViewport: null,
firefoxUserPrefs: {
"permissions.default.microphone": 1, // Give permission to access the robot's microphone
},
});
const context = await browser.newContext({ ignoreHTTPSErrors: true }); // avoid ERR_CERT_COMMON_NAME_INVALID
const page = await context.newPage();
await listenConsole(page);
while (try_again) {
try {
await page.goto(`https://${robotHostname}/robot`);
console.log(logId + ": finished loading");
try_again = false;
} catch (e) {
console.log(logId + ": trying again");
console.log(e);
await sleep(200);
try_again = true;
}
}
console.log(logId + ": start script complete");
async function closeRobotBrowser() {
await new Promise((resolve) => {
page.goto(`https://${robotHostname}/whatever`); // page.close() doesn't work to trigger window.onbeforeunload
setTimeout(resolve, 500);
});
}
process.on("SIGTERM", () => {
closeRobotBrowser().finally(() => process.exit(0));
});
process.on("SIGINT", () => {
closeRobotBrowser().finally(() => process.exit(0));
});
})();