@@ -14,17 +14,31 @@ const sizeOf = require('image-size');
1414
1515class ResembleHelper extends Helper {
1616
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+
1731 /**
1832 * Compare Images
19- *
33+ *
2034 * @param image
2135 * @param diffImage
2236 * @param options
2337 * @returns {Promise<any | never> }
2438 */
2539 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 ;
2842
2943 // check whether the base and the screenshot images are present.
3044 fs . access ( image1 , fs . constants . F_OK | fs . constants . W_OK , ( err ) => {
@@ -42,7 +56,7 @@ class ResembleHelper extends Helper {
4256 } ) ;
4357
4458 return new Promise ( ( resolve , reject ) => {
45-
59+
4660 resemble . outputSettings ( {
4761 boundingBox : options . boundingBox ,
4862 ignoredBox : options . ignoredBox
@@ -55,23 +69,22 @@ class ResembleHelper extends Helper {
5569 if ( err ) {
5670 reject ( err ) ;
5771 } else {
58- if ( ! data . isSameDimensions ) {
72+ if ( ! data . isSameDimensions ) {
5973 let dimensions1 = sizeOf ( image1 ) ;
6074 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." ) ) ;
6276 }
6377 resolve ( data ) ;
6478 if ( data . misMatchPercentage >= tolerance ) {
65- mkdirp ( getDirName ( this . config . diffFolder + diffImage ) , function ( err ) {
79+ mkdirp ( getDirName ( this . diffFolder + diffImage ) , function ( err ) {
6680 if ( err ) return cb ( err ) ;
6781 } ) ;
68- fs . writeFile ( this . config . diffFolder + diffImage + '.png' , data . getBuffer ( ) , ( err , data ) => {
82+ fs . writeFile ( this . diffFolder + diffImage + '.png' , data . getBuffer ( ) , ( err , data ) => {
6983 if ( err ) {
7084 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 ) ;
7588 }
7689 } ) ;
7790 }
@@ -95,13 +108,13 @@ class ResembleHelper extends Helper {
95108
96109 /**
97110 * 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
99112 * @param name name of the image
100- * @returns {Promise<void> }
113+ * @returns {Promise<void> }
101114 */
102115 async screenshotElement ( selector , name ) {
103116 const helper = this . _getHelper ( ) ;
104- if ( this . helpers [ 'Puppeteer' ] ) {
117+ if ( this . helpers [ 'Puppeteer' ] ) {
105118 await helper . waitForVisible ( selector ) ;
106119 const els = await helper . _locate ( selector ) ;
107120 if ( ! els . length ) throw new Error ( `Element ${ selector } couldn't be located` ) ;
@@ -111,16 +124,13 @@ class ResembleHelper extends Helper {
111124 path : global . output_dir + "/" + name + '.png'
112125 } ) ;
113126 } else if ( this . helpers [ 'WebDriver' ] ) {
114- const configuration = this . config ;
115-
116127 await helper . waitForVisible ( selector ) ;
117128 const els = await helper . _locate ( selector ) ;
118129 if ( ! els . length ) throw new Error ( `Element ${ selector } couldn't be located` ) ;
119130 const el = els [ 0 ] ;
120131
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." ) ;
124134 }
125135
126136 /**
@@ -135,12 +145,12 @@ class ResembleHelper extends Helper {
135145 const allure = codeceptjs . container . plugins ( 'allure' ) ;
136146 const diffImage = "Diff_" + baseImage . split ( "." ) [ 0 ] + ".png" ;
137147
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' ) ;
142152 }
143- }
153+ }
144154
145155 /**
146156 * 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 {
155165 */
156166
157167 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 } ` ) ;
163185 } ) ;
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 {
166206 let base64data = new Buffer ( data , 'binary' ) ;
167207 const params = {
168- Bucket : bucketName ,
169- Key : `output /${ baseImage } ` ,
170- Body : base64data
208+ Bucket : bucketName ,
209+ Key : `base /${ baseImage } ` ,
210+ Body : base64data
171211 } ;
172212 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 } ` )
175215 } ) ;
216+ }
176217 } ) ;
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+ }
212221 }
213222
214223 /**
@@ -222,24 +231,24 @@ class ResembleHelper extends Helper {
222231 */
223232
224233 _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" ) ;
242250 } ) ;
251+ } ) ;
243252 }
244253
245254 /**
@@ -255,10 +264,9 @@ class ResembleHelper extends Helper {
255264 }
256265
257266 const awsC = this . config . aws ;
258- this . config . screenshotFolder = global . output_dir + "/" ;
259267
260268 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 ) ;
262270 }
263271
264272 if ( options . prepareBaseImage !== undefined && options . prepareBaseImage ) {
@@ -269,9 +277,9 @@ class ResembleHelper extends Helper {
269277
270278 this . _addAttachment ( baseImage , misMatch , options . tolerance ) ;
271279
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 )
275283 }
276284
277285 this . debug ( "MisMatch Percentage Calculated is " + misMatch ) ;
@@ -294,10 +302,9 @@ class ResembleHelper extends Helper {
294302 }
295303
296304 const awsC = this . config . aws ;
297- this . config . screenshotFolder = global . output_dir + "/" ;
298305
299306 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 ) ;
301308 }
302309
303310 if ( options . prepareBaseImage !== undefined && options . prepareBaseImage ) {
@@ -308,10 +315,10 @@ class ResembleHelper extends Helper {
308315 const misMatch = await this . _fetchMisMatchPercentage ( baseImage , options ) ;
309316
310317 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 )
315322 }
316323
317324 this . debug ( "MisMatch Percentage Calculated is " + misMatch ) ;
@@ -324,25 +331,23 @@ class ResembleHelper extends Helper {
324331 * @param screenShotImage Name of the screenshot Image (Screenshot Image Path is taken from Configuration)
325332 */
326333 async _prepareBaseImage ( screenShotImage ) {
327- const configuration = this . config ;
328-
329- await this . _createDir ( configuration . baseFolder + screenShotImage ) ;
334+ await this . _createDir ( this . baseFolder + screenShotImage ) ;
330335
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 ) => {
332337 if ( err ) {
333338 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' } ` ) ;
335340 }
336341 } ) ;
337342
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 ) => {
339344 if ( err ) {
340345 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' } ` ) ;
342347 }
343348 } ) ;
344349
345- fs . copyFileSync ( configuration . screenshotFolder + screenShotImage , configuration . baseFolder + screenShotImage ) ;
350+ fs . copyFileSync ( this . screenshotFolder + screenShotImage , this . baseFolder + screenShotImage ) ;
346351 }
347352
348353 /**
@@ -371,15 +376,15 @@ class ResembleHelper extends Helper {
371376 let location , size ;
372377
373378 if ( this . helpers [ 'Puppeteer' ] ) {
374- const box = await el . boundingBox ( ) ;
379+ const box = await el . boundingBox ( ) ;
375380 size = location = box ;
376381 }
377382
378383 if ( this . helpers [ 'WebDriver' ] || this . helpers [ 'Appium' ] ) {
379384 location = await el . getLocation ( ) ;
380385 size = await el . getSize ( ) ;
381- }
382-
386+ }
387+
383388 if ( this . helpers [ 'WebDriverIO' ] ) {
384389 location = await helper . browser . getLocation ( selector ) ;
385390 size = await helper . browser . getElementSize ( selector ) ;
@@ -417,4 +422,5 @@ class ResembleHelper extends Helper {
417422 throw new Error ( 'No matching helper found. Supported helpers: WebDriver/Appium/Puppeteer' ) ;
418423 }
419424}
425+
420426module . exports = ResembleHelper ;
0 commit comments