@@ -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,9 +20,10 @@ 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" ;
26
+ import { IRectangle } from "./interfaces/rectangle" ;
25
27
import { Point } from "./point" ;
26
28
import { ImageHelper } from "./image-helper" ;
27
29
import { ImageOptions } from "./image-options"
@@ -268,73 +270,84 @@ export class AppiumDriver {
268
270
return await this . driver . getSessionId ( ) ;
269
271
}
270
272
273
+ public async compareElement ( element : UIElement , imageName : string , ) {
274
+ return await this . compareRectangles ( await element . getRectangle ( ) , imageName ) ;
275
+ }
276
+
277
+ public async compareRectangles ( rect : IRectangle , imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 ) {
278
+ return await this . compare ( imageName , timeOutSeconds , tollerance , rect ) ;
279
+ }
280
+
271
281
public async compareScreen ( imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 ) {
272
- if ( ! imageName . endsWith ( AppiumDriver . pngFileExt ) ) {
273
- imageName = imageName . concat ( AppiumDriver . pngFileExt ) ;
274
- }
282
+ return await this . compare ( imageName , timeOutSeconds , tollerance ) ;
283
+ }
275
284
276
- if ( ! this . _storageByDeviceName ) {
277
- this . _storageByDeviceName = getStorageByDeviceName ( this . _args ) ;
278
- }
285
+ private async compare ( imageName : string , timeOutSeconds : number = 3 , tollerance : number = 0.01 , rect ?: IRectangle ) {
279
286
280
- let expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
281
- if ( ! fileExists ( expectedImage ) ) {
282
- if ( ! this . _storageByPlatform ) {
283
- this . _storageByPlatform = getStorageByPlatform ( this . _args ) ;
284
- }
285
- expectedImage = resolve ( this . _storageByPlatform , imageName ) ;
286
- }
287
-
288
- if ( ! fileExists ( expectedImage ) ) {
289
- expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
290
- }
291
-
292
287
if ( ! this . _logPath ) {
293
288
this . _logPath = getReportPath ( this . _args ) ;
294
289
}
295
290
296
- expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
291
+ imageName = addExt ( imageName , AppiumDriver . pngFileExt ) ;
297
292
298
- // Firts capture of screen when the expected image is not available
299
- if ( ! fileExists ( expectedImage ) ) {
300
- await this . takeScreenshot ( resolve ( this . _storageByDeviceName , imageName . replace ( "." , "_actual." ) ) ) ;
301
- console . log ( "Remove the 'actual' suffix to continue using the image as expected one " , expectedImage ) ;
302
- let eventStartTime = Date . now ( ) . valueOf ( ) ;
303
- let counter = 1 ;
304
- timeOutSeconds *= 1000 ;
293
+ const pathExpectedImage = this . getExpectedImagePath ( imageName ) ;
305
294
306
- while ( ( Date . now ( ) . valueOf ( ) - eventStartTime ) <= timeOutSeconds ) {
307
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual" + "_" + counter + "." ) ) ) ;
308
- 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 ) ;
309
302
}
310
303
304
+ console . log ( "Remove the 'actual' suffix to continue using the image as expected one " , pathExpectedImage ) ;
311
305
return false ;
312
306
}
313
307
314
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual." ) ) ) ;
315
- let diffImage = actualImage . replace ( "actual" , "diff" ) ;
316
- let result = await this . _imageHelper . compareImages ( 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
317
316
if ( ! result ) {
318
- let eventStartTime = Date . now ( ) . valueOf ( ) ;
317
+ const eventStartTime = Date . now ( ) . valueOf ( ) ;
319
318
let counter = 1 ;
320
319
timeOutSeconds *= 1000 ;
321
320
while ( ( Date . now ( ) . valueOf ( ) - eventStartTime ) <= timeOutSeconds && ! result ) {
322
- let actualImage = await this . takeScreenshot ( resolve ( this . _logPath , imageName . replace ( "." , "_actual" + "_" + counter + "." ) ) ) ;
323
- result = await this . _imageHelper . compareImages ( 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 ) ;
324
326
counter ++ ;
325
327
}
326
328
} else {
327
- if ( fileExists ( diffImage ) ) {
328
- unlinkSync ( diffImage ) ;
329
+ if ( fileExists ( pathDiffImage ) ) {
330
+ unlinkSync ( pathDiffImage ) ;
329
331
}
330
- if ( fileExists ( actualImage ) ) {
331
- unlinkSync ( actualImage ) ;
332
+ if ( fileExists ( pathActualImage ) ) {
333
+ unlinkSync ( pathActualImage ) ;
332
334
}
333
335
}
334
336
337
+ this . _imageHelper . imageCropRect = undefined ;
335
338
return result ;
336
339
}
337
340
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 ;
346
+ } else {
347
+ this . _imageHelper . imageCropRect = ImageHelper . cropImageDefault ( this . _args ) ;
348
+ }
349
+ }
350
+
338
351
public takeScreenshot ( fileName : string ) {
339
352
if ( ! fileName . endsWith ( AppiumDriver . pngFileExt ) ) {
340
353
fileName = fileName . concat ( AppiumDriver . pngFileExt ) ;
@@ -457,4 +470,26 @@ export class AppiumDriver {
457
470
log ( " > " + meth . magenta + path + " " + ( data || "" ) . grey , verbose ) ;
458
471
} ) ;
459
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
+ }
460
495
}
0 commit comments