@@ -11,6 +11,7 @@ import { UIElement } from "./ui-element";
1111import { Direction } from "./direction" ;
1212import { Locator } from "./locators" ;
1313import {
14+ addExt ,
1415 log ,
1516 getStorageByPlatform ,
1617 getStorageByDeviceName ,
@@ -19,9 +20,10 @@ import {
1920 getAppPath ,
2021 getReportPath ,
2122 calculateOffset ,
22- scroll
23+ scroll ,
2324} from "./utils" ;
2425import { INsCapabilities } from "./interfaces/ns-capabilities" ;
26+ import { IRectangle } from "./interfaces/rectangle" ;
2527import { Point } from "./point" ;
2628import { ImageHelper } from "./image-helper" ;
2729import { ImageOptions } from "./image-options"
@@ -268,73 +270,84 @@ export class AppiumDriver {
268270 return await this . driver . getSessionId ( ) ;
269271 }
270272
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+
271281 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+ }
275284
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 ) {
279286
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-
292287 if ( ! this . _logPath ) {
293288 this . _logPath = getReportPath ( this . _args ) ;
294289 }
295290
296- expectedImage = resolve ( this . _storageByDeviceName , imageName ) ;
291+ imageName = addExt ( imageName , AppiumDriver . pngFileExt ) ;
297292
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 ) ;
305294
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 ) ;
309302 }
310303
304+ console . log ( "Remove the 'actual' suffix to continue using the image as expected one " , pathExpectedImage ) ;
311305 return false ;
312306 }
313307
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
317316 if ( ! result ) {
318- let eventStartTime = Date . now ( ) . valueOf ( ) ;
317+ const eventStartTime = Date . now ( ) . valueOf ( ) ;
319318 let counter = 1 ;
320319 timeOutSeconds *= 1000 ;
321320 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 ) ;
324326 counter ++ ;
325327 }
326328 } else {
327- if ( fileExists ( diffImage ) ) {
328- unlinkSync ( diffImage ) ;
329+ if ( fileExists ( pathDiffImage ) ) {
330+ unlinkSync ( pathDiffImage ) ;
329331 }
330- if ( fileExists ( actualImage ) ) {
331- unlinkSync ( actualImage ) ;
332+ if ( fileExists ( pathActualImage ) ) {
333+ unlinkSync ( pathActualImage ) ;
332334 }
333335 }
334336
337+ this . _imageHelper . imageCropRect = undefined ;
335338 return result ;
336339 }
337340
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+
338351 public takeScreenshot ( fileName : string ) {
339352 if ( ! fileName . endsWith ( AppiumDriver . pngFileExt ) ) {
340353 fileName = fileName . concat ( AppiumDriver . pngFileExt ) ;
@@ -457,4 +470,26 @@ export class AppiumDriver {
457470 log ( " > " + meth . magenta + path + " " + ( data || "" ) . grey , verbose ) ;
458471 } ) ;
459472 } ;
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+ }
460495}
0 commit comments