Skip to content

Commit 50d6665

Browse files
fix: prevent crashing in case device density is missing
1 parent 0d832c4 commit 50d6665

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Diff for: lib/appium-driver.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1001,22 +1001,27 @@ export class AppiumDriver {
10011001
* Useful for image comparison
10021002
*/
10031003
public getScreenActualViewPort(): IRectangle {
1004-
return <IRectangle>(this._args.appiumCaps && this._args.appiumCaps.viewportRect) || this._args.device.viewportRect;
1004+
return <IRectangle>(this._args.device.viewportRect || this.imageHelper.options.cropRectangle);
10051005
}
10061006

10071007
/**
1008-
* Get screen view port
1009-
* This is convenient to use for some gestures on the screen
1008+
* Get screen view port.
1009+
* Provides the view port that is needed for some gestures like swipe etc.
10101010
*/
10111011
public getScreenViewPort(): IRectangle {
1012-
const rect = (this._args.appiumCaps && this._args.appiumCaps.viewportRect) || this._args.device.viewportRect;
1013-
if (rect && Object.getOwnPropertyNames(rect).length > 0) {
1012+
const rect = this.getScreenActualViewPort();
1013+
if (rect
1014+
&& Object.getOwnPropertyNames(rect).length > 0
1015+
&& this._args.appiumCaps.device.deviceScreenDensity) {
10141016
return <IRectangle>{
10151017
x: rect.x / this._args.appiumCaps.device.deviceScreenDensity,
10161018
y: rect.y / this._args.appiumCaps.device.deviceScreenDensity,
1017-
width: rect.x / this._args.appiumCaps.device.deviceScreenDensity,
1018-
height: rect.x / this._args.appiumCaps.device.deviceScreenDensity,
1019+
width: rect.width / this._args.appiumCaps.device.deviceScreenDensity,
1020+
height: rect.height / this._args.appiumCaps.device.deviceScreenDensity,
10191021
}
1022+
} else {
1023+
logError("Device's density is undefined!");
1024+
return rect;
10201025
}
10211026
}
10221027

Diff for: lib/ui-element.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Point } from "./point";
22
import { Direction } from "./direction";
33
import { INsCapabilities } from "./interfaces/ns-capabilities";
44
import { AutomationName } from "./automation-name";
5-
import { calculateOffset, adbShellCommand } from "./utils";
5+
import { calculateOffset, adbShellCommand, logError } from "./utils";
66
import { AndroidKeyEvent } from "mobile-devices-controller";
77

88
export class UIElement {
@@ -249,7 +249,7 @@ export class UIElement {
249249
actRect.width *= density;
250250
actRect.height *= density;
251251
} else {
252-
throw new Error("Device's density is undefined!");
252+
logError("Device's density is undefined!");
253253
}
254254
return actRect;
255255
}

0 commit comments

Comments
 (0)