How to use divergedWorker method in backstopjs

Best JavaScript code snippet using backstopjs

ImageScrubber.js

Source:ImageScrubber.js Github

copy

Full Screen

1import React from 'react';2import styled from 'styled-components';3import TwentyTwenty from 'backstop-twentytwenty';4import { colors, fonts, shadows } from '../../styles';5const ScrubberViewBtn = styled.button`6 margin: 1em;7 padding: 10px 16px;8 height: 32px;9 background-color: ${props =>10 props.selected ? colors.secondaryText : colors.lightGray};11 color: ${props => (props.selected ? colors.lightGray : colors.secondaryText)};12 border-radius: 3px;13 text-transform: uppercase;14 font-family: ${fonts.latoRegular};15 text-align: center;16 font-size: 12px;17 border: none;18 box-shadow: ${props => (props.selected ? 'none' : shadows.shadow01)};19 transition: all 100ms ease-in-out;20 &:focus {21 outline: none;22 }23 &:hover {24 cursor: pointer;25 box-shadow: ${props => (!props.selected ? shadows.shadow02 : '')};26 }27 &.loadingDiverged {28 animation: blink normal 1200ms infinite ease-in-out;29 background-color: green;30 }31 @keyframes blink {32 0% {33 opacity: 1;34 }35 50% {36 opacity: 0.75;37 }38 100% {39 opacity: 1;40 }41 }42`;43const Wrapper = styled.div`44 cursor: ew-resize;45 padding-bottom: 20px;46 overflow: hidden;47 .testImage {48 opacity: 1;49 }50 .testImage,51 .refImage {52 max-width: 100%;53 }54`;55const WrapTitle = styled.div`56 display: flex;57 justify-content: center;58 padding-top: 10px;59 padding-bottom: 10px;60 position: sticky;61 top: 0;62 z-index: 5;63 background: white;64 border-bottom: 1px solid #e4e4e4;65`;66const SliderBar = styled.div`67 height: 100%;68 width: 5px;69 background: ${colors.red};70 transform: translate(-2.5px, 0);71`;72export default class ImageScrubber extends React.Component {73 constructor (props) {74 super(props);75 this.state = {76 isRefImageMissing: false,77 isLoading: false78 };79 this.handleRefImageLoadingError = this.handleRefImageLoadingError.bind(this);80 this.loadingDiverge = this.loadingDiverge.bind(this);81 }82 handleRefImageLoadingError () {83 this.setState({84 isRefImageMissing: true85 });86 }87 loadingDiverge (torf) {88 this.setState({89 isLoading: !!torf90 });91 }92 render () {93 const {94 scrubberModalMode,95 testImageType,96 position,97 refImage,98 testImage,99 diffImage,100 divergedImage,101 showScrubberTestImage,102 showScrubberRefImage,103 showScrubberDiffImage,104 showScrubberDivergedImage,105 showScrubber106 } = this.props;107 const scrubberTestImageSlug = this.props[testImageType];108 const hasDiff = diffImage && diffImage.length > 0;109 // only show the diverged option if the report comes from web server110 function showDivergedOption () {111 return /remote/.test(location.search);112 }113 // TODO: halp. i don't haz context.114 const that = this;115 function divergedWorker () {116 if (that.state.isLoading) {117 console.error('Diverged process is already running. Please hang on.');118 return;119 }120 if (divergedImage) {121 showScrubberDivergedImage(divergedImage);122 return;123 }124 showScrubberDivergedImage('');125 that.loadingDiverge(true);126 const refImg = document.images.isolatedRefImage;127 const testImg = document.images.isolatedTestImage;128 const h = refImg.height;129 const w = refImg.width;130 const worker = new Worker('divergedWorker.js');131 worker.addEventListener(132 'message',133 function (result) {134 const divergedImgData = result.data;135 let clampedImgData = getEmptyImgData(h, w);136 for (let i = divergedImgData.length - 1; i >= 0; i--) {137 clampedImgData.data[i] = divergedImgData[i];138 }139 const lcsDiffResult = imageToCanvasContext(null, h, w);140 lcsDiffResult.putImageData(clampedImgData, 0, 0);141 const divergedImageResult = lcsDiffResult.canvas.toDataURL(142 'image/png'143 );144 showScrubberDivergedImage(divergedImageResult);145 that.loadingDiverge(false);146 },147 false148 );149 worker.addEventListener('error', function (error) {150 showScrubberDivergedImage('');151 that.loadingDiverge(false);152 console.error(error);153 });154 worker.postMessage({155 divergedInput: [156 getImgDataDataFromContext(imageToCanvasContext(refImg)),157 getImgDataDataFromContext(imageToCanvasContext(testImg)),158 h,159 w160 ]161 });162 }163 const dontUseScrubberView = this.state.isRefImageMissing || !hasDiff;164 const showIsolatedRefImage = !hasDiff && scrubberModalMode === 'SHOW_SCRUBBER_REF_IMAGE';165 const showIsolatedTestImage = !hasDiff && scrubberModalMode === 'SHOW_SCRUBBER_TEST_IMAGE';166 return (167 <div>168 <WrapTitle>169 {hasDiff && (170 <div>171 <ScrubberViewBtn172 selected={scrubberModalMode === 'SHOW_SCRUBBER_REF_IMAGE'}173 onClick={showScrubberRefImage}174 >175 REFERENCE176 </ScrubberViewBtn>177 <ScrubberViewBtn178 selected={scrubberModalMode === 'SHOW_SCRUBBER_TEST_IMAGE'}179 onClick={showScrubberTestImage}180 >181 TEST182 </ScrubberViewBtn>183 <ScrubberViewBtn184 selected={scrubberModalMode === 'SHOW_SCRUBBER_DIFF_IMAGE'}185 onClick={showScrubberDiffImage}186 >187 DIFF188 </ScrubberViewBtn>189 <ScrubberViewBtn190 selected={scrubberModalMode === 'SCRUB'}191 onClick={showScrubber}192 >193 SCRUBBER194 </ScrubberViewBtn>195 <ScrubberViewBtn196 selected={scrubberModalMode === 'SHOW_SCRUBBER_DIVERGED_IMAGE'}197 onClick={divergedWorker}198 className={this.state.isLoading ? 'loadingDiverged' : ''}199 style={{200 display: showDivergedOption() ? '' : 'none'201 }}202 >203 {this.state.isLoading ? 'DIVERGING!' : 'DIVERGED'}204 </ScrubberViewBtn>205 </div>206 )}207 </WrapTitle>208 <Wrapper>209 <img210 id="isolatedRefImage"211 src={refImage}212 style={{213 margin: 'auto',214 display: showIsolatedRefImage ? 'block' : 'none'215 }}216 />217 <img218 id="isolatedTestImage"219 className="testImage"220 src={testImage}221 style={{222 margin: 'auto',223 display: showIsolatedTestImage ? 'block' : 'none'224 }}225 />226 <img227 className="diffImage"228 src={diffImage}229 style={{230 margin: 'auto',231 display: dontUseScrubberView ? 'block' : 'none'232 }}233 />234 <div235 style={{236 display: dontUseScrubberView ? 'none' : 'block'237 }}238 >239 <TwentyTwenty240 verticalAlign="top"241 minDistanceToBeginInteraction={0}242 maxAngleToBeginInteraction={Infinity}243 initialPosition={position}244 newPosition={position}245 >246 <img247 id="scrubberRefImage"248 className="refImage"249 src={refImage}250 onError={this.handleRefImageLoadingError}251 />252 <img253 id="scrubberTestImage"254 className="testImage"255 src={scrubberTestImageSlug}256 />257 <SliderBar className="slider" />258 </TwentyTwenty>259 </div>260 </Wrapper>261 </div>262 );263 }264}265/**266 * ========= DIVERGED HELPERS ========267 */268function getImgDataDataFromContext (context) {269 return context.getImageData(0, 0, context.canvas.width, context.canvas.height)270 .data;271}272function getEmptyImgData (h, w) {273 var o = imageToCanvasContext(null, h, w);274 return o.createImageData(w, h);275}276function imageToCanvasContext (_img, h, w) {277 let img = _img;278 if (!_img) {279 img = { height: h, width: w };280 }281 const canvas = document.createElement('canvas');282 canvas.width = img.width;283 canvas.height = img.height;284 const context = canvas.getContext('2d');285 if (_img) {286 context.drawImage(img, 0, 0);287 }288 return context;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1module.exports = async (page, scenario) => {2 await require('./loadCookies')(page, scenario);3 const clickSelectors = scenario.clickSelectors || [];4 const hoverSelectors = scenario.hoverSelectors || [];5 for (const clickSelector of clickSelectors) {6 await page.waitForSelector(clickSelector);7 await page.click(clickSelector);8 }9 for (const hoverSelector of hoverSelectors) {10 await page.waitForSelector(hoverSelector);11 await page.hover(hoverSelector);12 }13 if (postInteractionWait) {14 await page.waitFor(postInteractionWait);15 }16};17{18 {19 },20 {21 }22}

Full Screen

Using AI Code Generation

copy

Full Screen

1var backstop = require('backstopjs');2var backstopConfig = require('./backstop.json');3backstop('reference', { config: backstopConfig })4 .then(function() {5 backstop('test', { config: backstopConfig })6 .then(function() {7 console.log('All done!');8 })9 .catch(function(err) {10 console.log(err);11 });12 })13 .catch(function(err) {14 console.log(err);15 });

Full Screen

Using AI Code Generation

copy

Full Screen

1module.exports = async (page, scenario, vp) => {2 await require('./loadCookies')(page, scenario);3 await page.evaluateOnNewDocument(function () {4 Object.defineProperty(navigator, 'webdriver', {5 get: () => undefined,6 });7 });8 await page.evaluateOnNewDocument(function () {9 window.callPhantom = function () { };10 });11 await page.evaluateOnNewDocument(function () {12 window._phantom = function () { };13 });14 await page.evaluateOnNewDocument(function () {15 window.__nightmare = function () { };16 });17 await page.evaluateOnNewDocument(function () {18 window.navigator.chrome = {19 runtime: {},20 };21 });22 await page.evaluateOnNewDocument(function () {23 const originalQuery = window.navigator.permissions.query;24 return window.navigator.permissions.query = (parameters) => (25 Promise.resolve({ state: Notification.permission }) :26 originalQuery(parameters)27 );28 });29 await page.evaluateOnNewDocument(function () {30 window.navigator.geolocation.getCurrentPosition = function (cb) {31 setTimeout(() => {32 cb({33 coords: {34 },35 });36 }, 1000);37 };38 });39 await page.evaluateOnNewDocument(function () {40 Object.defineProperty(navigator, 'webdriver', {41 get: () => false,42 });43 });44 await page.evaluateOnNewDocument(function () {45 Object.defineProperty(navigator, 'plugins', {46 get: () => [1, 2, 3, 4, 5],47 });48 });49 await page.evaluateOnNewDocument(function () {50 Object.defineProperty(navigator, 'languages', {51 get: () => ['en-US', 'en'],52 });53 });54 await page.evaluateOnNewDocument(function () {

Full Screen

Using AI Code Generation

copy

Full Screen

1module.exports = async (engine, scenario, vp) => {2 console.log('SCENARIO > ' + scenario.label);3 await engine.waitFor(1000);4};5 {6 }

Full Screen

Using AI Code Generation

copy

Full Screen

1var backstop = require('backstopjs');2var path = require('path');3var config = require('./backstop.json');4var childProcess = require('child_process');5var fs = require('fs');6var os = require('os');

Full Screen

Using AI Code Generation

copy

Full Screen

1var backstop = require('backstopjs');2var fs = require('fs');3var config = require('./backstop.json');4var path = require('path');5var child_process = require('child_process');6var phantomjs = require('phantomjs-prebuilt');7var binPath = phantomjs.path;8var spawn = child_process.spawn;9var exec = child_process.exec;10var backstopjs = require('backstopjs');11var path = require('path');12var config = require('./backstop.json');13var child_process = require('child_process');14var phantomjs = require('phantomjs-prebuilt');15var binPath = phantomjs.path;16var spawn = child_process.spawn;17var exec = child_process.exec;18var fs = require('fs');19var backstop = require('backstopjs');20var path = require('path');21var config = require('./backstop.json');22var child_process = require('child_process');23var phantomjs = require('phantomjs-prebuilt');24var binPath = phantomjs.path;25var spawn = child_process.spawn;26var exec = child_process.exec;27var fs = require('fs');28var backstopjs = require('backstopjs');29var path = require('path');30var config = require('./backstop.json');31var child_process = require('child_process');32var phantomjs = require('phantomjs-prebuilt');33var binPath = phantomjs.path;34var spawn = child_process.spawn;35var exec = child_process.exec;36var fs = require('fs');37var backstopjs = require('backstopjs');38var path = require('path');39var config = require('./backstop.json');40var child_process = require('child_process');41var phantomjs = require('phantomjs-prebuilt');42var binPath = phantomjs.path;43var spawn = child_process.spawn;44var exec = child_process.exec;45var fs = require('fs');46var backstopjs = require('backstopjs');47var path = require('path');48var config = require('./backstop.json');49var child_process = require('child_process');50var phantomjs = require('phantomjs-prebuilt');51var binPath = phantomjs.path;52var spawn = child_process.spawn;53var exec = child_process.exec;54var fs = require('fs');55var backstopjs = require('backstopjs');56var path = require('path');57var config = require('./backstop.json');58var child_process = require('child_process');

Full Screen

Using AI Code Generation

copy

Full Screen

1const backstop = require('backstopjs');2const config = require('./backstop.json');3const path = require('path');4const testConfig = {5 {6 },7 {8 },9 {10 },11 {12 }13 {14 }15 "paths": {16 },17 "engineOptions": {18 },19}20const configPath = path.join(__dirname, 'backstop.json');21backstop('test', {22})23.then(() => {24 console.log('Test completed');25})26.catch((error) => {27 console.log(error);28});

Full Screen

Using AI Code Generation

copy

Full Screen

1var backstop = require('backstopjs');2var config = require('./backstop.json');3var divergedWorker = require('./node_modules/backstopjs/core/util/divergedWorker');4var diverged = divergedWorker(config);5if (diverged) {6 backstop('reference').then(function() {7 backstop('test');8 });9} else {10 backstop('test');11}12{13 {14 },15 {16 },17 {18 }19 {20 }21 "paths": {22 },23}24module.exports = function (casper, scenario) {25 casper.echo("onBefore.js: Running casperjs code before taking screenshot");26 casper.echo("onBefore.js: Hiding elements that are not needed for screenshot");

Full Screen

Using AI Code Generation

copy

Full Screen

1var engine = require('backstopjs');2var url = require('url');3var path = require('path');4var fs = require('fs');5var config = require('./backstop.json');6var getDivergedConfig = function (config, scenario, variant, url) {7 var clonedScenario = JSON.parse(JSON.stringify(scenario));8 clonedScenario.label = scenario.label + ' (' + variant + ')';9 clonedScenario.url = url;10 return engine.core.getConfig(clonedScenario, config);11};12var getScenarioDivergedConfig = function (config, scenario, variant, url) {13 var clonedConfig = JSON.parse(JSON.stringify(config));14 clonedConfig.scenarios = [getDivergedConfig(config, scenario, variant, url)];15 return clonedConfig;16};17var getDivergedConfigs = function (config, scenario, variant, url) {18 var divergedConfig = getScenarioDivergedConfig(config, scenario, variant, url);19 var divergedConfigs = [divergedConfig];20 if (scenario.viewports) {21 divergedConfigs = scenario.viewports.map(function (viewport) {22 var clonedConfig = JSON.parse(JSON.stringify(divergedConfig));23 clonedConfig.viewports = [viewport];24 return clonedConfig;25 });26 }27 return divergedConfigs;28};29var divergedWorker = function (config, scenario, variant, url, callback) {30 var divergedConfigs = getDivergedConfigs(config, scenario, variant, url);31 var divergedEngine = engine;32 divergedEngine.testRunner = require('backstopjs/core/command/testRunner');33 divergedEngine.testRunner(divergedConfigs, callback);34};35var scenario = config.scenarios[0];36var callback = function (err, data) {37 if (err) {38 console.log(err);39 } else {40 console.log(data);41 }42};43divergedWorker(config, scenario, 'google', url1, callback);44divergedWorker(config, scenario, 'yahoo', url2, callback);45{46 {47 },48 {

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run backstopjs automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful