forked from codeceptjs/codeceptjs-resemblehelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
429 lines (374 loc) · 13.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
const resemble = require("resemblejs");
const fs = require('fs');
const assert = require('assert');
const mkdirp = require('mkdirp');
const getDirName = require('path').dirname;
const AWS = require('aws-sdk');
const path = require('path');
const sizeOf = require('image-size');
/**
* Resemble.js helper class for CodeceptJS, this allows screen comparison
* @author Puneet Kala
*/
class ResembleHelper extends Helper {
constructor(config) {
super(config);
this.baseFolder = this.resolvePath(config.baseFolder);
this.diffFolder = this.resolvePath(config.diffFolder);
this.screenshotFolder = global.output_dir + "/";
}
resolvePath(folderPath) {
if (!path.isAbsolute(folderPath)) {
return path.resolve(global.codecept_dir, folderPath) + "/";
}
return folderPath;
}
/**
* Compare Images
*
* @param image
* @param diffImage
* @param options
* @returns {Promise<resolve | reject>}
*/
async _compareImages(image, diffImage, options) {
const baseImage = this.baseFolder + image;
const actualImage = this.screenshotFolder + image;
// check whether the base and the screenshot images are present.
fs.access(baseImage, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
throw new Error(
`${baseImage} ${err.code === 'ENOENT' ? 'base image does not exist' : 'is read-only'}`);
}
});
fs.access(actualImage, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
throw new Error(
`${actualImage} ${err.code === 'ENOENT' ? 'screenshot image does not exist' : 'is read-only'}`);
}
});
return new Promise((resolve, reject) => {
resemble.outputSettings({
boundingBox: options.boundingBox,
ignoredBox: options.ignoredBox
});
this.debug("Tolerance Level Provided " + options.tolerance);
const tolerance = options.tolerance;
resemble.compare(baseImage, actualImage, options, (err, data) => {
if (err) {
reject(err);
} else {
if (!data.isSameDimensions) {
let dimensions1 = sizeOf(baseImage);
let dimensions2 = sizeOf(actualImage);
reject(new Error("The base image is of " + dimensions1.height + " X " + dimensions1.width + " and actual image is of " + dimensions2.height + " X " + dimensions2.width + ". Please use images of same dimensions so as to avoid any unexpected results."));
}
resolve(data);
if (data.misMatchPercentage >= tolerance) {
mkdirp(getDirName(this.diffFolder + diffImage), function (error) {
if (error) return cb(error);
});
fs.writeFileSync(this.diffFolder + diffImage + '.png', data.getBuffer());
const diffImagePath = path.join(process.cwd(), this.diffFolder + diffImage + '.png');
this.debug("Diff Image File Saved to: " + diffImagePath);
}
}
});
});
}
/**
*
* @param image
* @param options
* @returns {Promise<*>}
*/
async _fetchMisMatchPercentage(image, options) {
const diffImage = "Diff_" + image.split(".")[0];
const result = this._compareImages(image, diffImage, options);
const data = await Promise.resolve(result);
return data.misMatchPercentage;
}
/**
* Take screenshot of individual element.
* @param selector selector of the element to be screenshotted
* @param name name of the image
* @returns {Promise<void>}
*/
async screenshotElement(selector, name) {
const helper = this._getHelper();
if (this.helpers['Puppeteer']) {
await helper.waitForVisible(selector);
const els = await helper._locate(selector);
if (!els.length) throw new Error(`Element ${selector} couldn't be located`);
const el = els[0];
await el.screenshot({
path: global.output_dir + "/" + name + '.png'
});
} else if (this.helpers['WebDriver']) {
await helper.waitForVisible(selector);
const els = await helper._locate(selector);
if (!els.length) throw new Error(`Element ${selector} couldn't be located`);
const el = els[0];
await el.saveScreenshot(this.screenshotFolder + name + '.png');
} else throw new Error("Method only works with Puppeteer and WebDriver helpers.");
}
/**
* This method attaches image attachments of the base, screenshot and diff to the allure reporter when the mismatch exceeds tolerance.
* @param baseImage
* @param misMatch
* @param tolerance
* @returns {Promise<void>}
*/
async _addAttachment(baseImage, misMatch, tolerance) {
const allure = codeceptjs.container.plugins('allure');
const diffImage = "Diff_" + baseImage.split(".")[0] + ".png";
if (allure !== undefined && misMatch >= tolerance) {
allure.addAttachment('Base Image', fs.readFileSync(this.baseFolder + baseImage), 'image/png');
allure.addAttachment('Screenshot Image', fs.readFileSync(this.screenshotFolder + baseImage), 'image/png');
allure.addAttachment('Diff Image', fs.readFileSync(this.diffFolder + diffImage), 'image/png');
}
}
/**
* This method attaches context, and images to Mochawesome reporter when the mismatch exceeds tolerance.
* @param baseImage
* @param misMatch
* @param tolerance
* @returns {Promise<void>}
*/
async _addMochaContext(baseImage, misMatch, tolerance) {
const mocha = this.helpers['Mochawesome'];
const diffImage = "Diff_" + baseImage.split(".")[0] + ".png";
if (mocha !== undefined && misMatch >= tolerance) {
await mocha.addMochawesomeContext("Base Image");
await mocha.addMochawesomeContext(this.baseFolder + baseImage);
await mocha.addMochawesomeContext("ScreenShot Image");
await mocha.addMochawesomeContext(this.screenshotFolder + baseImage);
await mocha.addMochawesomeContext("Diff Image");
await mocha.addMochawesomeContext(this.diffFolder + diffImage);
}
}
/**
* This method uploads the diff and screenshot images into the bucket with diff image under bucketName/diff/diffImage and the screenshot image as
* bucketName/output/ssImage
* @param accessKeyId
* @param secretAccessKey
* @param region
* @param bucketName
* @param baseImage
* @param ifBaseImage - tells if the prepareBaseImage is true or false. If false, then it won't upload the baseImage.
* @returns {Promise<void>}
*/
async _upload(accessKeyId, secretAccessKey, region, bucketName, baseImage, ifBaseImage) {
console.log("Starting Upload... ");
const s3 = new AWS.S3({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: region
});
fs.readFile(this.screenshotFolder + baseImage, (err, data) => {
if (err) throw err;
let base64data = new Buffer(data, 'binary');
const params = {
Bucket: bucketName,
Key: `output/${baseImage}`,
Body: base64data
};
s3.upload(params, (uErr, uData) => {
if (uErr) throw uErr;
console.log(`Screenshot Image uploaded successfully at ${uData.Location}`);
});
});
fs.readFile(this.diffFolder + "Diff_" + baseImage, (err, data) => {
if (err) console.log("Diff image not generated");
else {
let base64data = new Buffer(data, 'binary');
const params = {
Bucket: bucketName,
Key: `diff/Diff_${baseImage}`,
Body: base64data
};
s3.upload(params, (uErr, uData) => {
if (uErr) throw uErr;
console.log(`Diff Image uploaded successfully at ${uData.Location}`)
});
}
});
if (ifBaseImage) {
fs.readFile(this.baseFolder + baseImage, (err, data) => {
if (err) throw err;
else {
let base64data = new Buffer(data, 'binary');
const params = {
Bucket: bucketName,
Key: `base/${baseImage}`,
Body: base64data
};
s3.upload(params, (uErr, uData) => {
if (uErr) throw uErr;
console.log(`Base Image uploaded at ${uData.Location}`)
});
}
});
} else {
console.log("Not Uploading base Image");
}
}
/**
* This method downloads base images from specified bucket into the base folder as mentioned in config file.
* @param accessKeyId
* @param secretAccessKey
* @param region
* @param bucketName
* @param baseImage
* @returns {Promise<void>}
*/
_download(accessKeyId, secretAccessKey, region, bucketName, baseImage) {
console.log("Starting Download...");
const s3 = new AWS.S3({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: region
});
const params = {
Bucket: bucketName,
Key: `base/${baseImage}`
};
return new Promise((resolve) => {
s3.getObject(params, (err, data) => {
if (err) console.error(err);
console.log(this.baseFolder + baseImage);
fs.writeFileSync(this.baseFolder + baseImage, data.Body);
resolve("File Downloaded Successfully");
});
});
}
/**
* Check Visual Difference for Base and Screenshot Image
* @param baseImage Name of the Base Image (Base Image path is taken from Configuration)
* @param options Options ex {prepareBaseImage: true, tolerance: 5} along with Resemble JS Options, read more here: https://github.com/rsmbl/Resemble.js
* @returns {Promise<void>}
*/
async seeVisualDiff(baseImage, options) {
await this._assertVisualDiff(undefined, baseImage, options);
}
/**
* See Visual Diff for an Element on a Page
*
* @param selector Selector which has to be compared expects these -> CSS|XPath|ID
* @param baseImage Base Image for comparison
* @param options Options ex {prepareBaseImage: true, tolerance: 5} along with Resemble JS Options, read more here: https://github.com/rsmbl/Resemble.js
* @returns {Promise<void>}
*/
async seeVisualDiffForElement(selector, baseImage, options) {
await this._assertVisualDiff(selector, baseImage, options);
}
async _assertVisualDiff(selector, baseImage, options) {
if (!options) {
options = {};
options.tolerance = 0;
}
const awsC = this.config.aws;
if (awsC !== undefined && options.prepareBaseImage === false) {
await this._download(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage);
}
if (options.prepareBaseImage !== undefined && options.prepareBaseImage) {
await this._prepareBaseImage(baseImage);
}
if (selector) {
options.boundingBox = await this._getBoundingBox(selector);
}
const misMatch = await this._fetchMisMatchPercentage(baseImage, options);
this._addAttachment(baseImage, misMatch, options.tolerance);
this._addMochaContext(baseImage, misMatch, options.tolerance);
if (awsC !== undefined) {
await this._upload(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, options.prepareBaseImage)
}
this.debug("MisMatch Percentage Calculated is " + misMatch);
assert(misMatch <= options.tolerance, "MissMatch Percentage " + misMatch);
}
/**
* Function to prepare Base Images from Screenshots
*
* @param screenShotImage Name of the screenshot Image (Screenshot Image Path is taken from Configuration)
*/
async _prepareBaseImage(screenShotImage) {
await this._createDir(this.baseFolder + screenShotImage);
fs.access(this.screenshotFolder + screenShotImage, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
throw new Error(
`${this.screenshotFolder + screenShotImage} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);
}
});
fs.access(this.baseFolder, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
throw new Error(
`${this.baseFolder} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);
}
});
fs.copyFileSync(this.screenshotFolder + screenShotImage, this.baseFolder + screenShotImage);
}
/**
* Function to create Directory
* @param directory
* @returns {Promise<void>}
* @private
*/
_createDir(directory) {
mkdirp.sync(getDirName(directory));
}
/**
* Function to fetch Bounding box for an element, fetched using selector
*
* @param selector CSS|XPath|ID selector
* @returns {Promise<{boundingBox: {left: *, top: *, right: *, bottom: *}}>}
*/
async _getBoundingBox(selector) {
const helper = this._getHelper();
await helper.waitForVisible(selector);
const els = await helper._locate(selector);
if (!els.length) throw new Error(`Element ${selector} couldn't be located`);
const el = els[0];
let location, size;
if (this.helpers['Puppeteer']) {
const box = await el.boundingBox();
size = location = box;
}
if (this.helpers['WebDriver'] || this.helpers['Appium']) {
location = await el.getLocation();
size = await el.getSize();
}
if (this.helpers['WebDriverIO']) {
location = await helper.browser.getLocation(selector);
size = await helper.browser.getElementSize(selector);
}
if (!size) {
throw new Error("Cannot get element size!");
}
const bottom = size.height + location.y;
const right = size.width + location.x;
const boundingBox = {
left: location.x,
top: location.y,
right: right,
bottom: bottom
};
this.debugSection('Area', JSON.stringify(boundingBox));
return boundingBox;
}
_getHelper() {
if (this.helpers['Puppeteer']) {
return this.helpers['Puppeteer'];
}
if (this.helpers['WebDriver']) {
return this.helpers['WebDriver'];
}
if (this.helpers['Appium']) {
return this.helpers['Appium'];
}
if (this.helpers['WebDriverIO']) {
return this.helpers['WebDriverIO'];
}
throw new Error('No matching helper found. Supported helpers: WebDriver/Appium/Puppeteer');
}
}
module.exports = ResembleHelper;