@@ -9,7 +9,7 @@ import { AppiumDriver } from "./appium-driver";
99import { logError , checkImageLogType , resolvePath , copy , addExt , logWarn } from "./utils" ;
1010import { unlinkSync , existsSync , mkdirSync } from "fs" ;
1111import { basename , join } from "path" ;
12- import { isObject } from "util" ;
12+ import { isObject , isNumber } from "util" ;
1313import { logInfo } from "mobile-devices-controller/lib/utils" ;
1414
1515export interface IImageCompareOptions {
@@ -82,7 +82,6 @@ export interface IImageCompareOptions {
8282export class ImageHelper {
8383 private _blockOutAreas : IRectangle [ ] ;
8484 private _imagesResults = new Map < string , boolean > ( ) ;
85- private _imageCropRect : IRectangle ;
8685 private _options : IImageCompareOptions = { } ;
8786 private _defaultOptions : IImageCompareOptions = {
8887 timeOutSeconds : 2 ,
@@ -101,12 +100,14 @@ export class ImageHelper {
101100 constructor ( private _args : INsCapabilities , private _driver : AppiumDriver ) {
102101 this . _defaultOptions . cropRectangle = ( this . _args . appiumCaps && this . _args . appiumCaps . viewportRect ) || this . _args . device . viewportRect ;
103102 if ( ! this . _defaultOptions . cropRectangle
104- || this . _defaultOptions . cropRectangle . y === undefined
105- || this . _defaultOptions . cropRectangle . y === null
106- || this . _defaultOptions . cropRectangle . y === NaN ) {
103+ || ! isNumber ( this . _defaultOptions . cropRectangle . y ) ) {
107104 this . _defaultOptions . cropRectangle = this . _defaultOptions . cropRectangle || { } ;
108105 this . _defaultOptions . cropRectangle . y = this . _args . device . config . offsetPixels || 0 ;
109106 this . _defaultOptions . cropRectangle . x = 0 ;
107+ if ( this . _args . device . deviceScreenSize && this . _args . device . deviceScreenSize . width && this . _args . device . deviceScreenSize . height ) {
108+ this . _defaultOptions . cropRectangle . height = this . _args . device . deviceScreenSize . height - this . _defaultOptions . cropRectangle . y ;
109+ this . _defaultOptions . cropRectangle . width = this . _args . device . deviceScreenSize . width - this . _defaultOptions . cropRectangle . x ;
110+ }
110111 }
111112 ImageHelper . fullClone ( this . _defaultOptions , this . _options ) ;
112113
@@ -137,14 +138,6 @@ export class ImageHelper {
137138 this . _options = this . extendOptions ( options ) ;
138139 }
139140
140- get imageCropRect ( ) : IRectangle {
141- return this . _imageCropRect || this . options . cropRectangle ;
142- }
143-
144- set imageCropRect ( clipRectangle : IRectangle ) {
145- this . _imageCropRect = clipRectangle ;
146- }
147-
148141 get blockOutAreas ( ) {
149142 return this . _blockOutAreas ;
150143 }
@@ -257,7 +250,6 @@ export class ImageHelper {
257250 // First time capture
258251 if ( ! existsSync ( pathExpectedImage ) ) {
259252 await captureFirstImage ( ) ;
260-
261253 return false ;
262254 }
263255
@@ -269,7 +261,7 @@ export class ImageHelper {
269261 const pathDiffImage = pathActualImage . replace ( "actual" , "diff" ) ;
270262
271263 // await this.prepareImageToCompare(pathActualImage, options.cropRectangle);
272- let result = await this . compareImages ( pathActualImage , pathExpectedImage , pathDiffImage , options . tolerance , options . toleranceType ) ;
264+ let result = await this . compareImages ( options , pathActualImage , pathExpectedImage , pathDiffImage ) ;
273265
274266 // Iterate
275267 if ( ! result ) {
@@ -283,7 +275,7 @@ export class ImageHelper {
283275 await this . clipRectangleImage ( options . cropRectangle , pathActualImage ) ;
284276 }
285277 // await this.prepareImageToCompare(pathActualImage, this.imageCropRect);
286- result = await this . compareImages ( pathActualImage , pathExpectedImage , pathDiffImage , options . tolerance , options . toleranceType ) ;
278+ result = await this . compareImages ( options , pathActualImage , pathExpectedImage , pathDiffImage ) ;
287279 if ( ! result && checkImageLogType ( this . _args . testReporter , LogImageType . everyImage ) ) {
288280 this . _args . testReporterLog ( `Actual image: ${ basename ( pathActualImage ) . replace ( / \. \w { 3 , 3 } $ / ig, "" ) } ` ) ;
289281 this . _args . testReporterLog ( join ( this . _args . reportsPath , basename ( pathActualImage ) ) ) ;
@@ -314,12 +306,12 @@ export class ImageHelper {
314306 return result ;
315307 }
316308
317- public compareImages ( actual : string , expected : string , output : string , tolerance : number , toleranceType : ImageOptions ) {
309+ public compareImages ( options : IImageCompareOptions , actual : string , expected : string , output : string ) {
318310 const clipRect = {
319- x : this . imageCropRect . x ,
320- y : this . imageCropRect . y ,
321- width : this . imageCropRect . width ,
322- height : this . imageCropRect . height
311+ x : this . options . cropRectangle . x ,
312+ y : this . options . cropRectangle . y ,
313+ width : this . options . cropRectangle . width ,
314+ height : this . options . cropRectangle . height
323315 }
324316
325317 if ( ! this . options . keepOriginalImageSize ) {
@@ -334,22 +326,22 @@ export class ImageHelper {
334326 imageBPath : expected ,
335327 imageOutputPath : output ,
336328 imageOutputLimit : this . imageOutputLimit ,
337- thresholdType : toleranceType ,
338- threshold : tolerance ,
329+ thresholdType : options . toleranceType ,
330+ threshold : options . tolerance ,
339331 delta : this . delta ,
340332 cropImageA : clipRect ,
341333 cropImageB : clipRect ,
342334 blockOut : this . _blockOutAreas ,
343335 verbose : this . _args . verbose ,
344336 } ) ;
345337
346- if ( toleranceType == ImageOptions . percent ) {
347- if ( tolerance >= 1 ) {
338+ if ( options . toleranceType == ImageOptions . percent ) {
339+ if ( options . tolerance >= 1 ) {
348340 logError ( "Tolerance range is from 0 to 1 -> percentage thresholds: 1 = 100%, 0.2 = 20%" ) ;
349341 }
350- console . log ( `Using ${ tolerance * 100 } % tolerance` ) ;
342+ console . log ( `Using ${ options . tolerance * 100 } % tolerance` ) ;
351343 } else {
352- console . log ( `Using ${ tolerance } px tolerance` ) ;
344+ console . log ( `Using ${ options . tolerance } px tolerance` ) ;
353345 }
354346
355347 const result = this . runDiff ( diff , output ) ;
@@ -361,26 +353,34 @@ export class ImageHelper {
361353 let imageToClip : PngJsImage ;
362354 imageToClip = await this . readImage ( path ) ;
363355 let shouldExit = false ;
364- Object . getOwnPropertyNames ( rect ) . forEach ( prop => {
365- if ( rect [ prop ] === undefined || rect [ prop ] === null ) {
366- shouldExit = true ;
367- return ;
368- }
369- } ) ;
356+ if ( ! isNumber ( rect [ "x" ] )
357+ || ! isNumber ( rect [ "y" ] )
358+ || ! isNumber ( rect [ "width" ] )
359+ || ! isNumber ( rect [ "height" ] ) ) {
360+ shouldExit = true ;
361+ }
370362 if ( shouldExit ) {
371- logError ( `Could not crop the image. Not enough data` , rect ) ;
372- return
363+ logError ( `Could not crop the image. Not enough data {x: ${ rect [ "x" ] } , y: ${ rect [ "y" ] } , width: ${ rect [ "width" ] } , height: ${ rect [ "height" ] } }` ) ;
373364 }
374- imageToClip . clip ( rect . x , rect . y , rect . width , rect . height ) ;
375- return new Promise ( ( resolve , reject ) => {
376- imageToClip . writeImage ( path , ( err ) => {
377- if ( err ) {
378- return reject ( err ) ;
379- }
380- return resolve ( ) ;
381- } ) ;
382365
383- } )
366+ if ( ! shouldExit ) {
367+ imageToClip . clip ( rect . x , rect . y , rect . width , rect . height ) ;
368+ } else {
369+ logWarn ( "Image will not be cropped!" )
370+ return true ;
371+ }
372+ return new Promise ( ( resolve , reject ) => {
373+ try {
374+ imageToClip . writeImage ( path , ( err ) => {
375+ if ( err ) {
376+ return reject ( err ) ;
377+ }
378+ return resolve ( ) ;
379+ } ) ;
380+ } catch ( error ) {
381+ logError ( error ) ;
382+ }
383+ } ) ;
384384 }
385385
386386 public readImage ( path : string ) : Promise < any > {
@@ -471,10 +471,6 @@ export class ImageHelper {
471471 }
472472 } ) ;
473473
474- if ( ! options . cropRectangle ) {
475- ImageHelper . fullClone ( this . imageCropRect , options . cropRectangle ) ;
476- }
477-
478474 return options ;
479475 }
480476
0 commit comments