zulip/frontend_tests/puppeteer_lib/common.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

const path = require('path');
const puppeteer = require('puppeteer');
class CommonUtils {
constructor() {
this.browser = null;
this.screenshot_id = 0;
}
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;
}
async screenshot(page, name = null) {
if (name === null) {
name = `${this.screenshot_id}`;
this.screenshot_id += 1;
}
const root_dir = path.resolve(__dirname, '../../');
const screenshot_path = path.join(root_dir, 'var/puppeteer', `${name}.png`);
await page.screenshot({
path: screenshot_path,
});
}
async run_test(test_function) {
// Pass a page instance to test so we can take
// a screenshot of it when the test fails.
const page = await this.get_page();
try {
await test_function(page);
} catch (e) {
console.log(e);
// Take a screenshot, and increment the screenshot_id.
await this.screenshot(page, `failure-${this.screenshot_id}`);
this.screenshot_id += 1;
await this.browser.close();
process.exit(1);
} finally {
this.browser.close();
}
}
}
const common = new CommonUtils();
module.exports = common;