@@ -14,17 +14,31 @@ const sizeOf = require('image-size');
14
14
15
15
class ResembleHelper extends Helper {
16
16
17
+ constructor ( config ) {
18
+ super ( config ) ;
19
+ this . baseFolder = this . resolvePath ( config . baseFolder ) ;
20
+ this . diffFolder = this . resolvePath ( config . diffFolder ) ;
21
+ this . screenshotFolder = global . output_dir + "/" ;
22
+ }
23
+
24
+ resolvePath ( folderPath ) {
25
+ if ( ! path . isAbsolute ( folderPath ) ) {
26
+ return path . resolve ( global . codecept_dir , folderPath ) + "/" ; // custom helper
27
+ }
28
+ return folderPath ;
29
+ }
30
+
17
31
/**
18
32
* Compare Images
19
- *
33
+ *
20
34
* @param image
21
35
* @param diffImage
22
36
* @param options
23
37
* @returns {Promise<any | never> }
24
38
*/
25
39
async _compareImages ( image , diffImage , options ) {
26
- const image1 = this . config . baseFolder + image ;
27
- const image2 = this . config . screenshotFolder + image ;
40
+ const image1 = this . baseFolder + image ;
41
+ const image2 = this . screenshotFolder + image ;
28
42
29
43
// check whether the base and the screenshot images are present.
30
44
fs . access ( image1 , fs . constants . F_OK | fs . constants . W_OK , ( err ) => {
@@ -42,7 +56,7 @@ class ResembleHelper extends Helper {
42
56
} ) ;
43
57
44
58
return new Promise ( ( resolve , reject ) => {
45
-
59
+
46
60
resemble . outputSettings ( {
47
61
boundingBox : options . boundingBox ,
48
62
ignoredBox : options . ignoredBox
@@ -55,23 +69,22 @@ class ResembleHelper extends Helper {
55
69
if ( err ) {
56
70
reject ( err ) ;
57
71
} else {
58
- if ( ! data . isSameDimensions ) {
72
+ if ( ! data . isSameDimensions ) {
59
73
let dimensions1 = sizeOf ( image1 ) ;
60
74
let dimensions2 = sizeOf ( image2 ) ;
61
- reject ( new Error ( "The image1 is of " + dimensions1 . height + " X " + dimensions1 . width + " and image2 is of " + dimensions2 . height + " X " + dimensions2 . width + ". Please use images of same dimensions so as to avoid any unexpected results." ) ) ;
75
+ reject ( new Error ( "The image1 is of " + dimensions1 . height + " X " + dimensions1 . width + " and image2 is of " + dimensions2 . height + " X " + dimensions2 . width + ". Please use images of same dimensions so as to avoid any unexpected results." ) ) ;
62
76
}
63
77
resolve ( data ) ;
64
78
if ( data . misMatchPercentage >= tolerance ) {
65
- mkdirp ( getDirName ( this . config . diffFolder + diffImage ) , function ( err ) {
79
+ mkdirp ( getDirName ( this . diffFolder + diffImage ) , function ( err ) {
66
80
if ( err ) return cb ( err ) ;
67
81
} ) ;
68
- fs . writeFile ( this . config . diffFolder + diffImage + '.png' , data . getBuffer ( ) , ( err , data ) => {
82
+ fs . writeFile ( this . diffFolder + diffImage + '.png' , data . getBuffer ( ) , ( err , data ) => {
69
83
if ( err ) {
70
84
throw new Error ( this . err ) ;
71
- }
72
- else {
73
- const diffImagePath = path . join ( process . cwd ( ) , this . config . diffFolder + diffImage + '.png' ) ;
74
- this . debug ( "Diff Image File Saved to: " + diffImagePath ) ;
85
+ } else {
86
+ const diffImagePath = path . join ( process . cwd ( ) , this . diffFolder + diffImage + '.png' ) ;
87
+ this . debug ( "Diff Image File Saved to: " + diffImagePath ) ;
75
88
}
76
89
} ) ;
77
90
}
@@ -95,13 +108,13 @@ class ResembleHelper extends Helper {
95
108
96
109
/**
97
110
* Take screenshot of individual element.
98
- * @param selector selector of the element to be screenshotted
111
+ * @param selector selector of the element to be screenshotted
99
112
* @param name name of the image
100
- * @returns {Promise<void> }
113
+ * @returns {Promise<void> }
101
114
*/
102
115
async screenshotElement ( selector , name ) {
103
116
const helper = this . _getHelper ( ) ;
104
- if ( this . helpers [ 'Puppeteer' ] ) {
117
+ if ( this . helpers [ 'Puppeteer' ] ) {
105
118
await helper . waitForVisible ( selector ) ;
106
119
const els = await helper . _locate ( selector ) ;
107
120
if ( ! els . length ) throw new Error ( `Element ${ selector } couldn't be located` ) ;
@@ -111,16 +124,13 @@ class ResembleHelper extends Helper {
111
124
path : global . output_dir + "/" + name + '.png'
112
125
} ) ;
113
126
} else if ( this . helpers [ 'WebDriver' ] ) {
114
- const configuration = this . config ;
115
-
116
127
await helper . waitForVisible ( selector ) ;
117
128
const els = await helper . _locate ( selector ) ;
118
129
if ( ! els . length ) throw new Error ( `Element ${ selector } couldn't be located` ) ;
119
130
const el = els [ 0 ] ;
120
131
121
- await el . saveScreenshot ( configuration . screenshotFolder + name + '.png' ) ;
122
- }
123
- else throw new Error ( "Method only works with Puppeteer and WebDriver helpers." ) ;
132
+ await el . saveScreenshot ( this . screenshotFolder + name + '.png' ) ;
133
+ } else throw new Error ( "Method only works with Puppeteer and WebDriver helpers." ) ;
124
134
}
125
135
126
136
/**
@@ -135,12 +145,12 @@ class ResembleHelper extends Helper {
135
145
const allure = codeceptjs . container . plugins ( 'allure' ) ;
136
146
const diffImage = "Diff_" + baseImage . split ( "." ) [ 0 ] + ".png" ;
137
147
138
- if ( allure !== undefined && misMatch >= tolerance ) {
139
- allure . addAttachment ( 'Base Image' , fs . readFileSync ( this . config . baseFolder + baseImage ) , 'image/png' ) ;
140
- allure . addAttachment ( 'Screenshot Image' , fs . readFileSync ( this . config . screenshotFolder + baseImage ) , 'image/png' ) ;
141
- allure . addAttachment ( 'Diff Image' , fs . readFileSync ( this . config . diffFolder + diffImage ) , 'image/png' ) ;
148
+ if ( allure !== undefined && misMatch >= tolerance ) {
149
+ allure . addAttachment ( 'Base Image' , fs . readFileSync ( this . baseFolder + baseImage ) , 'image/png' ) ;
150
+ allure . addAttachment ( 'Screenshot Image' , fs . readFileSync ( this . screenshotFolder + baseImage ) , 'image/png' ) ;
151
+ allure . addAttachment ( 'Diff Image' , fs . readFileSync ( this . diffFolder + diffImage ) , 'image/png' ) ;
142
152
}
143
- }
153
+ }
144
154
145
155
/**
146
156
* This method uploads the diff and screenshot images into the bucket with diff image under bucketName/diff/diffImage and the screenshot image as
@@ -155,60 +165,59 @@ class ResembleHelper extends Helper {
155
165
*/
156
166
157
167
async _upload ( accessKeyId , secretAccessKey , region , bucketName , baseImage , ifBaseImage ) {
158
- console . log ( "Starting Upload... " ) ;
159
- const s3 = new AWS . S3 ( {
160
- accessKeyId : accessKeyId ,
161
- secretAccessKey : secretAccessKey ,
162
- region : region
168
+ console . log ( "Starting Upload... " ) ;
169
+ const s3 = new AWS . S3 ( {
170
+ accessKeyId : accessKeyId ,
171
+ secretAccessKey : secretAccessKey ,
172
+ region : region
173
+ } ) ;
174
+ fs . readFile ( this . screenshotFolder + baseImage , ( err , data ) => {
175
+ if ( err ) throw err ;
176
+ let base64data = new Buffer ( data , 'binary' ) ;
177
+ const params = {
178
+ Bucket : bucketName ,
179
+ Key : `output/${ baseImage } ` ,
180
+ Body : base64data
181
+ } ;
182
+ s3 . upload ( params , ( uerr , data ) => {
183
+ if ( uerr ) throw uerr ;
184
+ console . log ( `Screenshot Image uploaded successfully at ${ data . Location } ` ) ;
163
185
} ) ;
164
- fs . readFile ( this . config . screenshotFolder + baseImage , ( err , data ) => {
165
- if ( err ) throw err ;
186
+ } ) ;
187
+ fs . readFile ( this . diffFolder + "Diff_" + baseImage , ( err , data ) => {
188
+ if ( err ) console . log ( "Diff image not generated" ) ;
189
+ else {
190
+ let base64data = new Buffer ( data , 'binary' ) ;
191
+ const params = {
192
+ Bucket : bucketName ,
193
+ Key : `diff/Diff_${ baseImage } ` ,
194
+ Body : base64data
195
+ } ;
196
+ s3 . upload ( params , ( uerr , data ) => {
197
+ if ( uerr ) throw uerr ;
198
+ console . log ( `Diff Image uploaded successfully at ${ data . Location } ` )
199
+ } ) ;
200
+ }
201
+ } ) ;
202
+ if ( ifBaseImage ) {
203
+ fs . readFile ( this . baseFolder + baseImage , ( err , data ) => {
204
+ if ( err ) throw err ;
205
+ else {
166
206
let base64data = new Buffer ( data , 'binary' ) ;
167
207
const params = {
168
- Bucket : bucketName ,
169
- Key : `output /${ baseImage } ` ,
170
- Body : base64data
208
+ Bucket : bucketName ,
209
+ Key : `base /${ baseImage } ` ,
210
+ Body : base64data
171
211
} ;
172
212
s3 . upload ( params , ( uerr , data ) => {
173
- if ( uerr ) throw uerr ;
174
- console . log ( `Screenshot Image uploaded successfully at ${ data . Location } ` ) ;
213
+ if ( uerr ) throw uerr ;
214
+ console . log ( `Base Image uploaded at ${ data . Location } ` )
175
215
} ) ;
216
+ }
176
217
} ) ;
177
- fs . readFile ( this . config . diffFolder + "Diff_" + baseImage , ( err , data ) => {
178
- if ( err ) console . log ( "Diff image not generated" ) ;
179
- else {
180
- let base64data = new Buffer ( data , 'binary' ) ;
181
- const params = {
182
- Bucket : bucketName ,
183
- Key : `diff/Diff_${ baseImage } ` ,
184
- Body : base64data
185
- } ;
186
- s3 . upload ( params , ( uerr , data ) => {
187
- if ( uerr ) throw uerr ;
188
- console . log ( `Diff Image uploaded successfully at ${ data . Location } ` )
189
- } ) ;
190
- }
191
- } ) ;
192
- if ( ifBaseImage ) {
193
- fs . readFile ( this . config . baseFolder + baseImage , ( err , data ) => {
194
- if ( err ) throw err ;
195
- else {
196
- let base64data = new Buffer ( data , 'binary' ) ;
197
- const params = {
198
- Bucket : bucketName ,
199
- Key : `base/${ baseImage } ` ,
200
- Body : base64data
201
- } ;
202
- s3 . upload ( params , ( uerr , data ) => {
203
- if ( uerr ) throw uerr ;
204
- console . log ( `Base Image uploaded at ${ data . Location } ` )
205
- } ) ;
206
- }
207
- } ) ;
208
- }
209
- else {
210
- console . log ( "Not Uploading base Image" ) ;
211
- }
218
+ } else {
219
+ console . log ( "Not Uploading base Image" ) ;
220
+ }
212
221
}
213
222
214
223
/**
@@ -222,24 +231,24 @@ class ResembleHelper extends Helper {
222
231
*/
223
232
224
233
_download ( accessKeyId , secretAccessKey , region , bucketName , baseImage ) {
225
- console . log ( "Starting Download..." ) ;
226
- const s3 = new AWS . S3 ( {
227
- accessKeyId : accessKeyId ,
228
- secretAccessKey : secretAccessKey ,
229
- region : region
230
- } ) ;
231
- const params = {
232
- Bucket : bucketName ,
233
- Key : `base/${ baseImage } `
234
- } ;
235
- return new Promise ( ( resolve , reject ) => {
236
- s3 . getObject ( params , ( err , data ) => {
237
- if ( err ) console . error ( err ) ;
238
- console . log ( this . config . baseFolder + baseImage ) ;
239
- fs . writeFileSync ( this . config . baseFolder + baseImage , data . Body ) ;
240
- resolve ( "File Downloaded Successfully" ) ;
241
- } ) ;
234
+ console . log ( "Starting Download..." ) ;
235
+ const s3 = new AWS . S3 ( {
236
+ accessKeyId : accessKeyId ,
237
+ secretAccessKey : secretAccessKey ,
238
+ region : region
239
+ } ) ;
240
+ const params = {
241
+ Bucket : bucketName ,
242
+ Key : `base/${ baseImage } `
243
+ } ;
244
+ return new Promise ( ( resolve , reject ) => {
245
+ s3 . getObject ( params , ( err , data ) => {
246
+ if ( err ) console . error ( err ) ;
247
+ console . log ( this . baseFolder + baseImage ) ;
248
+ fs . writeFileSync ( this . baseFolder + baseImage , data . Body ) ;
249
+ resolve ( "File Downloaded Successfully" ) ;
242
250
} ) ;
251
+ } ) ;
243
252
}
244
253
245
254
/**
@@ -255,10 +264,9 @@ class ResembleHelper extends Helper {
255
264
}
256
265
257
266
const awsC = this . config . aws ;
258
- this . config . screenshotFolder = global . output_dir + "/" ;
259
267
260
268
if ( awsC !== undefined && options . prepareBaseImage === false ) {
261
- await this . _download ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage ) ;
269
+ await this . _download ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage ) ;
262
270
}
263
271
264
272
if ( options . prepareBaseImage !== undefined && options . prepareBaseImage ) {
@@ -269,9 +277,9 @@ class ResembleHelper extends Helper {
269
277
270
278
this . _addAttachment ( baseImage , misMatch , options . tolerance ) ;
271
279
272
- if ( awsC !== undefined ) {
273
- let ifUpload = options . prepareBaseImage === false ? false : true ;
274
- await this . _upload ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage , ifUpload )
280
+ if ( awsC !== undefined ) {
281
+ let ifUpload = options . prepareBaseImage === false ? false : true ;
282
+ await this . _upload ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage , ifUpload )
275
283
}
276
284
277
285
this . debug ( "MisMatch Percentage Calculated is " + misMatch ) ;
@@ -294,10 +302,9 @@ class ResembleHelper extends Helper {
294
302
}
295
303
296
304
const awsC = this . config . aws ;
297
- this . config . screenshotFolder = global . output_dir + "/" ;
298
305
299
306
if ( awsC !== undefined && options . prepareBaseImage === false ) {
300
- await this . _download ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage ) ;
307
+ await this . _download ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage ) ;
301
308
}
302
309
303
310
if ( options . prepareBaseImage !== undefined && options . prepareBaseImage ) {
@@ -308,10 +315,10 @@ class ResembleHelper extends Helper {
308
315
const misMatch = await this . _fetchMisMatchPercentage ( baseImage , options ) ;
309
316
310
317
this . _addAttachment ( baseImage , misMatch , options . tolerance ) ;
311
-
312
- if ( awsC !== undefined ) {
313
- let ifUpload = options . prepareBaseImage === false ? false : true ;
314
- await this . _upload ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage , ifUpload )
318
+
319
+ if ( awsC !== undefined ) {
320
+ let ifUpload = options . prepareBaseImage === false ? false : true ;
321
+ await this . _upload ( awsC . accessKeyId , awsC . secretAccessKey , awsC . region , awsC . bucketName , baseImage , ifUpload )
315
322
}
316
323
317
324
this . debug ( "MisMatch Percentage Calculated is " + misMatch ) ;
@@ -324,25 +331,23 @@ class ResembleHelper extends Helper {
324
331
* @param screenShotImage Name of the screenshot Image (Screenshot Image Path is taken from Configuration)
325
332
*/
326
333
async _prepareBaseImage ( screenShotImage ) {
327
- const configuration = this . config ;
328
-
329
- await this . _createDir ( configuration . baseFolder + screenShotImage ) ;
334
+ await this . _createDir ( this . baseFolder + screenShotImage ) ;
330
335
331
- fs . access ( configuration . screenshotFolder + screenShotImage , fs . constants . F_OK | fs . constants . W_OK , ( err ) => {
336
+ fs . access ( this . screenshotFolder + screenShotImage , fs . constants . F_OK | fs . constants . W_OK , ( err ) => {
332
337
if ( err ) {
333
338
throw new Error (
334
- `${ configuration . screenshotFolder + screenShotImage } ${ err . code === 'ENOENT' ? 'does not exist' : 'is read-only' } ` ) ;
339
+ `${ this . screenshotFolder + screenShotImage } ${ err . code === 'ENOENT' ? 'does not exist' : 'is read-only' } ` ) ;
335
340
}
336
341
} ) ;
337
342
338
- fs . access ( configuration . baseFolder , fs . constants . F_OK | fs . constants . W_OK , ( err ) => {
343
+ fs . access ( this . baseFolder , fs . constants . F_OK | fs . constants . W_OK , ( err ) => {
339
344
if ( err ) {
340
345
throw new Error (
341
- `${ configuration . baseFolder } ${ err . code === 'ENOENT' ? 'does not exist' : 'is read-only' } ` ) ;
346
+ `${ this . baseFolder } ${ err . code === 'ENOENT' ? 'does not exist' : 'is read-only' } ` ) ;
342
347
}
343
348
} ) ;
344
349
345
- fs . copyFileSync ( configuration . screenshotFolder + screenShotImage , configuration . baseFolder + screenShotImage ) ;
350
+ fs . copyFileSync ( this . screenshotFolder + screenShotImage , this . baseFolder + screenShotImage ) ;
346
351
}
347
352
348
353
/**
@@ -371,15 +376,15 @@ class ResembleHelper extends Helper {
371
376
let location , size ;
372
377
373
378
if ( this . helpers [ 'Puppeteer' ] ) {
374
- const box = await el . boundingBox ( ) ;
379
+ const box = await el . boundingBox ( ) ;
375
380
size = location = box ;
376
381
}
377
382
378
383
if ( this . helpers [ 'WebDriver' ] || this . helpers [ 'Appium' ] ) {
379
384
location = await el . getLocation ( ) ;
380
385
size = await el . getSize ( ) ;
381
- }
382
-
386
+ }
387
+
383
388
if ( this . helpers [ 'WebDriverIO' ] ) {
384
389
location = await helper . browser . getLocation ( selector ) ;
385
390
size = await helper . browser . getElementSize ( selector ) ;
@@ -417,4 +422,5 @@ class ResembleHelper extends Helper {
417
422
throw new Error ( 'No matching helper found. Supported helpers: WebDriver/Appium/Puppeteer' ) ;
418
423
}
419
424
}
425
+
420
426
module . exports = ResembleHelper ;
0 commit comments