UNPKG

@percy/puppeteer

Version:

Pupppeteer client library for visual testing with Percy

43 lines (36 loc) 1.41 kB
const utils = require('@percy/sdk-utils'); // Collect client and environment information const sdkPkg = require('./package.json'); const puppeteerPkg = require('puppeteer/package.json'); const CLIENT_INFO = `${sdkPkg.name}/${sdkPkg.version}`; const ENV_INFO = `${puppeteerPkg.name}/${puppeteerPkg.version}`; // Take a DOM snapshot and post it to the snapshot endpoint async function percySnapshot(page, name, options) { if (!page) throw new Error('A Puppeteer `page` object is required.'); if (!name) throw new Error('The `name` argument is required.'); if (!(await utils.isPercyEnabled())) return; let log = utils.logger('puppeteer'); try { // Inject the DOM serialization script await page.evaluate(await utils.fetchPercyDOM()); // Serialize and capture the DOM /* istanbul ignore next: no instrumenting injected code */ let domSnapshot = await page.evaluate((options) => { /* eslint-disable-next-line no-undef */ return PercyDOM.serialize(options); }, options); // Post the DOM to the snapshot endpoint with snapshot options and other info await utils.postSnapshot({ ...options, environmentInfo: ENV_INFO, clientInfo: CLIENT_INFO, url: page.url(), domSnapshot, name }); } catch (err) { log.error(`Could not take DOM snapshot "${name}"`); log.error(err); } } module.exports = percySnapshot;