@@ -270,137 +270,96 @@ export class AppiumDriver {
270
270
}
271
271
272
272
public async compareElement ( element : UIElement , imageName : string , ) {
273
- return this . compareRectangle ( await element . getRectangle ( ) , imageName ) ;
273
+ return this . compareRectangles ( await element . getRectangle ( ) , imageName ) ;
274
274
}
275
275
276
- public async compareRectangle ( rect : IRectangle , imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 ) {
276
+ public async compareRectangles ( rect : IRectangle , imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 ) {
277
+
277
278
if ( ! imageName . endsWith ( AppiumDriver . pngFileExt ) ) {
278
279
imageName = imageName . concat ( AppiumDriver . pngFileExt ) ;
279
280
}
280
281
281
- if ( ! this . _storageByDeviceName ) {
282
- this . _storageByDeviceName = getStorageByDeviceName ( this . _args ) ;
283
- }
284
-
285
- let expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
286
- if ( ! fileExists ( expectedImage ) ) {
287
- if ( ! this . _storageByPlatform ) {
288
- this . _storageByPlatform = getStorageByPlatform ( this . _args ) ;
289
- }
290
- expectedImage = resolve ( this . _storageByPlatform , imageName ) ;
291
- }
292
-
293
- if ( ! fileExists ( expectedImage ) ) {
294
- expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
295
- }
282
+ const pathExpectedImage = this . getExpectedImagePath ( imageName ) ;
296
283
297
- if ( ! this . _logPath ) {
298
- this . _logPath = getReportPath ( this . _args ) ;
284
+ // First time capture
285
+ if ( ! fileExists ( pathExpectedImage ) ) {
286
+ const pathActualImage = resolve ( this . _storageByDeviceName , imageName . replace ( "." , "_actual." ) ) ;
287
+ await this . takeScreenshot ( pathActualImage ) ;
288
+ await this . _imageHelper . clipRectangleImage ( rect , pathActualImage ) ;
289
+ console . log ( "Remove the 'actual' suffix to continue using the image as expected one " , pathExpectedImage ) ;
290
+ return false ;
299
291
}
300
292
301
- expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
302
-
303
- // Firts capture of screen when the expected image is not available
304
- if ( ! fileExists ( expectedImage ) ) {
305
- await this . takeScreenshot ( resolve ( this . _storageByDeviceName , imageName . replace ( "." , "_actual." ) ) ) ;
306
- console . log ( "Remove the 'actual' suffix to continue using the image as expected one " , expectedImage ) ;
307
- let eventStartTime = Date . now ( ) . valueOf ( ) ;
308
- let counter = 1 ;
309
- timeOutSeconds *= 1000 ;
310
-
311
- while ( ( Date . now ( ) . valueOf ( ) - eventStartTime ) <= timeOutSeconds ) {
312
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual" + "_" + counter + "." ) ) ) ;
313
- counter ++ ;
314
- }
293
+ // Compare
294
+ let pathActualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual." ) ) ) ;
295
+ const pathDiffImage = pathActualImage . replace ( "actual" , "diff" ) ;
315
296
316
- // return false;
317
- }
297
+ await this . _imageHelper . clipRectangleImage ( rect , pathActualImage ) ;
298
+ const cropPoint = new Point ( 0 , 0 ) ;
299
+ await this . _imageHelper . setCropImagePoint ( cropPoint ) ;
300
+ let result = await this . _imageHelper . compareImages ( pathActualImage , pathExpectedImage , pathDiffImage , tollerance ) ;
318
301
319
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual." ) ) ) ;
320
- let diffImage = actualImage . replace ( "actual" , "diff" ) ;
321
- let result = await this . _imageHelper . compareRectangle ( rect , actualImage , expectedImage , diffImage , tollerance ) ;
302
+ // Iterate
322
303
if ( ! result ) {
323
- let eventStartTime = Date . now ( ) . valueOf ( ) ;
304
+ const eventStartTime = Date . now ( ) . valueOf ( ) ;
324
305
let counter = 1 ;
325
306
timeOutSeconds *= 1000 ;
326
307
while ( ( Date . now ( ) . valueOf ( ) - eventStartTime ) <= timeOutSeconds && ! result ) {
327
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual" + "_" + counter + "." ) ) ) ;
328
- result = await this . _imageHelper . compareRectangle ( rect , actualImage , expectedImage , diffImage , tollerance ) ;
308
+ const pathActualImageConter = resolve ( this . _logPath , imageName . replace ( "." , "_actual_" + counter + "." ) ) ;
309
+ pathActualImage = await this . takeScreenshot ( pathActualImageConter ) ;
310
+ await this . _imageHelper . clipRectangleImage ( rect , pathActualImage ) ;
311
+ await this . _imageHelper . setCropImagePoint ( cropPoint ) ;
312
+ result = await this . _imageHelper . compareImages ( pathActualImage , pathExpectedImage , pathDiffImage , tollerance ) ;
329
313
counter ++ ;
330
314
}
331
315
} else {
332
- if ( fileExists ( diffImage ) ) {
333
- unlinkSync ( diffImage ) ;
316
+ if ( fileExists ( pathDiffImage ) ) {
317
+ unlinkSync ( pathDiffImage ) ;
334
318
}
335
- if ( fileExists ( actualImage ) ) {
336
- unlinkSync ( actualImage ) ;
319
+ if ( fileExists ( pathActualImage ) ) {
320
+ unlinkSync ( pathActualImage ) ;
337
321
}
338
322
}
339
323
340
324
return result ;
341
325
}
342
326
343
327
public async compareScreen ( imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 ) {
328
+
344
329
if ( ! imageName . endsWith ( AppiumDriver . pngFileExt ) ) {
345
330
imageName = imageName . concat ( AppiumDriver . pngFileExt ) ;
346
331
}
347
332
348
- if ( ! this . _storageByDeviceName ) {
349
- this . _storageByDeviceName = getStorageByDeviceName ( this . _args ) ;
350
- }
351
-
352
- let expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
353
- if ( ! fileExists ( expectedImage ) ) {
354
- if ( ! this . _storageByPlatform ) {
355
- this . _storageByPlatform = getStorageByPlatform ( this . _args ) ;
356
- }
357
- expectedImage = resolve ( this . _storageByPlatform , imageName ) ;
358
- }
359
-
360
- if ( ! fileExists ( expectedImage ) ) {
361
- expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
362
- }
363
-
364
- if ( ! this . _logPath ) {
365
- this . _logPath = getReportPath ( this . _args ) ;
366
- }
367
-
368
- expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
333
+ const pathExpectedImage = this . getExpectedImagePath ( imageName ) ;
369
334
370
- // Firts capture of screen when the expected image is not available
371
- if ( ! fileExists ( expectedImage ) ) {
335
+ // First time capture
336
+ if ( ! fileExists ( pathExpectedImage ) ) {
372
337
await this . takeScreenshot ( resolve ( this . _storageByDeviceName , imageName . replace ( "." , "_actual." ) ) ) ;
373
- console . log ( "Remove the 'actual' suffix to continue using the image as expected one " , expectedImage ) ;
374
- let eventStartTime = Date . now ( ) . valueOf ( ) ;
375
- let counter = 1 ;
376
- timeOutSeconds *= 1000 ;
377
-
378
- while ( ( Date . now ( ) . valueOf ( ) - eventStartTime ) <= timeOutSeconds ) {
379
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual" + "_" + counter + "." ) ) ) ;
380
- counter ++ ;
381
- }
382
-
338
+ console . log ( "Remove the 'actual' suffix to continue using the image as expected one " , pathExpectedImage ) ;
383
339
return false ;
384
340
}
385
341
386
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual." ) ) ) ;
387
- let diffImage = actualImage . replace ( "actual" , "diff" ) ;
388
- let result = await this . _imageHelper . compareImages ( actualImage , expectedImage , diffImage , tollerance ) ;
342
+ // Compare
343
+ let pathActualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual." ) ) ) ;
344
+ const pathDiffImage = pathActualImage . replace ( "actual" , "diff" ) ;
345
+ let result = await this . _imageHelper . compareImages ( pathActualImage , pathExpectedImage , pathDiffImage , tollerance ) ;
346
+
347
+ // Iterate
389
348
if ( ! result ) {
390
- let eventStartTime = Date . now ( ) . valueOf ( ) ;
349
+ const eventStartTime = Date . now ( ) . valueOf ( ) ;
391
350
let counter = 1 ;
392
351
timeOutSeconds *= 1000 ;
393
352
while ( ( Date . now ( ) . valueOf ( ) - eventStartTime ) <= timeOutSeconds && ! result ) {
394
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual" + "_ " + counter + "." ) ) ) ;
395
- result = await this . _imageHelper . compareImages ( actualImage , expectedImage , diffImage , tollerance ) ;
353
+ pathActualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual_ " + counter + "." ) ) ) ;
354
+ result = await this . _imageHelper . compareImages ( pathActualImage , pathExpectedImage , pathDiffImage , tollerance ) ;
396
355
counter ++ ;
397
356
}
398
357
} else {
399
- if ( fileExists ( diffImage ) ) {
400
- unlinkSync ( diffImage ) ;
358
+ if ( fileExists ( pathDiffImage ) ) {
359
+ unlinkSync ( pathDiffImage ) ;
401
360
}
402
- if ( fileExists ( actualImage ) ) {
403
- unlinkSync ( actualImage ) ;
361
+ if ( fileExists ( pathActualImage ) ) {
362
+ unlinkSync ( pathActualImage ) ;
404
363
}
405
364
}
406
365
@@ -529,4 +488,30 @@ export class AppiumDriver {
529
488
log ( " > " + meth . magenta + path + " " + ( data || "" ) . grey , verbose ) ;
530
489
} ) ;
531
490
} ;
491
+
492
+ private getExpectedImagePath ( imageName : string ) {
493
+
494
+ if ( ! this . _storageByDeviceName ) {
495
+ this . _storageByDeviceName = getStorageByDeviceName ( this . _args ) ;
496
+ }
497
+
498
+ let pathExpectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
499
+
500
+ if ( ! fileExists ( pathExpectedImage ) ) {
501
+ if ( ! this . _storageByPlatform ) {
502
+ this . _storageByPlatform = getStorageByPlatform ( this . _args ) ;
503
+ }
504
+ pathExpectedImage = resolve ( this . _storageByPlatform , imageName ) ;
505
+ }
506
+
507
+ if ( ! fileExists ( pathExpectedImage ) ) {
508
+ pathExpectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
509
+ }
510
+
511
+ if ( ! this . _logPath ) {
512
+ this . _logPath = getReportPath ( this . _args ) ;
513
+ }
514
+
515
+ return pathExpectedImage ;
516
+ }
532
517
}
0 commit comments