Skip to content

Commit 10b1342

Browse files
author
Zdravko
authored
chore: switch tolerance of compare methods to percent and prevent cre… (#91)
* chore: switch tolerance of compare methods to percent and prevent creating image for sauce labs runs. * fix: a typo and do small refactoring * chore: bump version
1 parent c166c49 commit 10b1342

7 files changed

+38
-23
lines changed

lib/appium-driver.d.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { INsCapabilities } from "./interfaces/ns-capabilities";
88
import { IRectangle } from "./interfaces/rectangle";
99
import { Point } from "./point";
1010
import { ImageHelper } from "./image-helper";
11+
import { ImageOptions } from "./image-options";
1112
export declare class AppiumDriver {
1213
private _driver;
1314
private _wd;
@@ -24,7 +25,7 @@ export declare class AppiumDriver {
2425
private _logPath;
2526
private _storageByDeviceName;
2627
private _storageByPlatform;
27-
private constructor();
28+
private constructor(_driver, _wd, _webio, _driverConfig, _args);
2829
readonly imageHelper: ImageHelper;
2930
defaultWaitTime: number;
3031
readonly capabilities: any;
@@ -128,10 +129,10 @@ export declare class AppiumDriver {
128129
clickPoint(xCoordinate: number, yCoordinate: number): Promise<void>;
129130
source(): Promise<any>;
130131
sessionId(): Promise<any>;
131-
compareElement(element: UIElement, imageName: string): Promise<boolean>;
132-
compareRectangle(rect: IRectangle, imageName: string, timeOutSeconds?: number, tollerance?: number): Promise<boolean>;
133-
compareScreen(imageName: string, timeOutSeconds?: number, tollerance?: number): Promise<boolean>;
134-
private compare(imageName, timeOutSeconds?, tollerance?, rect?);
132+
compareElement(element: UIElement, imageName: string, tolerance?: number, timeOutSeconds?: number, toleranceType?: ImageOptions): Promise<boolean>;
133+
compareRectangle(rect: IRectangle, imageName: string, timeOutSeconds?: number, tolerance?: number, toleranceType?: ImageOptions): Promise<boolean>;
134+
compareScreen(imageName: string, timeOutSeconds?: number, tolerance?: number, toleranceType?: ImageOptions): Promise<boolean>;
135+
private compare(imageName, timeOutSeconds?, tolerance?, rect?, toleranceType?);
135136
prepareImageToCompare(filePath: string, rect: IRectangle): Promise<void>;
136137
takeScreenshot(fileName: string): Promise<string>;
137138
logScreenshot(fileName: string): Promise<string>;

lib/appium-driver.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,19 @@ export class AppiumDriver {
286286
return await this.driver.getSessionId();
287287
}
288288

289-
public async compareElement(element: UIElement, imageName: string, ) {
290-
return await this.compareRectangle(await element.getActualRectangle(), imageName);
289+
public async compareElement(element: UIElement, imageName: string, tolerance: number = 0.01, timeOutSeconds: number = 3, toleranceType?: ImageOptions) {
290+
return await this.compareRectangle(await element.getActualRectangle(), imageName, timeOutSeconds, tolerance, toleranceType);
291291
}
292292

293-
public async compareRectangle(rect: IRectangle, imageName: string, timeOutSeconds: number = 3, tollerance: number = 0.01) {
294-
return await this.compare(imageName, timeOutSeconds, tollerance, rect);
293+
public async compareRectangle(rect: IRectangle, imageName: string, timeOutSeconds: number = 3, tolerance: number = 0.01, toleranceType?: ImageOptions) {
294+
return await this.compare(imageName, timeOutSeconds, tolerance, rect, toleranceType);
295295
}
296296

297-
public async compareScreen(imageName: string, timeOutSeconds: number = 3, tollerance: number = 0.01) {
298-
return await this.compare(imageName, timeOutSeconds, tollerance);
297+
public async compareScreen(imageName: string, timeOutSeconds: number = 3, tolerance: number = 0.01, toleranceType?: ImageOptions) {
298+
return await this.compare(imageName, timeOutSeconds, tolerance, undefined, toleranceType);
299299
}
300300

301-
private async compare(imageName: string, timeOutSeconds: number = 3, tollerance: number = 0.01, rect?: IRectangle) {
301+
private async compare(imageName: string, timeOutSeconds: number = 3, tolerance: number = 0.01, rect?: IRectangle, toleranceType?: ImageOptions) {
302302

303303
if (!this._logPath) {
304304
this._logPath = getReportPath(this._args);
@@ -326,7 +326,7 @@ export class AppiumDriver {
326326
const pathDiffImage = pathActualImage.replace("actual", "diff");
327327

328328
await this.prepareImageToCompare(pathActualImage, rect);
329-
let result = await this._imageHelper.compareImages(pathActualImage, pathExpectedImage, pathDiffImage, tollerance);
329+
let result = await this._imageHelper.compareImages(pathActualImage, pathExpectedImage, pathDiffImage, tolerance, toleranceType);
330330

331331
// Iterate
332332
if (!result) {
@@ -338,7 +338,7 @@ export class AppiumDriver {
338338
pathActualImage = await this.takeScreenshot(pathActualImageConter);
339339

340340
await this.prepareImageToCompare(pathActualImage, rect);
341-
result = await this._imageHelper.compareImages(pathActualImage, pathExpectedImage, pathDiffImage, tollerance);
341+
result = await this._imageHelper.compareImages(pathActualImage, pathExpectedImage, pathDiffImage, tolerance, toleranceType);
342342
counter++;
343343
}
344344
} else {

lib/device-controller.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ export class DeviceManger implements IDeviceManager {
159159
}
160160

161161
private static getDefaultDevice(args) {
162-
return new Device(args.appiumCaps.deviceName, args.appiumCaps.platformVersion, undefined, args.appiumCaps.platformName, undefined, undefined);
162+
let device = new Device(args.appiumCaps.deviceName, args.appiumCaps.platformVersion, undefined, args.appiumCaps.platformName, undefined, undefined);
163+
device.config = { "density": args.appiumCaps.density, "offsetPixels": args.appiumCaps.offsetPixels };
164+
return device;
163165
}
164166
}

lib/image-helper.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export declare class ImageHelper {
1010
blockOutAreas: IRectangle[];
1111
imageOutputLimit(): ImageOptions;
1212
thresholdType(): ImageOptions;
13-
threshold(): number;
13+
threshold(thresholdType: any): 10 | 0.01;
1414
delta(): number;
1515
static cropImageDefault(_args: INsCapabilities): {
1616
x: number;

lib/image-helper.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ export class ImageHelper {
3434
}
3535

3636
public thresholdType() {
37-
return ImageOptions.pixel;
37+
return ImageOptions.percent;
3838
}
3939

40-
public threshold() {
41-
return 0.1; // 0.1 percent; 500 percent
40+
public threshold(thresholdType) {
41+
if (thresholdType == ImageOptions.percent) {
42+
return 0.01; // 0.01 = 1 percent; 500 percent
43+
} else {
44+
return 10; // 10 pixels
45+
}
4246
}
4347

4448
public delta() {
@@ -78,7 +82,7 @@ export class ImageHelper {
7882
});
7983
}
8084

81-
public compareImages(actual: string, expected: string, output: string, valueThreshold: number = this.threshold(), typeThreshold: any = ImageOptions.pixel) {
85+
public compareImages(actual: string, expected: string, output: string, valueThreshold: number = this.threshold(this.thresholdType()), typeThreshold: any = this.thresholdType()) {
8286
const diff = new BlinkDiff({
8387

8488
imageAPath: actual,
@@ -95,6 +99,11 @@ export class ImageHelper {
9599
verbose: this._args.verbose,
96100
});
97101

102+
if (typeThreshold == ImageOptions.percent) {
103+
valueThreshold = Math.floor(valueThreshold * 100);
104+
}
105+
106+
console.log("Using " + valueThreshold + " " + typeThreshold + "s tolerance");
98107
const result = this.runDiff(diff, output);
99108
this._blockOutAreas = undefined;
100109
return result;

lib/utils.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export function isWin() {
236236

237237
export function getStorageByDeviceName(args: INsCapabilities) {
238238
let storage = getStorage(args);
239-
const appName = getAppName(args);
239+
const appName = resolveSauceLabAppName(getAppName(args));
240240
storage = createStorageFolder(storage, appName);
241241
storage = createStorageFolder(storage, args.appiumCaps.deviceName);
242242

@@ -250,7 +250,7 @@ export function getStorageByPlatform(args: INsCapabilities) {
250250
storage = createStorageFolder(storage, "images");
251251
}
252252

253-
const appName = getAppName(args);
253+
const appName = resolveSauceLabAppName(getAppName(args));
254254
storage = createStorageFolder(storage, appName);
255255
storage = createStorageFolder(storage, args.appiumCaps.platformName.toLowerCase());
256256

@@ -428,3 +428,6 @@ export const findFreePort = async (retries: number = 10, port: number = 3000, ar
428428
}
429429
return p;
430430
}
431+
function resolveSauceLabAppName(appName: string) {
432+
return appName.includes("sauce-storage:") ? appName.replace("sauce-storage:", "") : appName;
433+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-dev-appium",
3-
"version": "3.1.0",
3+
"version": "3.2.0",
44
"description": "A NativeScript plugin to help integrate and run Appium tests",
55
"author": "NativeScript",
66
"license": "MIT",

0 commit comments

Comments
 (0)