|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// use any assertion library you like |
| 4 | +const resemble = require("resemblejs"); |
| 5 | +const fs = require('fs'); |
| 6 | +let assert = require('assert'); |
| 7 | +const mkdirp = require('mkdirp'); |
| 8 | +const getDirName = require('path').dirname; |
| 9 | +/** |
| 10 | + * Resemble.js helper class for CodeceptJS, this allows screen comparison |
| 11 | + * @author Puneet Kala |
| 12 | + */ |
| 13 | +class ResembleHelper extends Helper { |
| 14 | + |
| 15 | + constructor(config) { |
| 16 | + super(config); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * |
| 21 | + * @param image1 |
| 22 | + * @param image2 |
| 23 | + * @param diffImage |
| 24 | + * @param tolerance |
| 25 | + * @param options |
| 26 | + * @returns {Promise<any | never>} |
| 27 | + */ |
| 28 | + async _compareImages (image1, image2, diffImage, tolerance, options) { |
| 29 | + image1 = this.config.baseFolder + image1; |
| 30 | + image2 = this.config.screenshotFolder + image2; |
| 31 | + |
| 32 | + return new Promise((resolve, reject) => { |
| 33 | + if (options !== undefined) |
| 34 | + { |
| 35 | + resemble.outputSettings({ |
| 36 | + boundingBox: options.boundingBox |
| 37 | + }); |
| 38 | + } |
| 39 | + resemble.compare(image1, image2, options, (err, data) => { |
| 40 | + if (err) { |
| 41 | + reject(err); |
| 42 | + } else { |
| 43 | + resolve(data); |
| 44 | + if (data.misMatchPercentage >= tolerance) { |
| 45 | + mkdirp(getDirName(this.config.diffFolder + diffImage), function (err) { |
| 46 | + if (err) return cb(err); |
| 47 | + }); |
| 48 | + fs.writeFile(this.config.diffFolder + diffImage + '.png', data.getBuffer(), (err, data) => { |
| 49 | + if (err) { |
| 50 | + throw new Error(this.err); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + }); |
| 56 | + }).catch((error) => { |
| 57 | + console.log('caught', error.message); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * |
| 63 | + * @param image1 |
| 64 | + * @param image2 |
| 65 | + * @param diffImage |
| 66 | + * @param tolerance |
| 67 | + * @param options |
| 68 | + * @returns {Promise<*>} |
| 69 | + */ |
| 70 | + async _fetchMisMatchPercentage (image1, image2, diffImage, tolerance, options) { |
| 71 | + var result = this._compareImages(image1, image2, diffImage, tolerance, options); |
| 72 | + var data = await Promise.resolve(result); |
| 73 | + return data.misMatchPercentage; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Mis Match Percentage Verification |
| 78 | + * @param baseImage Name of the Base Image (Base Image path is taken from Configuration) |
| 79 | + * @param screenShotImage Name of the screenshot Image (Screenshot Image Path is taken from Configuration) |
| 80 | + * @param diffImageName Name of the Diff Image which will be saved after comparison (Diff Image path is taken from Configuration) |
| 81 | + * @param tolerance Tolerance Percentage, default value 10 |
| 82 | + * @param prepareBase True | False, depending on the requirement if the base images are missing |
| 83 | + * @param selector If set, passed selector will be used to fetch Bouding Box and compared on two images |
| 84 | + * @param options Resemble JS Options, read more here: https://github.com/rsmbl/Resemble.js |
| 85 | + * @returns {Promise<void>} |
| 86 | + */ |
| 87 | + async verifyMisMatchPercentage(baseImage, screenShotImage, diffImageName, tolerance = 10, prepareBase = false, selector, options){ |
| 88 | + if (prepareBase) |
| 89 | + { |
| 90 | + await this.prepareBaseImage(baseImage, screenShotImage); |
| 91 | + } |
| 92 | + |
| 93 | + if (selector !== undefined) |
| 94 | + { |
| 95 | + if (options !== undefined) |
| 96 | + { |
| 97 | + options.boundingBox = await this.getBoundingBox(selector); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + var options = {}; |
| 102 | + options.boundingBox = await this.getBoundingBox(selector); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + var misMatch = await this._fetchMisMatchPercentage(baseImage, screenShotImage, diffImageName, tolerance, options); |
| 107 | + console.log("MisMatch Percentage Calculated is " + misMatch); |
| 108 | + assert.ok(misMatch < tolerance, "MissMatch Percentage " + misMatch); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Function to prepare Base Images from Screenshots |
| 113 | + * |
| 114 | + * @param baseImage Name of the Base Image (Base Image path is taken from Configuration) |
| 115 | + * @param screenShotImage Name of the screenshot Image (Screenshot Image Path is taken from Configuration) |
| 116 | + */ |
| 117 | + async prepareBaseImage(baseImage, screenShotImage) { |
| 118 | + var configuration = this.config; |
| 119 | + |
| 120 | + await this._createDir(configuration.baseFolder + baseImage); |
| 121 | + |
| 122 | + fs.access(configuration.screenshotFolder + screenShotImage, fs.constants.F_OK | fs.constants.W_OK, (err) => { |
| 123 | + if (err) { |
| 124 | + console.error( |
| 125 | + `${configuration.screenshotFolder + screenShotImage} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + fs.access(configuration.baseFolder, fs.constants.F_OK | fs.constants.W_OK, (err) => { |
| 130 | + if (err) { |
| 131 | + console.error( |
| 132 | + `${configuration.baseFolder} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + fs.copyFileSync(configuration.screenshotFolder + screenShotImage, configuration.baseFolder + baseImage); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Function to create Directory |
| 141 | + * @param directory |
| 142 | + * @returns {Promise<void>} |
| 143 | + * @private |
| 144 | + */ |
| 145 | + async _createDir (directory) { |
| 146 | + mkdirp.sync(getDirName(directory)); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Function to fetch Bounding box for an element, fetched using selector |
| 151 | + * |
| 152 | + * @param selector CSS|XPath|ID selector |
| 153 | + * @returns {Promise<{boundingBox: {left: *, top: *, right: *, bottom: *}}>} |
| 154 | + */ |
| 155 | + async getBoundingBox(selector){ |
| 156 | + const browser = this._getBrowser(); |
| 157 | + |
| 158 | + var ele = await browser.element(selector) |
| 159 | + .then((res) => { |
| 160 | + return res; |
| 161 | + }) |
| 162 | + .catch((err) => { |
| 163 | + // Catch the error because webdriver.io throws if the element could not be found |
| 164 | + // Source: https://github.com/webdriverio/webdriverio/blob/master/lib/protocol/element.js |
| 165 | + return null; |
| 166 | + }); |
| 167 | + var location = await browser.getLocation(selector); |
| 168 | + var size = await browser.getElementSize(selector); |
| 169 | + var bottom = size.height + location.y; |
| 170 | + var right = size.width + location.x; |
| 171 | + var boundingBox = { |
| 172 | + left: location.x, |
| 173 | + top: location.y, |
| 174 | + right: right, |
| 175 | + bottom: bottom |
| 176 | + }; |
| 177 | + |
| 178 | + return boundingBox; |
| 179 | + } |
| 180 | + |
| 181 | + _getBrowser() { |
| 182 | + if (this.helpers['WebDriver']) { |
| 183 | + return this.helpers['WebDriver'].browser; |
| 184 | + } |
| 185 | + if (this.helpers['Appium']) { |
| 186 | + return this.helpers['Appium'].browser; |
| 187 | + } |
| 188 | + if (this.helpers['WebDriverIO']) { |
| 189 | + return this.helpers['WebDriverIO'].browser; |
| 190 | + } |
| 191 | + throw new Error('No matching helper found. Supported helpers: WebDriver/Appium/WebDriverIO'); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +module.exports = ResembleHelper; |
0 commit comments