MCPcopy Create free account
hub / github.com/garris/BackstopJS / captureScreenshot

Function captureScreenshot

core/util/runPuppet.js:386–463  ·  view source on GitHub ↗
(page, browser, selector, selectorMap, config, selectors, viewport, logger)

Source from the content-addressed store, hash-verified

384}
385
386async function captureScreenshot (page, browser, selector, selectorMap, config, selectors, viewport, logger) {
387 let filePath, logFilePath;
388 const fullPage = (selector === NOCLIP_SELECTOR || selector === DOCUMENT_SELECTOR);
389 if (selector) {
390 filePath = selectorMap[selector].filePath;
391 logFilePath = selectorMap[selector].logFilePath;
392 ensureDirectoryPath(filePath); // logs in same dir
393
394 try {
395 await page.screenshot({
396 path: filePath,
397 fullPage
398 });
399 await writeScenarioLogs(config, logFilePath, logger);
400 } catch (e) {
401 logger.log('red', 'Error capturing..', e);
402 await writeScenarioLogs(config, logFilePath, logger);
403 return fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);
404 }
405 } else {
406 // OTHER-SELECTOR screenshot
407 const selectorShot = async (s, path, logFilePath) => {
408 const el = await page.$(s);
409 if (el) {
410 const box = await el.boundingBox();
411 if (box) {
412 // Resize the viewport to screenshot elements outside of the viewport
413 if (config.useBoundingBoxViewportForSelectors !== false) {
414 const bodyHandle = await page.$('body');
415 const boundingBox = await bodyHandle.boundingBox();
416
417 await page.setViewport({
418 width: Math.max(viewport.width, Math.ceil(boundingBox.width)),
419 height: Math.max(viewport.height, Math.ceil(boundingBox.height))
420 });
421 }
422
423 const type = config.puppeteerOffscreenCaptureFix ? page : el;
424 const params = config.puppeteerOffscreenCaptureFix
425 ? {
426 captureBeyondViewport: false,
427 path,
428 clip: box
429 }
430 : { captureBeyondViewport: false, path };
431
432 await type.screenshot(params);
433 await writeScenarioLogs(config, logFilePath, logger);
434 } else {
435 logger.log('yellow', `Element not visible for capturing: ${s}`);
436 await writeScenarioLogs(config, logFilePath, logger);
437 return fs.copy(config.env.backstop + HIDDEN_SELECTOR_PATH, path);
438 }
439 } else {
440 logger.log('magenta', `Element not found for capturing: ${s}`);
441 await writeScenarioLogs(config, logFilePath, logger);
442 return fs.copy(config.env.backstop + SELECTOR_NOT_FOUND_PATH, path);
443 }

Callers 1

delegateSelectorsFunction · 0.70

Calls 3

ensureDirectoryPathFunction · 0.85
writeScenarioLogsFunction · 0.85
selectorsShotFunction · 0.70

Tested by

no test coverage detected