-
Notifications
You must be signed in to change notification settings - Fork 0
/
guiHelper.js
92 lines (78 loc) · 2.72 KB
/
guiHelper.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
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
/* This file helps to start and stop the browser and make screenshots of desired adapter
* Do not forget to include in package.json:
* "devDependencies": {
* "puppeteer": "^21.4.1",
* "colorette": "^1.2.1"
* }
**/
const puppeteer = require('puppeteer');
const { blue, cyan, red, yellow } = require('colorette');
const { existsSync, mkdirSync } = require('node:fs');
let gBrowser;
let gPage;
/* This function starts the browser and opens the desired adapter
* @param {string} adapterName - could be just an adapter name or adapter name with path, like 'device-manager/tab_m.html'
* @param {string} rootDir
* @param {boolean} headless
* @param {pathUrl} string
*/
async function startBrowser(adapterName, rootDir, headless, pathUrl) {
if (!rootDir.endsWith('/')) {
rootDir += '/';
}
const browser = await puppeteer.launch({
headless: headless === undefined ? false : headless,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const pages = await browser.pages();
const timeout = 5000;
pages[0].setDefaultTimeout(timeout);
await pages[0].setViewport({
width: 1920,
height: 1080,
deviceScaleFactor: 1,
});
gBrowser = browser;
gPage = pages[0];
// LOGGING
gPage
.on('console', message => {
const type = message.type().substr(0, 3).toUpperCase();
const colors = {
LOG: text => text,
ERR: red,
WAR: yellow,
INF: cyan,
};
const color = colors[type] || blue;
console.log(color(`[BROWSER] ${type} ${message.text()}`));
})
.on('pageerror', ({ message }) => console.log(red(`[BROWSER] ${message}`)));
pathUrl =
pathUrl ||
`/adapter/${adapterName.includes('/') ? (!adapterName.includes('?') ? `${adapterName}?` : adapterName) : `${adapterName}/index_m.html?`}&newReact=true&0&react=dark`;
if (!pathUrl.startsWith('/')) {
pathUrl = `/${pathUrl}`;
}
await gPage.goto(`http://127.0.0.1:8081${pathUrl}`, { waitUntil: 'domcontentloaded' });
// Create directory
!existsSync(`${rootDir}tmp/screenshots`) && mkdirSync(`${rootDir}tmp/screenshots`);
await gPage.screenshot({ path: `${rootDir}tmp/screenshots/00_starting.png` });
return { browser, page: pages[0] };
}
async function stopBrowser(browser) {
browser = browser || gBrowser;
await browser.close();
}
async function screenshot(rootDir, page, fileName) {
if (!rootDir.endsWith('/')) {
rootDir += '/';
}
page = page || gPage;
await page.screenshot({ path: `${rootDir}tmp/screenshots/${fileName}.png` });
}
module.exports = {
startBrowser,
stopBrowser,
screenshot,
};