puppeteer: Add screenshot method to common module.

This will be useful for debugging purposes and we'll use it to take
a screenshot on failure.
This commit is contained in:
Priyank Patel 2020-05-15 17:27:29 +00:00 committed by Tim Abbott
parent 1f367ed537
commit b2f566f53e
1 changed files with 15 additions and 0 deletions

View File

@ -1,8 +1,10 @@
const path = require('path');
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
class CommonUtils { class CommonUtils {
constructor() { constructor() {
this.browser = null; this.browser = null;
this.screenshot_id = 0;
} }
async ensure_browser() { async ensure_browser() {
@ -29,6 +31,19 @@ class CommonUtils {
return page; 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) { async run_test(test_function) {
try { try {
await test_function(); await test_function();