2021-04-14 13:05:03 +00:00
|
|
|
const path = require( 'path' );
|
|
|
|
const mkdirp = require( 'mkdirp' );
|
2021-11-23 12:56:24 +00:00
|
|
|
const { resolveLocalE2ePath } = require( './test-config' );
|
2021-04-14 13:05:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a screenshot if browser context exists.
|
2021-10-28 19:32:31 +00:00
|
|
|
*
|
2021-04-14 13:05:03 +00:00
|
|
|
* @param message
|
2021-10-28 19:32:31 +00:00
|
|
|
* @return {Promise<{filePath: string, title: string}|{filePath: *, title: *}>}
|
2021-04-14 13:05:03 +00:00
|
|
|
*/
|
2022-01-04 18:34:30 +00:00
|
|
|
const takeScreenshotFor = async (message) => {
|
|
|
|
let now = new Date();
|
|
|
|
const title = `${message.replace( /\.$/, '' )}-${now.getDate()}${now.getHours()}${now.getMinutes()}${now.getSeconds()}`;
|
2021-11-23 12:56:24 +00:00
|
|
|
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 {
|
2021-10-28 19:32:31 +00:00
|
|
|
await page.screenshot( {
|
2021-04-14 13:05:03 +00:00
|
|
|
path: filePath,
|
|
|
|
fullPage: true,
|
2021-10-28 19:32:31 +00:00
|
|
|
} );
|
2021-04-14 13:05:03 +00:00
|
|
|
} catch ( error ) {
|
|
|
|
return {
|
2021-04-27 16:50:39 +00:00
|
|
|
title,
|
2021-04-14 13:05:03 +00:00
|
|
|
filePath: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
title,
|
|
|
|
filePath,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = takeScreenshotFor;
|