@@ -9,7 +9,7 @@ import { AppiumDriver } from "./appium-driver";
9
9
import { logError , checkImageLogType , resolvePath , copy , addExt , logWarn } from "./utils" ;
10
10
import { unlinkSync , existsSync , mkdirSync } from "fs" ;
11
11
import { basename , join } from "path" ;
12
- import { isObject } from "util" ;
12
+ import { isObject , isNumber } from "util" ;
13
13
import { logInfo } from "mobile-devices-controller/lib/utils" ;
14
14
15
15
export interface IImageCompareOptions {
@@ -82,7 +82,6 @@ export interface IImageCompareOptions {
82
82
export class ImageHelper {
83
83
private _blockOutAreas : IRectangle [ ] ;
84
84
private _imagesResults = new Map < string , boolean > ( ) ;
85
- private _imageCropRect : IRectangle ;
86
85
private _options : IImageCompareOptions = { } ;
87
86
private _defaultOptions : IImageCompareOptions = {
88
87
timeOutSeconds : 2 ,
@@ -101,12 +100,14 @@ export class ImageHelper {
101
100
constructor ( private _args : INsCapabilities , private _driver : AppiumDriver ) {
102
101
this . _defaultOptions . cropRectangle = ( this . _args . appiumCaps && this . _args . appiumCaps . viewportRect ) || this . _args . device . viewportRect ;
103
102
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 ) ) {
107
104
this . _defaultOptions . cropRectangle = this . _defaultOptions . cropRectangle || { } ;
108
105
this . _defaultOptions . cropRectangle . y = this . _args . device . config . offsetPixels || 0 ;
109
106
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
+ }
110
111
}
111
112
ImageHelper . fullClone ( this . _defaultOptions , this . _options ) ;
112
113
@@ -137,14 +138,6 @@ export class ImageHelper {
137
138
this . _options = this . extendOptions ( options ) ;
138
139
}
139
140
140
- get imageCropRect ( ) : IRectangle {
141
- return this . _imageCropRect || this . options . cropRectangle ;
142
- }
143
-
144
- set imageCropRect ( clipRectangle : IRectangle ) {
145
- this . _imageCropRect = clipRectangle ;
146
- }
147
-
148
141
get blockOutAreas ( ) {
149
142
return this . _blockOutAreas ;
150
143
}
@@ -257,7 +250,6 @@ export class ImageHelper {
257
250
// First time capture
258
251
if ( ! existsSync ( pathExpectedImage ) ) {
259
252
await captureFirstImage ( ) ;
260
-
261
253
return false ;
262
254
}
263
255
@@ -269,7 +261,7 @@ export class ImageHelper {
269
261
const pathDiffImage = pathActualImage . replace ( "actual" , "diff" ) ;
270
262
271
263
// 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 ) ;
273
265
274
266
// Iterate
275
267
if ( ! result ) {
@@ -283,7 +275,7 @@ export class ImageHelper {
283
275
await this . clipRectangleImage ( options . cropRectangle , pathActualImage ) ;
284
276
}
285
277
// 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 ) ;
287
279
if ( ! result && checkImageLogType ( this . _args . testReporter , LogImageType . everyImage ) ) {
288
280
this . _args . testReporterLog ( `Actual image: ${ basename ( pathActualImage ) . replace ( / \. \w { 3 , 3 } $ / ig, "" ) } ` ) ;
289
281
this . _args . testReporterLog ( join ( this . _args . reportsPath , basename ( pathActualImage ) ) ) ;
@@ -314,12 +306,12 @@ export class ImageHelper {
314
306
return result ;
315
307
}
316
308
317
- public compareImages ( actual : string , expected : string , output : string , tolerance : number , toleranceType : ImageOptions ) {
309
+ public compareImages ( options : IImageCompareOptions , actual : string , expected : string , output : string ) {
318
310
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
323
315
}
324
316
325
317
if ( ! this . options . keepOriginalImageSize ) {
@@ -334,22 +326,22 @@ export class ImageHelper {
334
326
imageBPath : expected ,
335
327
imageOutputPath : output ,
336
328
imageOutputLimit : this . imageOutputLimit ,
337
- thresholdType : toleranceType ,
338
- threshold : tolerance ,
329
+ thresholdType : options . toleranceType ,
330
+ threshold : options . tolerance ,
339
331
delta : this . delta ,
340
332
cropImageA : clipRect ,
341
333
cropImageB : clipRect ,
342
334
blockOut : this . _blockOutAreas ,
343
335
verbose : this . _args . verbose ,
344
336
} ) ;
345
337
346
- if ( toleranceType == ImageOptions . percent ) {
347
- if ( tolerance >= 1 ) {
338
+ if ( options . toleranceType == ImageOptions . percent ) {
339
+ if ( options . tolerance >= 1 ) {
348
340
logError ( "Tolerance range is from 0 to 1 -> percentage thresholds: 1 = 100%, 0.2 = 20%" ) ;
349
341
}
350
- console . log ( `Using ${ tolerance * 100 } % tolerance` ) ;
342
+ console . log ( `Using ${ options . tolerance * 100 } % tolerance` ) ;
351
343
} else {
352
- console . log ( `Using ${ tolerance } px tolerance` ) ;
344
+ console . log ( `Using ${ options . tolerance } px tolerance` ) ;
353
345
}
354
346
355
347
const result = this . runDiff ( diff , output ) ;
@@ -361,26 +353,34 @@ export class ImageHelper {
361
353
let imageToClip : PngJsImage ;
362
354
imageToClip = await this . readImage ( path ) ;
363
355
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
+ }
370
362
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" ] } }` ) ;
373
364
}
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
- } ) ;
382
365
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
+ } ) ;
384
384
}
385
385
386
386
public readImage ( path : string ) : Promise < any > {
@@ -471,10 +471,6 @@ export class ImageHelper {
471
471
}
472
472
} ) ;
473
473
474
- if ( ! options . cropRectangle ) {
475
- ImageHelper . fullClone ( this . imageCropRect , options . cropRectangle ) ;
476
- }
477
-
478
474
return options ;
479
475
}
480
476
0 commit comments