protractor-screenshot-extension
Version:
An extension for Protractor that allows testing visual screenshots
274 lines (264 loc) • 15.5 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('fs'), require('pngjs'), require('protractor')) :
typeof define === 'function' && define.amd ? define('protractor-screenshot-extension', ['exports', 'fs', 'pngjs', 'protractor'], factory) :
(global = global || self, factory(global['protractor-screenshot-extension'] = {}, global.fs, global.pngjs, global.protractor));
}(this, function (exports, fs, pngjs, protractor) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var pixelmatch = require('pixelmatch');
/** @type {?} */
var shell = require('shelljs');
/** @type {?} */
var actualDirectory = 'actual';
/** @type {?} */
var diffDirectory = 'diff';
/** @type {?} */
var baselineDirectory = 'baseline';
var ProtractorScreenshotExtension = /** @class */ (function () {
function ProtractorScreenshotExtension(screenshotDirectory) {
this._screenshotDirectory = screenshotDirectory;
if (this._screenshotDirectory.slice(-1) === '/') {
this._screenshotDirectory = this._screenshotDirectory.slice(0, -1);
}
// Create all 3 subfolders.
shell.mkdir('-p', screenshotDirectory + "/" + actualDirectory);
shell.mkdir('-p', screenshotDirectory + "/" + diffDirectory);
shell.mkdir('-p', screenshotDirectory + "/" + baselineDirectory);
}
/**
* @param {?} element
* @param {?} tag
* @param {?=} options
* @return {?}
*/
ProtractorScreenshotExtension.prototype.checkElementScreenshot = /**
* @param {?} element
* @param {?} tag
* @param {?=} options
* @return {?}
*/
function (element, tag, options) {
return __awaiter(this, void 0, void 0, function () {
var baselineImagePath, screenshot, _a, _b, stream, testScreenshotBuffer, _c, _d, testScreenshotPng, baselineScreenshotPng, diffPng, numberOfPixelsDifferent, diffImagePath, actualImagePath;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
baselineImagePath = this._screenshotDirectory + "/" + baselineDirectory + "/" + tag + ".png";
if (!!fs.existsSync(baselineImagePath)) return [3 /*break*/, 3];
console.log("Saving baseline image: " + baselineImagePath);
_b = (_a = Buffer).from;
return [4 /*yield*/, element.takeScreenshot()];
case 1:
screenshot = _b.apply(_a, [_e.sent(), 'base64']);
return [4 /*yield*/, this._addBlackoutRectangles(screenshot, options, element)];
case 2:
screenshot = _e.sent();
stream = fs.createWriteStream(baselineImagePath);
stream.write(screenshot);
stream.end();
return [2 /*return*/, 0];
case 3:
_d = (_c = Buffer).from;
return [4 /*yield*/, element.takeScreenshot()];
case 4:
testScreenshotBuffer = _d.apply(_c, [_e.sent(), 'base64']);
return [4 /*yield*/, this._addBlackoutRectangles(testScreenshotBuffer, options, element)];
case 5:
testScreenshotBuffer = _e.sent();
testScreenshotPng = pngjs.PNG.sync.read(testScreenshotBuffer);
baselineScreenshotPng = pngjs.PNG.sync.read(fs.readFileSync(baselineImagePath));
diffPng = new pngjs.PNG({ width: baselineScreenshotPng.width, height: baselineScreenshotPng.height });
numberOfPixelsDifferent = pixelmatch(testScreenshotPng.data, baselineScreenshotPng.data, diffPng.data, baselineScreenshotPng.width, baselineScreenshotPng.height, {
threshold: options ? options.threshold : undefined,
includeAA: options ? options.includeAA : undefined
});
if (!!numberOfPixelsDifferent) {
diffImagePath = this._screenshotDirectory + "/" + diffDirectory + "/" + tag + ".png";
actualImagePath = this._screenshotDirectory + "/" + actualDirectory + "/" + tag + ".png";
fs.createWriteStream(actualImagePath).write(testScreenshotBuffer);
diffPng.pack().pipe(fs.createWriteStream(diffImagePath));
}
return [2 /*return*/, numberOfPixelsDifferent];
}
});
});
};
/**
* @param {?} tag
* @param {?=} options
* @return {?}
*/
ProtractorScreenshotExtension.prototype.checkPageScreenshot = /**
* @param {?} tag
* @param {?=} options
* @return {?}
*/
function (tag, options) {
return this.checkElementScreenshot(protractor.browser, tag, options);
};
/**
* @private
* @param {?} image
* @param {?} x
* @param {?} y
* @param {?} w
* @param {?} h
* @return {?}
*/
ProtractorScreenshotExtension.prototype._blackoutRectangle = /**
* @private
* @param {?} image
* @param {?} x
* @param {?} y
* @param {?} w
* @param {?} h
* @return {?}
*/
function (image, x, y, w, h) {
// Create a black image that will be used for blacking out regions to be ignored.
/** @type {?} */
var blackPng = new pngjs.PNG({ width: w, height: h });
for (var i = 0; i < blackPng.height; i++) {
for (var j = 0; j < blackPng.width; j++) {
/** @type {?} */
var idx = (blackPng.width * i + j) * 4;
blackPng.data[idx] = 0; // red
blackPng.data[idx + 1] = 0; // blue
blackPng.data[idx + 2] = 0; // green
blackPng.data[idx + 3] = 255; // alpha
}
}
blackPng.bitblt(image, 0, 0, w, h, x, y);
};
/**
* @private
* @param {?} imageBuffer
* @param {?=} options
* @param {?=} parentElement
* @return {?}
*/
ProtractorScreenshotExtension.prototype._addBlackoutRectangles = /**
* @private
* @param {?} imageBuffer
* @param {?=} options
* @param {?=} parentElement
* @return {?}
*/
function (imageBuffer, options, parentElement) {
return __awaiter(this, void 0, void 0, function () {
var imagePng_1;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(options && (options.ignoreRectangles || options.ignoreElements))) return [3 /*break*/, 3];
imagePng_1 = pngjs.PNG.sync.read(imageBuffer);
if (options.ignoreRectangles && options.ignoreRectangles.length) {
options.ignoreRectangles.forEach(function (rect) {
_this._blackoutRectangle(imagePng_1, rect.x, rect.y, rect.w, rect.h);
});
}
if (!(options.ignoreElements && options.ignoreElements.length)) return [3 /*break*/, 2];
// Using `map` instead of `forEach` in order to await all async calls.
return [4 /*yield*/, Promise.all(options.ignoreElements.map(function (ignoreElement) { return __awaiter(_this, void 0, void 0, function () {
var parentLocation, ignoreLocation, ignoreSize, parentSize, _a, conversionFactor;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
parentLocation = { x: 0, y: 0 };
if (!!!parentElement.getLocation) return [3 /*break*/, 2];
return [4 /*yield*/, parentElement.getLocation()];
case 1:
// If an ElementFinder was passed in, call `getLocation()`, otherwise assume the whole browser was passed in.
parentLocation = _b.sent();
_b.label = 2;
case 2: return [4 /*yield*/, ignoreElement.getLocation()];
case 3:
ignoreLocation = _b.sent();
return [4 /*yield*/, ignoreElement.getSize()];
case 4:
ignoreSize = _b.sent();
// Adjust coordinates, as the screenshot can be twice the viewport size for high resolution displays.
if (!parentElement.driver) return [3 /*break*/, 6];
return [4 /*yield*/, parentElement.driver.manage().window().getSize()];
case 5:
_a = _b.sent();
return [3 /*break*/, 8];
case 6: return [4 /*yield*/, parentElement.getSize()];
case 7:
_a = _b.sent();
_b.label = 8;
case 8:
parentSize = _a;
conversionFactor = imagePng_1.width / parentSize.width;
this._blackoutRectangle(imagePng_1, (ignoreLocation.x - parentLocation.x) * conversionFactor, (ignoreLocation.y - parentLocation.y) * conversionFactor, ignoreSize.width * conversionFactor, ignoreSize.height * conversionFactor);
return [2 /*return*/];
}
});
}); }))];
case 1:
// Using `map` instead of `forEach` in order to await all async calls.
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/, pngjs.PNG.sync.write(imagePng_1)];
case 3: return [2 /*return*/, imageBuffer];
}
});
});
};
return ProtractorScreenshotExtension;
}());
exports.ProtractorScreenshotExtension = ProtractorScreenshotExtension;
Object.defineProperty(exports, '__esModule', { value: true });
}));
//# sourceMappingURL=protractor-screenshot-extension.umd.js.map