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

Function captureScreenshot

core/util/runPlaywright.js:435–498  ·  view source on GitHub ↗
(page, browserContext, selector, selectorMap, config, selectors, viewport)

Source from the content-addressed store, hash-verified

433}
434
435async function captureScreenshot (page, browserContext, selector, selectorMap, config, selectors, viewport) {
436 let filePath;
437 const fullPage = (selector === NOCLIP_SELECTOR || selector === DOCUMENT_SELECTOR);
438 if (selector) {
439 filePath = selectorMap[selector].filePath;
440 ensureDirectoryPath(filePath);
441
442 try {
443 await page.screenshot({
444 path: filePath,
445 fullPage
446 });
447 } catch (e) {
448 console.log(chalk.red('Error capturing..'), e);
449 return fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);
450 }
451 } else {
452 // OTHER-SELECTOR screenshot
453 const selectorShot = async (s, path) => {
454 const el = await page.$(s);
455 if (el) {
456 const box = await el.boundingBox();
457 if (box) {
458 // Resize the viewport to screenshot elements outside of the viewport
459 if (config.useBoundingBoxViewportForSelectors !== false) {
460 const bodyHandle = await page.$('body');
461 const boundingBox = await bodyHandle.boundingBox();
462
463 await page.setViewportSize({
464 width: Math.max(viewport.width, Math.ceil(boundingBox.width)),
465 height: Math.max(viewport.height, Math.ceil(boundingBox.height))
466 });
467 }
468
469 const type = el;
470 const params = { captureBeyondViewport: false, path };
471
472 await type.screenshot(params);
473 } else {
474 console.log(chalk.yellow(`Element not visible for capturing: ${s}`));
475 return fs.copy(config.env.backstop + HIDDEN_SELECTOR_PATH, path);
476 }
477 } else {
478 console.log(chalk.magenta(`Element not found for capturing: ${s}`));
479 return fs.copy(config.env.backstop + SELECTOR_NOT_FOUND_PATH, path);
480 }
481 };
482
483 const selectorsShot = async () => {
484 for (let i = 0; i < selectors.length; i++) {
485 const selector = selectors[i];
486 filePath = selectorMap[selector].filePath;
487 ensureDirectoryPath(filePath);
488 try {
489 await selectorShot(selector, filePath);
490 } catch (e) {
491 console.log(chalk.red(`Error capturing Element ${selector}`), e);
492 return fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);

Callers 1

delegateSelectorsFunction · 0.70

Calls 2

ensureDirectoryPathFunction · 0.85
selectorsShotFunction · 0.70

Tested by

no test coverage detected