@@ -11,6 +11,7 @@ import { UIElement } from "./ui-element";
11
11
import { Direction } from "./direction" ;
12
12
import { Locator } from "./locators" ;
13
13
import {
14
+ addExt ,
14
15
log ,
15
16
getStorageByPlatform ,
16
17
getStorageByDeviceName ,
@@ -19,7 +20,7 @@ import {
19
20
getAppPath ,
20
21
getReportPath ,
21
22
calculateOffset ,
22
- scroll
23
+ scroll ,
23
24
} from "./utils" ;
24
25
import { INsCapabilities } from "./interfaces/ns-capabilities" ;
25
26
import { IRectangle } from "./interfaces/rectangle" ;
@@ -270,141 +271,81 @@ export class AppiumDriver {
270
271
}
271
272
272
273
public async compareElement ( element : UIElement , imageName : string , ) {
273
- return this . compareRectangle ( await element . getRectangle ( ) , imageName ) ;
274
+ return this . compareRectangles ( await element . getRectangle ( ) , imageName ) ;
274
275
}
275
276
276
- public async compareRectangle ( rect : IRectangle , imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 ) {
277
- if ( ! imageName . endsWith ( AppiumDriver . pngFileExt ) ) {
278
- imageName = imageName . concat ( AppiumDriver . pngFileExt ) ;
279
- }
280
-
281
- if ( ! this . _storageByDeviceName ) {
282
- this . _storageByDeviceName = getStorageByDeviceName ( this . _args ) ;
283
- }
277
+ public async compareRectangles ( rect : IRectangle , imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 ) {
278
+ return this . compare ( imageName , timeOutSeconds , tollerance , rect ) ;
279
+ }
284
280
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
- }
281
+ public async compareScreen ( imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 ) {
282
+ return this . compare ( imageName , timeOutSeconds , tollerance ) ;
283
+ }
292
284
293
- if ( ! fileExists ( expectedImage ) ) {
294
- expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
295
- }
285
+ public async compare ( imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 , rect ?: IRectangle ) {
296
286
297
287
if ( ! this . _logPath ) {
298
288
this . _logPath = getReportPath ( this . _args ) ;
299
289
}
300
290
301
- expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
291
+ imageName = addExt ( imageName , AppiumDriver . pngFileExt ) ;
302
292
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 ;
293
+ const pathExpectedImage = this . getExpectedImagePath ( imageName ) ;
310
294
311
- while ( ( Date . now ( ) . valueOf ( ) - eventStartTime ) <= timeOutSeconds ) {
312
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual" + "_" + counter + "." ) ) ) ;
313
- counter ++ ;
295
+ // First time capture
296
+ if ( ! fileExists ( pathExpectedImage ) ) {
297
+ const pathActualImage = resolve ( this . _storageByDeviceName , imageName . replace ( "." , "_actual." ) ) ;
298
+ await this . takeScreenshot ( pathActualImage ) ;
299
+
300
+ if ( rect ) {
301
+ await this . _imageHelper . clipRectangleImage ( rect , pathActualImage ) ;
314
302
}
315
303
316
- // return false;
304
+ console . log ( "Remove the 'actual' suffix to continue using the image as expected one " , pathExpectedImage ) ;
305
+ return false ;
317
306
}
318
307
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 ) ;
308
+ // Compare
309
+ let pathActualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual." ) ) ) ;
310
+ const pathDiffImage = pathActualImage . replace ( "actual" , "diff" ) ;
311
+
312
+ await this . prepareImageToCompare ( pathActualImage , rect ) ;
313
+ let result = await this . _imageHelper . compareImages ( pathActualImage , pathExpectedImage , pathDiffImage , tollerance ) ;
314
+
315
+ // Iterate
322
316
if ( ! result ) {
323
- let eventStartTime = Date . now ( ) . valueOf ( ) ;
317
+ const eventStartTime = Date . now ( ) . valueOf ( ) ;
324
318
let counter = 1 ;
325
319
timeOutSeconds *= 1000 ;
326
320
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 ) ;
321
+ const pathActualImageConter = resolve ( this . _logPath , imageName . replace ( "." , "_actual_" + counter + "." ) ) ;
322
+ pathActualImage = await this . takeScreenshot ( pathActualImageConter ) ;
323
+
324
+ await this . prepareImageToCompare ( pathActualImage , rect ) ;
325
+ result = await this . _imageHelper . compareImages ( pathActualImage , pathExpectedImage , pathDiffImage , tollerance ) ;
329
326
counter ++ ;
330
327
}
331
328
} else {
332
- if ( fileExists ( diffImage ) ) {
333
- unlinkSync ( diffImage ) ;
329
+ if ( fileExists ( pathDiffImage ) ) {
330
+ unlinkSync ( pathDiffImage ) ;
334
331
}
335
- if ( fileExists ( actualImage ) ) {
336
- unlinkSync ( actualImage ) ;
332
+ if ( fileExists ( pathActualImage ) ) {
333
+ unlinkSync ( pathActualImage ) ;
337
334
}
338
335
}
339
336
337
+ this . _imageHelper . imageCropRect = undefined ;
340
338
return result ;
341
339
}
342
340
343
- public async compareScreen ( imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 ) {
344
- if ( ! imageName . endsWith ( AppiumDriver . pngFileExt ) ) {
345
- imageName = imageName . concat ( AppiumDriver . pngFileExt ) ;
346
- }
347
-
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 ) ;
369
-
370
- // Firts capture of screen when the expected image is not available
371
- if ( ! fileExists ( expectedImage ) ) {
372
- 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
-
383
- return false ;
384
- }
385
-
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 ) ;
389
- if ( ! result ) {
390
- let eventStartTime = Date . now ( ) . valueOf ( ) ;
391
- let counter = 1 ;
392
- timeOutSeconds *= 1000 ;
393
- 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 ) ;
396
- counter ++ ;
397
- }
341
+ public async prepareImageToCompare ( filePath : string , rect : IRectangle ) {
342
+ if ( rect ) {
343
+ await this . _imageHelper . clipRectangleImage ( rect , filePath ) ;
344
+ const rectToCrop = { x : 0 , y : 0 , width : undefined , height : undefined } ;
345
+ this . _imageHelper . imageCropRect = rectToCrop ;
398
346
} else {
399
- if ( fileExists ( diffImage ) ) {
400
- unlinkSync ( diffImage ) ;
401
- }
402
- if ( fileExists ( actualImage ) ) {
403
- unlinkSync ( actualImage ) ;
404
- }
347
+ this . _imageHelper . imageCropRect = ImageHelper . cropImageDefault ( this . _args ) ;
405
348
}
406
-
407
- return result ;
408
349
}
409
350
410
351
public takeScreenshot ( fileName : string ) {
@@ -529,4 +470,26 @@ export class AppiumDriver {
529
470
log ( " > " + meth . magenta + path + " " + ( data || "" ) . grey , verbose ) ;
530
471
} ) ;
531
472
} ;
473
+
474
+ private getExpectedImagePath ( imageName : string ) {
475
+
476
+ if ( ! this . _storageByDeviceName ) {
477
+ this . _storageByDeviceName = getStorageByDeviceName ( this . _args ) ;
478
+ }
479
+
480
+ let pathExpectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
481
+
482
+ if ( ! fileExists ( pathExpectedImage ) ) {
483
+ if ( ! this . _storageByPlatform ) {
484
+ this . _storageByPlatform = getStorageByPlatform ( this . _args ) ;
485
+ }
486
+ pathExpectedImage = resolve ( this . _storageByPlatform , imageName ) ;
487
+ }
488
+
489
+ if ( ! fileExists ( pathExpectedImage ) ) {
490
+ pathExpectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
491
+ }
492
+
493
+ return pathExpectedImage ;
494
+ }
532
495
}
0 commit comments