@@ -33,13 +33,13 @@ class ResembleHelper extends Helper {
33
33
* Compare Images
34
34
*
35
35
* @param image
36
- * @param diffImage
37
36
* @param options
38
37
* @returns {Promise<resolve | reject> }
39
38
*/
40
- async _compareImages ( image , diffImage , options ) {
41
- const baseImage = this . baseFolder + image ;
42
- const actualImage = this . screenshotFolder + image ;
39
+ async _compareImages ( image , options ) {
40
+ const baseImage = this . _getBaseImagePath ( image , options ) ;
41
+ const actualImage = this . _getActualImagePath ( image ) ;
42
+ const diffImage = this . _getDiffImagePath ( image ) ;
43
43
44
44
// check whether the base and the screenshot images are present.
45
45
fs . access ( baseImage , fs . constants . F_OK | fs . constants . R_OK , ( err ) => {
@@ -83,11 +83,11 @@ class ResembleHelper extends Helper {
83
83
}
84
84
resolve ( data ) ;
85
85
if ( data . misMatchPercentage >= tolerance ) {
86
- if ( ! fs . existsSync ( getDirName ( this . diffFolder + diffImage ) ) ) {
87
- fs . mkdirSync ( getDirName ( this . diffFolder + diffImage ) ) ;
86
+ if ( ! fs . existsSync ( getDirName ( diffImage ) ) ) {
87
+ fs . mkdirSync ( getDirName ( diffImage ) ) ;
88
88
}
89
- fs . writeFileSync ( this . diffFolder + diffImage + '.png' , data . getBuffer ( ) ) ;
90
- const diffImagePath = path . join ( process . cwd ( ) , this . diffFolder + diffImage + '.png' ) ;
89
+ fs . writeFileSync ( diffImage , data . getBuffer ( ) ) ;
90
+ const diffImagePath = path . join ( process . cwd ( ) , diffImage ) ;
91
91
this . debug ( `Diff Image File Saved to: ${ diffImagePath } ` ) ;
92
92
}
93
93
}
@@ -102,8 +102,7 @@ class ResembleHelper extends Helper {
102
102
* @returns {Promise<*> }
103
103
*/
104
104
async _fetchMisMatchPercentage ( image , options ) {
105
- const diffImage = "Diff_" + image . split ( "." ) [ 0 ] ;
106
- const result = this . _compareImages ( image , diffImage , options ) ;
105
+ const result = this . _compareImages ( image , options ) ;
107
106
const data = await Promise . resolve ( result ) ;
108
107
return data . misMatchPercentage ;
109
108
}
@@ -144,40 +143,38 @@ class ResembleHelper extends Helper {
144
143
* This method attaches image attachments of the base, screenshot and diff to the allure reporter when the mismatch exceeds tolerance.
145
144
* @param baseImage
146
145
* @param misMatch
147
- * @param tolerance
146
+ * @param options
148
147
* @returns {Promise<void> }
149
148
*/
150
149
151
- async _addAttachment ( baseImage , misMatch , tolerance ) {
150
+ async _addAttachment ( baseImage , misMatch , options ) {
152
151
const allure = codeceptjs . container . plugins ( 'allure' ) ;
153
- const diffImage = "Diff_" + baseImage . split ( "." ) [ 0 ] + ".png" ;
154
152
155
- if ( allure !== undefined && misMatch >= tolerance ) {
156
- allure . addAttachment ( 'Base Image' , fs . readFileSync ( this . baseFolder + baseImage ) , 'image/png' ) ;
157
- allure . addAttachment ( 'Screenshot Image' , fs . readFileSync ( this . screenshotFolder + baseImage ) , 'image/png' ) ;
158
- allure . addAttachment ( 'Diff Image' , fs . readFileSync ( this . diffFolder + diffImage ) , 'image/png' ) ;
153
+ if ( allure !== undefined && misMatch >= options . tolerance ) {
154
+ allure . addAttachment ( 'Base Image' , fs . readFileSync ( this . _getBaseImagePath ( baseImage , options ) ) , 'image/png' ) ;
155
+ allure . addAttachment ( 'Screenshot Image' , fs . readFileSync ( this . _getActualImagePath ( baseImage ) ) , 'image/png' ) ;
156
+ allure . addAttachment ( 'Diff Image' , fs . readFileSync ( this . _getDiffImagePath ( baseImage ) ) , 'image/png' ) ;
159
157
}
160
158
}
161
159
162
160
/**
163
161
* This method attaches context, and images to Mochawesome reporter when the mismatch exceeds tolerance.
164
162
* @param baseImage
165
163
* @param misMatch
166
- * @param tolerance
164
+ * @param options
167
165
* @returns {Promise<void> }
168
166
*/
169
167
170
- async _addMochaContext ( baseImage , misMatch , tolerance ) {
168
+ async _addMochaContext ( baseImage , misMatch , options ) {
171
169
const mocha = this . helpers [ 'Mochawesome' ] ;
172
- const diffImage = "Diff_" + baseImage . split ( "." ) [ 0 ] + ".png" ;
173
170
174
- if ( mocha !== undefined && misMatch >= tolerance ) {
171
+ if ( mocha !== undefined && misMatch >= options . tolerance ) {
175
172
await mocha . addMochawesomeContext ( "Base Image" ) ;
176
- await mocha . addMochawesomeContext ( this . baseFolder + baseImage ) ;
173
+ await mocha . addMochawesomeContext ( this . _getBaseImagePath ( baseImage , options ) ) ;
177
174
await mocha . addMochawesomeContext ( "ScreenShot Image" ) ;
178
- await mocha . addMochawesomeContext ( this . screenshotFolder + baseImage ) ;
175
+ await mocha . addMochawesomeContext ( this . _getActualImagePath ( baseImage ) ) ;
179
176
await mocha . addMochawesomeContext ( "Diff Image" ) ;
180
- await mocha . addMochawesomeContext ( this . diffFolder + diffImage ) ;
177
+ await mocha . addMochawesomeContext ( this . _getDiffImagePath ( baseImage ) ) ;
181
178
}
182
179
}
183
180
@@ -189,18 +186,18 @@ class ResembleHelper extends Helper {
189
186
* @param region
190
187
* @param bucketName
191
188
* @param baseImage
192
- * @param ifBaseImage - tells if the prepareBaseImage is true or false. If false, then it won't upload the baseImage. However, this parameter is not considered if the config file has a prepareBaseImage set to true.
189
+ * @param options
193
190
* @returns {Promise<void> }
194
191
*/
195
192
196
- async _upload ( accessKeyId , secretAccessKey , region , bucketName , baseImage , ifBaseImage ) {
193
+ async _upload ( accessKeyId , secretAccessKey , region , bucketName , baseImage , options ) {
197
194
console . log ( "Starting Upload... " ) ;
198
195
const s3 = new AWS . S3 ( {
199
196
accessKeyId : accessKeyId ,
200
197
secretAccessKey : secretAccessKey ,
201
198
region : region
202
199
} ) ;
203
- fs . readFile ( this . screenshotFolder + baseImage , ( err , data ) => {
200
+ fs . readFile ( this . _getActualImagePath ( baseImage ) , ( err , data ) => {
204
201
if ( err ) throw err ;
205
202
let base64data = new Buffer ( data , 'binary' ) ;
206
203
const params = {
@@ -213,7 +210,7 @@ class ResembleHelper extends Helper {
213
210
console . log ( `Screenshot Image uploaded successfully at ${ uData . Location } ` ) ;
214
211
} ) ;
215
212
} ) ;
216
- fs . readFile ( this . diffFolder + "Diff_" + baseImage , ( err , data ) => {
213
+ fs . readFile ( this . _getDiffImagePath ( baseImage ) , ( err , data ) => {
217
214
if ( err ) console . log ( "Diff image not generated" ) ;
218
215
else {
219
216
let base64data = new Buffer ( data , 'binary' ) ;
@@ -228,14 +225,18 @@ class ResembleHelper extends Helper {
228
225
} ) ;
229
226
}
230
227
} ) ;
231
- if ( ifBaseImage ) {
232
- fs . readFile ( this . baseFolder + baseImage , ( err , data ) => {
228
+
229
+ // If prepareBaseImage is false, then it won't upload the baseImage. However, this parameter is not considered if the config file has a prepareBaseImage set to true.
230
+ if ( this . _getPrepareBaseImage ( options ) ) {
231
+ const baseImageName = this . _getBaseImageName ( baseImage , options ) ;
232
+
233
+ fs . readFile ( this . _getBaseImagePath ( baseImage , options ) , ( err , data ) => {
233
234
if ( err ) throw err ;
234
235
else {
235
236
let base64data = new Buffer ( data , 'binary' ) ;
236
237
const params = {
237
238
Bucket : bucketName ,
238
- Key : `base/${ baseImage } ` ,
239
+ Key : `base/${ baseImageName } ` ,
239
240
Body : base64data
240
241
} ;
241
242
s3 . upload ( params , ( uErr , uData ) => {
@@ -256,25 +257,27 @@ class ResembleHelper extends Helper {
256
257
* @param region
257
258
* @param bucketName
258
259
* @param baseImage
260
+ * @param options
259
261
* @returns {Promise<void> }
260
262
*/
261
263
262
- _download ( accessKeyId , secretAccessKey , region , bucketName , baseImage ) {
264
+ _download ( accessKeyId , secretAccessKey , region , bucketName , baseImage , options ) {
263
265
console . log ( "Starting Download..." ) ;
266
+ const baseImageName = this . _getBaseImageName ( baseImage , options ) ;
264
267
const s3 = new AWS . S3 ( {
265
268
accessKeyId : accessKeyId ,
266
269
secretAccessKey : secretAccessKey ,
267
270
region : region
268
271
} ) ;
269
272
const params = {
270
273
Bucket : bucketName ,
271
- Key : `base/${ baseImage } `
274
+ Key : `base/${ baseImageName } `
272
275
} ;
273
276
return new Promise ( ( resolve ) => {
274
277
s3 . getObject ( params , ( err , data ) => {
275
278
if ( err ) console . error ( err ) ;
276
- console . log ( this . baseFolder + baseImage ) ;
277
- fs . writeFileSync ( this . baseFolder + baseImage , data . Body ) ;
279
+ console . log ( this . _getBaseImagePath ( baseImage , options ) ) ;
280
+ fs . writeFileSync ( this . _getBaseImagePath ( baseImage , options ) , data . Body ) ;
278
281
resolve ( "File Downloaded Successfully" ) ;
279
282
} ) ;
280
283
} ) ;
@@ -308,24 +311,22 @@ class ResembleHelper extends Helper {
308
311
options . tolerance = 0 ;
309
312
}
310
313
311
- const prepareBaseImage = options . prepareBaseImage !== undefined
312
- ? options . prepareBaseImage
313
- : ( this . prepareBaseImage === true )
314
314
const awsC = this . config . aws ;
315
- if ( awsC !== undefined && prepareBaseImage === false ) {
316
- await this . _download ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage ) ;
317
- }
318
- if ( options . prepareBaseImage !== undefined && options . prepareBaseImage ) {
319
- await this . _prepareBaseImage ( baseImage ) ;
315
+
316
+ if ( this . _getPrepareBaseImage ( options ) ) {
317
+ await this . _prepareBaseImage ( baseImage , options ) ;
318
+ } else if ( awsC !== undefined ) {
319
+ await this . _download ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage , options ) ;
320
320
}
321
+
321
322
if ( selector ) {
322
323
options . boundingBox = await this . _getBoundingBox ( selector ) ;
323
324
}
324
325
const misMatch = await this . _fetchMisMatchPercentage ( baseImage , options ) ;
325
- this . _addAttachment ( baseImage , misMatch , options . tolerance ) ;
326
- this . _addMochaContext ( baseImage , misMatch , options . tolerance ) ;
326
+ this . _addAttachment ( baseImage , misMatch , options ) ;
327
+ this . _addMochaContext ( baseImage , misMatch , options ) ;
327
328
if ( awsC !== undefined ) {
328
- await this . _upload ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage , options . prepareBaseImage )
329
+ await this . _upload ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage , options )
329
330
}
330
331
331
332
this . debug ( "MisMatch Percentage Calculated is " + misMatch + " for baseline " + baseImage ) ;
@@ -339,14 +340,18 @@ class ResembleHelper extends Helper {
339
340
* Function to prepare Base Images from Screenshots
340
341
*
341
342
* @param screenShotImage Name of the screenshot Image (Screenshot Image Path is taken from Configuration)
343
+ * @param options
342
344
*/
343
- async _prepareBaseImage ( screenShotImage ) {
344
- await this . _createDir ( this . baseFolder + screenShotImage ) ;
345
+ async _prepareBaseImage ( screenShotImage , options ) {
346
+ const baseImage = this . _getBaseImagePath ( screenShotImage , options ) ;
347
+ const actualImage = this . _getActualImagePath ( screenShotImage ) ;
348
+
349
+ await this . _createDir ( baseImage ) ;
345
350
346
- fs . access ( this . screenshotFolder + screenShotImage , fs . constants . F_OK | fs . constants . W_OK , ( err ) => {
351
+ fs . access ( actualImage , fs . constants . F_OK | fs . constants . W_OK , ( err ) => {
347
352
if ( err ) {
348
353
throw new Error (
349
- `${ this . screenshotFolder + screenShotImage } ${ err . code === 'ENOENT' ? 'does not exist' : 'is read-only' } ` ) ;
354
+ `${ actualImage } ${ err . code === 'ENOENT' ? 'does not exist' : 'is read-only' } ` ) ;
350
355
}
351
356
} ) ;
352
357
@@ -357,7 +362,7 @@ class ResembleHelper extends Helper {
357
362
}
358
363
} ) ;
359
364
360
- fs . copyFileSync ( this . screenshotFolder + screenShotImage , this . baseFolder + screenShotImage ) ;
365
+ fs . copyFileSync ( actualImage , baseImage ) ;
361
366
}
362
367
363
368
/**
@@ -452,6 +457,60 @@ class ResembleHelper extends Helper {
452
457
453
458
throw new Error ( 'No matching helper found. Supported helpers: Playwright/WebDriver/Appium/Puppeteer/TestCafe' ) ;
454
459
}
460
+
461
+ /**
462
+ * Returns the final name of the expected base image, without a path
463
+ * @param image Name of the base-image, without path
464
+ * @param options Helper options
465
+ * @returns {string }
466
+ */
467
+ _getBaseImageName ( image , options ) {
468
+ return ( options . compareWithImage ? options . compareWithImage : image ) ;
469
+ }
470
+
471
+ /**
472
+ * Returns the path to the expected base image
473
+ * @param image Name of the base-image, without path
474
+ * @param options Helper options
475
+ * @returns {string }
476
+ */
477
+ _getBaseImagePath ( image , options ) {
478
+ return this . baseFolder + this . _getBaseImageName ( image , options ) ;
479
+ }
480
+
481
+ /**
482
+ * Returns the path to the actual screenshot image
483
+ * @param image Name of the image, without path
484
+ * @returns {string }
485
+ */
486
+ _getActualImagePath ( image ) {
487
+ return this . screenshotFolder + image ;
488
+ }
489
+
490
+ /**
491
+ * Returns the path to the image that displays differences between base and actual image.
492
+ * @param image Name of the image, without path
493
+ * @returns {string }
494
+ */
495
+ _getDiffImagePath ( image ) {
496
+ const diffImage = "Diff_" + image . split ( "." ) [ 0 ] + ".png" ;
497
+ return this . diffFolder + diffImage ;
498
+ }
499
+
500
+ /**
501
+ * Returns the final `prepareBaseImage` flag after evaluating options and config values
502
+ * @param options Helper options
503
+ * @returns {boolean }
504
+ */
505
+ _getPrepareBaseImage ( options ) {
506
+ if ( 'undefined' !== typeof options . prepareBaseImage ) {
507
+ // Cast to bool with `!!` for backwards compatibility
508
+ return ! ! options . prepareBaseImage ;
509
+ } else {
510
+ // Compare with `true` for backwards compatibility
511
+ return true === this . prepareBaseImage ;
512
+ }
513
+ }
455
514
}
456
515
457
516
module . exports = ResembleHelper ;
0 commit comments