Skip to content

Commit 91715d1

Browse files
author
Vasil Chimev
authored
refactor(image-comparison): calculate properly rectangle for iOS (#73)
* refactor(image-comparison): calculate properly rectangle for iOS * refactor(image-comparison): add method to get actual rectangle dimensions
1 parent e95c6ad commit 91715d1

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

lib/appium-driver.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export declare class AppiumDriver {
123123
source(): Promise<any>;
124124
sessionId(): Promise<any>;
125125
compareElement(element: UIElement, imageName: string): Promise<boolean>;
126-
compareRectangles(rect: IRectangle, imageName: string, timeOutSeconds?: number, tollerance?: number): Promise<boolean>;
126+
compareRectangle(rect: IRectangle, imageName: string, timeOutSeconds?: number, tollerance?: number): Promise<boolean>;
127127
compareScreen(imageName: string, timeOutSeconds?: number, tollerance?: number): Promise<boolean>;
128128
private compare(imageName, timeOutSeconds?, tollerance?, rect?);
129129
prepareImageToCompare(filePath: string, rect: IRectangle): Promise<void>;

lib/appium-driver.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class AppiumDriver {
122122
*/
123123
public async findElementByXPath(xPath: string, waitForElement: number = this.defaultWaitTime) {
124124
const searchM = "waitForElementByXPath";
125-
return await new UIElement(await this._driver.waitForElementByXPath(xPath, waitForElement), this._driver, this._wd, this._webio, searchM, xPath);
125+
return await new UIElement(await this._driver.waitForElementByXPath(xPath, waitForElement), this._driver, this._wd, this._webio, this._args, searchM, xPath);
126126
}
127127

128128
/**
@@ -163,7 +163,7 @@ export class AppiumDriver {
163163
* @param waitForElement
164164
*/
165165
public async findElementByClassName(className: string, waitForElement: number = this.defaultWaitTime) {
166-
return new UIElement(await this._driver.waitForElementByClassName(className, waitForElement), this._driver, this._wd, this._webio, "waitForElementByClassName", className);
166+
return new UIElement(await this._driver.waitForElementByClassName(className, waitForElement), this._driver, this._wd, this._webio, this._args, "waitForElementByClassName", className);
167167
}
168168

169169
/**
@@ -182,7 +182,7 @@ export class AppiumDriver {
182182
* @param waitForElement
183183
*/
184184
public async findElementByAccessibilityId(id, waitForElement: number = this.defaultWaitTime) {
185-
return new UIElement(await this._driver.waitForElementByAccessibilityId(id, waitForElement), this._driver, this._wd, this._webio, "waitForElementByAccessibilityId", id);
185+
return new UIElement(await this._driver.waitForElementByAccessibilityId(id, waitForElement), this._driver, this._wd, this._webio, this._args, "waitForElementByAccessibilityId", id);
186186
}
187187

188188
/**
@@ -271,10 +271,10 @@ export class AppiumDriver {
271271
}
272272

273273
public async compareElement(element: UIElement, imageName: string, ) {
274-
return await this.compareRectangles(await element.getRectangle(), imageName);
274+
return await this.compareRectangle(await element.getActualRectangle(), imageName);
275275
}
276276

277-
public async compareRectangles(rect: IRectangle, imageName: string, timeOutSeconds: number = 3, tollerance: number = 0.01) {
277+
public async compareRectangle(rect: IRectangle, imageName: string, timeOutSeconds: number = 3, tollerance: number = 0.01) {
278278
return await this.compare(imageName, timeOutSeconds, tollerance, rect);
279279
}
280280

@@ -452,7 +452,7 @@ export class AppiumDriver {
452452
return arrayOfUIElements;
453453
}
454454
array.forEach(async element => {
455-
arrayOfUIElements.push(new UIElement(await element, this._driver, this._wd, this._webio, searchM, args, i));
455+
arrayOfUIElements.push(new UIElement(await element, this._driver, this._wd, this._webio, this._args, searchM, args, i));
456456
i++;
457457
});
458458

lib/ui-element.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { Point } from "./point";
22
import { Direction } from "./direction";
3+
import { INsCapabilities } from "./interfaces/ns-capabilities";
34
export declare class UIElement {
45
private _element;
56
private _driver;
67
private _wd;
78
private _webio;
9+
private _args;
810
private _searchMethod;
911
private _searchParams;
1012
private _index;
11-
constructor(_element: any, _driver: any, _wd: any, _webio: any, _searchMethod: string, _searchParams: string, _index?: number);
13+
constructor(_element: any, _driver: any, _wd: any, _webio: any, _args: INsCapabilities, _searchMethod: string, _searchParams: string, _index?: number);
1214
/**
1315
* Click on element
1416
*/
@@ -69,6 +71,15 @@ export declare class UIElement {
6971
width: number;
7072
height: number;
7173
}>;
74+
/**
75+
* Get rectangle of element in actual dimensions
76+
*/
77+
getActualRectangle(): Promise<{
78+
x: number;
79+
y: number;
80+
width: number;
81+
height: number;
82+
}>;
7283
/**
7384
* Scroll with offset from elemnt with minimum inertia
7485
* @param direction

lib/ui-element.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { Point } from "./point";
22
import { Direction } from "./direction";
3+
import { INsCapabilities } from "./interfaces/ns-capabilities";
34
import { IRectangle } from "./interfaces/rectangle";
45
import { calculateOffset } from "./utils";
56

67
export class UIElement {
7-
constructor(private _element: any, private _driver: any, private _wd: any, private _webio: any, private _searchMethod: string, private _searchParams: string, private _index?: number) { }
8+
constructor(private _element: any,
9+
private _driver: any,
10+
private _wd: any,
11+
private _webio: any,
12+
private _args: INsCapabilities,
13+
private _searchMethod: string,
14+
private _searchParams: string,
15+
private _index?: number
16+
) { }
817

918
/**
1019
* Click on element
@@ -109,6 +118,21 @@ export class UIElement {
109118
return rect;
110119
}
111120

121+
/**
122+
* Get rectangle of element in actual dimensions
123+
*/
124+
public async getActualRectangle() {
125+
const actRect = await this.getRectangle();
126+
if (this._args.isIOS) {
127+
const density = this._args.device.config.density;
128+
actRect.x *= density;
129+
actRect.y *= density;
130+
actRect.width *= density;
131+
actRect.height *= density;
132+
}
133+
return actRect;
134+
}
135+
112136
/**
113137
* Scroll with offset from elemnt with minimum inertia
114138
* @param direction

0 commit comments

Comments
 (0)