UNPKG

protractor-screenshot-extension

Version:

An extension for Protractor that allows testing visual screenshots

217 lines (214 loc) 10.8 kB
import { __awaiter, __generator } from 'tslib'; import { existsSync, createWriteStream, readFileSync } from 'fs'; import { PNG } from 'pngjs'; import { browser } from 'protractor'; /** * @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 (!!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 = 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 = PNG.sync.read(testScreenshotBuffer); baselineScreenshotPng = PNG.sync.read(readFileSync(baselineImagePath)); diffPng = new 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"; createWriteStream(actualImagePath).write(testScreenshotBuffer); diffPng.pack().pipe(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(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 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 = 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*/, PNG.sync.write(imagePng_1)]; case 3: return [2 /*return*/, imageBuffer]; } }); }); }; return ProtractorScreenshotExtension; }()); export { ProtractorScreenshotExtension }; //# sourceMappingURL=protractor-screenshot-extension.js.map