woocommerce/packages/js/e2e-environment/utils/take-screenshot.js

39 lines
879 B
JavaScript
Raw Permalink Normal View History

2021-04-14 13:05:03 +00:00
const path = require( 'path' );
const mkdirp = require( 'mkdirp' );
const { resolveLocalE2ePath } = require( './test-config' );
2021-04-14 13:05:03 +00:00
/**
* Take a screenshot if browser context exists.
*
2021-04-14 13:05:03 +00:00
* @param message
* @return {Promise<{filePath: string, title: string}|{filePath: *, title: *}>}
2021-04-14 13:05:03 +00:00
*/
const takeScreenshotFor = async (message) => {
let now = new Date();
const title = `${message.replace( /\.$/, '' )}-${now.getDate()}${now.getHours()}${now.getMinutes()}${now.getSeconds()}`;
const savePath = resolveLocalE2ePath( 'screenshots' );
2021-04-14 13:05:03 +00:00
const filePath = path.join(
savePath,
`${ title }.png`.replace( /[^a-z0-9.-]+/gi, '-' )
);
mkdirp.sync( savePath );
try {
await page.screenshot( {
2021-04-14 13:05:03 +00:00
path: filePath,
fullPage: true,
} );
2021-04-14 13:05:03 +00:00
} catch ( error ) {
return {
title,
2021-04-14 13:05:03 +00:00
filePath: '',
};
}
return {
title,
filePath,
};
};
module.exports = takeScreenshotFor;