2020-05-15 16:02:16 +02:00
|
|
|
const puppeteer = require('puppeteer');
|
|
|
|
|
|
|
|
class CommonUtils {
|
|
|
|
constructor() {
|
|
|
|
this.browser = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async ensure_browser() {
|
|
|
|
if (this.browser === null) {
|
|
|
|
this.browser = await puppeteer.launch({
|
|
|
|
args: [
|
|
|
|
'--window-size=1400,1024',
|
|
|
|
'--no-sandbox', '--disable-setuid-sandbox',
|
|
|
|
],
|
|
|
|
defaultViewport: { width: 1280, height: 1024 },
|
|
|
|
headless: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async get_page(url = null) {
|
|
|
|
await this.ensure_browser();
|
|
|
|
|
|
|
|
const page = await this.browser.newPage();
|
|
|
|
if (url !== null) {
|
|
|
|
await page.goto(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
return page;
|
|
|
|
}
|
2020-05-15 17:38:25 +02:00
|
|
|
|
|
|
|
async run_test(test_function) {
|
|
|
|
try {
|
|
|
|
await test_function();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
await this.browser.close();
|
|
|
|
process.exit(1);
|
|
|
|
} finally {
|
|
|
|
this.browser.close();
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 16:02:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const common = new CommonUtils();
|
|
|
|
module.exports = common;
|