Best JavaScript code snippet using backstopjs
runPuppet.js
Source:runPuppet.js
...245 testPair.engineErrorMsg = error.message;246 compareConfig = {247 testPairs: [testPair]248 };249 await writeScenarioLogs(config, logFilePath, logger);250 await fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);251 }252 return Promise.resolve(compareConfig);253}254// TODO: Should be in engineTools255async function delegateSelectors (256 page,257 browser,258 scenario,259 viewport,260 variantOrScenarioLabelSafe,261 scenarioLabelSafe,262 config,263 selectors,264 selectorMap,265 logger266) {267 const compareConfig = { testPairs: [] };268 let captureDocument = false;269 let captureViewport = false;270 const captureList = [];271 const captureJobs = [];272 selectors.forEach(function (selector, selectorIndex) {273 const testPair = engineTools.generateTestPair(config, scenario, viewport, variantOrScenarioLabelSafe, scenarioLabelSafe, selectorIndex, selector);274 const filePath = config.isReference ? testPair.reference : testPair.test;275 const logFilePath = config.isReference ? testPair.referenceLog : testPair.testLog;276 if (!config.isReference) {277 compareConfig.testPairs.push(testPair);278 }279 selectorMap[selector].filePath = filePath;280 selectorMap[selector].logFilePath = logFilePath;281 if (selector === BODY_SELECTOR || selector === DOCUMENT_SELECTOR) {282 captureDocument = selector;283 } else if (selector === VIEWPORT_SELECTOR) {284 captureViewport = selector;285 } else {286 captureList.push(selector);287 }288 });289 if (captureDocument) {290 captureJobs.push(function () { return captureScreenshot(page, browser, captureDocument, selectorMap, config, [], viewport, logger); });291 }292 // TODO: push captureViewport into captureList (instead of calling captureScreenshot()) to improve perf.293 if (captureViewport) {294 captureJobs.push(function () { return captureScreenshot(page, browser, captureViewport, selectorMap, config, [], viewport, logger); });295 }296 if (captureList.length) {297 captureJobs.push(function () { return captureScreenshot(page, browser, null, selectorMap, config, captureList, viewport, logger); });298 }299 return new Promise(function (resolve, reject) {300 let job = null;301 const errors = [];302 const next = function () {303 if (captureJobs.length === 0) {304 if (errors.length === 0) {305 resolve();306 } else {307 reject(errors);308 }309 return;310 }311 job = captureJobs.shift();312 job().catch(function (e) {313 logger.log('reset', e);314 errors.push(e);315 }).then(function () {316 next();317 });318 };319 next();320 }).then(async () => {321 logger.log('green', 'x Close Browser');322 await browser.close();323 }).catch(async (err) => {324 logger.log('red', err);325 await browser.close();326 }).then(_ => compareConfig);327}328async function captureScreenshot (page, browser, selector, selectorMap, config, selectors, viewport, logger) {329 let filePath, logFilePath;330 const fullPage = (selector === NOCLIP_SELECTOR || selector === DOCUMENT_SELECTOR);331 if (selector) {332 filePath = selectorMap[selector].filePath;333 logFilePath = selectorMap[selector].logFilePath;334 ensureDirectoryPath(filePath); // logs in same dir335 try {336 await page.screenshot({337 path: filePath,338 fullPage: fullPage339 });340 await writeScenarioLogs(config, logFilePath, logger);341 } catch (e) {342 logger.log('red', 'Error capturing..', e);343 await writeScenarioLogs(config, logFilePath, logger);344 return fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);345 }346 } else {347 // OTHER-SELECTOR screenshot348 const selectorShot = async (s, path, logFilePath) => {349 const el = await page.$(s);350 if (el) {351 const box = await el.boundingBox();352 if (box) {353 // Resize the viewport to screenshot elements outside of the viewport354 if (config.useBoundingBoxViewportForSelectors !== false) {355 const bodyHandle = await page.$('body');356 const boundingBox = await bodyHandle.boundingBox();357 await page.setViewport({358 width: Math.max(viewport.width, Math.ceil(boundingBox.width)),359 height: Math.max(viewport.height, Math.ceil(boundingBox.height))360 });361 }362 const type = config.puppeteerOffscreenCaptureFix ? page : el;363 const params = config.puppeteerOffscreenCaptureFix364 ? {365 captureBeyondViewport: false,366 path: path,367 clip: box368 }369 : { captureBeyondViewport: false, path: path };370 await type.screenshot(params);371 await writeScenarioLogs(config, logFilePath, logger);372 } else {373 logger.log('yellow', `Element not visible for capturing: ${s}`);374 await writeScenarioLogs(config, logFilePath, logger);375 return fs.copy(config.env.backstop + HIDDEN_SELECTOR_PATH, path);376 }377 } else {378 logger.log('magenta', `Element not found for capturing: ${s}`);379 await writeScenarioLogs(config, logFilePath, logger);380 return fs.copy(config.env.backstop + SELECTOR_NOT_FOUND_PATH, path);381 }382 };383 const selectorsShot = async () => {384 for (let i = 0; i < selectors.length; i++) {385 const selector = selectors[i];386 filePath = selectorMap[selector].filePath;387 logFilePath = selectorMap[selector].logFilePath;388 ensureDirectoryPath(filePath);389 try {390 await selectorShot(selector, filePath, logFilePath);391 } catch (e) {392 logger.log('red', `Error capturing Element ${selector}`, e);393 await writeScenarioLogs(config, logFilePath, logger);394 return fs.copy(config.env.backstop + ERROR_SELECTOR_PATH, filePath);395 }396 }397 };398 await selectorsShot();399 }400}401// handle relative file name402function translateUrl (url, logger) {403 const RE = /^[./]/;404 if (RE.test(url)) {405 const fileUrl = 'file://' + path.join(process.cwd(), url);406 logger.log('reset', 'Relative filename detected -- translating to ' + fileUrl);407 return fileUrl;...
Using AI Code Generation
1var backstop = require('backstopjs');2backstop('reference', {config: './backstop.json'});3{4 {5 },6 {7 },8 {9 },10 {11 }12 {13 }14 "paths": {15 },16 "engineOptions": {17 },18}
Using AI Code Generation
1var backstopjs = require('backstopjs');2backstopjs.writeScenarioLogs({3 scenario: {4 },5 scenarioResult: {6 }7});
Using AI Code Generation
1var backstopjs = require('backstopjs');2var config = require('./backstop.json');3var scenario = {4};5backstopjs('test', {6 dockerCommandTemplate: 'docker run --rm -i --mount type=bind,source="{cwd}",target=/src backstopjs/backstopjs:{version} {backstopCommand} {args}'7})8.then(function (result) {9 console.log(result);10})11.catch(function (error) {12 console.error(error);13});14{15 {16 },17 {18 },19 {20 }21 {22 }
Using AI Code Generation
1var backstopjs = require('backstopjs');2var scenarioLogs = {3 {4 }5};6backstopjs('reference', { config: scenarioLogs })7 .then(function() {8 console.log('done');9 })10 .catch(function(err) {11 console.log('error', err);12 });
Using AI Code Generation
1module.exports = {2 {3 },4 {5 }6 {7 }8 paths: {9 },10};11var fs = require('fs');12var path = require('path');13var chromy = require('chromy');14var config = require('../config.json');15var BACKSTOP_DIR = path.join(__dirname, '..', '..');16var BACKSTOP_CONFIG = require(path.join(BACKSTOP_DIR, 'backstop.json'));17var CHROMY_CONFIG = BACKSTOP_CONFIG.chromyConfig;18module.exports = function (chromy, scenario) {19 var cookies = [];20 var cookiePath = path.join(BACKSTOP_DIR, scenario.cookiePath);21 if (fs.existsSync(cookiePath)) {22 cookies = JSON.parse(fs.readFileSync(cookiePath));23 }24 chromy.setCookie(cookies);25};26var fs = require('fs');27var path = require('path');28var chromy = require('chromy');29var config = require('../config.json');30var BACKSTOP_DIR = path.join(__dirname, '..', '..');31var BACKSTOP_CONFIG = require(path.join(BACKSTOP_DIR, 'backstop.json'));32var CHROMY_CONFIG = BACKSTOP_CONFIG.chromyConfig;33module.exports = function (chromy,
Using AI Code Generation
1const backstop = require('backstopjs');2backstop('test', {3}).then(() => {4 backstop('openReport');5});6docker run -v $(pwd):/src backstopjs/backstopjs test --docker --config=backstop.json --filter=scenarioLabel7docker run -v $(pwd):/src backstopjs/backstopjs openReport --docker8docker run -v $(pwd):/src backstopjs/backstopjs test --docker --config=backstop.json --filter=scenarioLabel9docker run -v $(pwd):/src backstopjs/backstopjs openReport --docker10docker run -v $(pwd):/src backstopjs/backstopjs test --docker --config=backstop.json --filter=scenarioLabel11docker run -v $(pwd):/src backstopjs/backstopjs openReport --docker
Using AI Code Generation
1var fs = require('fs');2var path = require('path');3var backstop = require('backstopjs');4var logPath = path.join(__dirname, 'logs');5var scenarioLogs = backstop('writeScenarioLogs', {6 config: path.join(__dirname, 'backstop.json'),7});8scenarioLogs.then(function (results) {9 console.log('Scenario logs written to: ' + logPath);10});11{12 {13 },14 {15 },16 {17 }18 {19 }20 "paths": {21 },22 "engineOptions": {23 },
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!