protractor-screenshot-extension
Version:
An extension for Protractor that allows testing visual screenshots
2 lines • 4.67 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("fs"),require("pngjs"),require("protractor")):"function"==typeof define&&define.amd?define("protractor-screenshot-extension",["exports","fs","pngjs","protractor"],t):t((e=e||self)["protractor-screenshot-extension"]={},e.fs,e.pngjs,e.protractor)}(this,function(e,t,r,n){"use strict";function i(e,t,r,n){return new(r||(r=Promise))(function(i,o){function c(e){try{s(n.next(e))}catch(t){o(t)}}function a(e){try{s(n["throw"](e))}catch(t){o(t)}}function s(e){e.done?i(e.value):new r(function(t){t(e.value)}).then(c,a)}s((n=n.apply(e,t||[])).next())})}function o(e,t){var r,n,i,o,c={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),"throw":a(1),"return":a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;c;)try{if(r=1,n&&(i=2&o[0]?n["return"]:o[0]?n["throw"]||((i=n["return"])&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return c.label++,{value:o[1],done:!1};case 5:c.label++,n=o[1],o=[0];continue;case 7:o=c.ops.pop(),c.trys.pop();continue;default:if(!(i=(i=c.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){c=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){c.label=o[1];break}if(6===o[0]&&c.label<i[1]){c.label=i[1],i=o;break}if(i&&c.label<i[2]){c.label=i[2],c.ops.push(o);break}i[2]&&c.ops.pop(),c.trys.pop();continue}o=t.call(e,c)}catch(a){o=[6,a],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}}var c=require("pixelmatch"),a=require("shelljs"),s="actual",u="diff",l="baseline",h=function(){function e(e){this._screenshotDirectory=e,"/"===this._screenshotDirectory.slice(-1)&&(this._screenshotDirectory=this._screenshotDirectory.slice(0,-1)),a.mkdir("-p",e+"/"+s),a.mkdir("-p",e+"/"+u),a.mkdir("-p",e+"/"+l)}return e.prototype.checkElementScreenshot=function(e,n,a){return i(this,void 0,void 0,function(){var i,h,f,d,p,g,y,b,w,m,v,k,x,S;return o(this,function(o){switch(o.label){case 0:return i=this._screenshotDirectory+"/"+l+"/"+n+".png",t.existsSync(i)?[3,3]:(console.log("Saving baseline image: "+i),d=(f=Buffer).from,[4,e.takeScreenshot()]);case 1:return h=d.apply(f,[o.sent(),"base64"]),[4,this._addBlackoutRectangles(h,a,e)];case 2:return h=o.sent(),(p=t.createWriteStream(i)).write(h),p.end(),[2,0];case 3:return b=(y=Buffer).from,[4,e.takeScreenshot()];case 4:return g=b.apply(y,[o.sent(),"base64"]),[4,this._addBlackoutRectangles(g,a,e)];case 5:return g=o.sent(),w=r.PNG.sync.read(g),m=r.PNG.sync.read(t.readFileSync(i)),v=new r.PNG({width:m.width,height:m.height}),(k=c(w.data,m.data,v.data,m.width,m.height,{threshold:a?a.threshold:undefined,includeAA:a?a.includeAA:undefined}))&&(x=this._screenshotDirectory+"/"+u+"/"+n+".png",S=this._screenshotDirectory+"/"+s+"/"+n+".png",t.createWriteStream(S).write(g),v.pack().pipe(t.createWriteStream(x))),[2,k]}})})},e.prototype.checkPageScreenshot=function(e,t){return this.checkElementScreenshot(n.browser,e,t)},e.prototype._blackoutRectangle=function(e,t,n,i,o){for(var c=new r.PNG({width:i,height:o}),a=0;a<c.height;a++)for(var s=0;s<c.width;s++){var u=4*(c.width*a+s);c.data[u]=0,c.data[u+1]=0,c.data[u+2]=0,c.data[u+3]=255}c.bitblt(e,0,0,i,o,t,n)},e.prototype._addBlackoutRectangles=function(e,t,n){return i(this,void 0,void 0,function(){var c,a=this;return o(this,function(s){switch(s.label){case 0:return t&&(t.ignoreRectangles||t.ignoreElements)?(c=r.PNG.sync.read(e),t.ignoreRectangles&&t.ignoreRectangles.length&&t.ignoreRectangles.forEach(function(e){a._blackoutRectangle(c,e.x,e.y,e.w,e.h)}),t.ignoreElements&&t.ignoreElements.length?[4,Promise.all(t.ignoreElements.map(function(e){return i(a,void 0,void 0,function(){var t,r,i,a,s,u;return o(this,function(o){switch(o.label){case 0:return t={x:0,y:0},n.getLocation?[4,n.getLocation()]:[3,2];case 1:t=o.sent(),o.label=2;case 2:return[4,e.getLocation()];case 3:return r=o.sent(),[4,e.getSize()];case 4:return i=o.sent(),n.driver?[4,n.driver.manage().window().getSize()]:[3,6];case 5:return s=o.sent(),[3,8];case 6:return[4,n.getSize()];case 7:s=o.sent(),o.label=8;case 8:return a=s,u=c.width/a.width,this._blackoutRectangle(c,(r.x-t.x)*u,(r.y-t.y)*u,i.width*u,i.height*u),[2]}})})}))]:[3,2]):[3,3];case 1:s.sent(),s.label=2;case 2:return[2,r.PNG.sync.write(c)];case 3:return[2,e]}})})},e}();e.ProtractorScreenshotExtension=h,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=protractor-screenshot-extension.umd.min.js.map