Skip to content

fix: prevent crashing in case device density is missing #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/appium-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,22 +1001,27 @@ export class AppiumDriver {
* Useful for image comparison
*/
public getScreenActualViewPort(): IRectangle {
return <IRectangle>(this._args.appiumCaps && this._args.appiumCaps.viewportRect) || this._args.device.viewportRect;
return <IRectangle>(this._args.device.viewportRect || this.imageHelper.options.cropRectangle);
}

/**
* Get screen view port
* This is convenient to use for some gestures on the screen
* Get screen view port.
* Provides the view port that is needed for some gestures like swipe etc.
*/
public getScreenViewPort(): IRectangle {
const rect = (this._args.appiumCaps && this._args.appiumCaps.viewportRect) || this._args.device.viewportRect;
if (rect && Object.getOwnPropertyNames(rect).length > 0) {
const rect = this.getScreenActualViewPort();
if (rect
&& Object.getOwnPropertyNames(rect).length > 0
&& this._args.appiumCaps.device.deviceScreenDensity) {
return <IRectangle>{
x: rect.x / this._args.appiumCaps.device.deviceScreenDensity,
y: rect.y / this._args.appiumCaps.device.deviceScreenDensity,
width: rect.x / this._args.appiumCaps.device.deviceScreenDensity,
height: rect.x / this._args.appiumCaps.device.deviceScreenDensity,
width: rect.width / this._args.appiumCaps.device.deviceScreenDensity,
height: rect.height / this._args.appiumCaps.device.deviceScreenDensity,
}
} else {
logError("Device's density is undefined!");
return rect;
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ui-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Point } from "./point";
import { Direction } from "./direction";
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { AutomationName } from "./automation-name";
import { calculateOffset, adbShellCommand } from "./utils";
import { calculateOffset, adbShellCommand, logError } from "./utils";
import { AndroidKeyEvent } from "mobile-devices-controller";

export class UIElement {
Expand Down Expand Up @@ -249,7 +249,7 @@ export class UIElement {
actRect.width *= density;
actRect.height *= density;
} else {
throw new Error("Device's density is undefined!");
logError("Device's density is undefined!");
}
return actRect;
}
Expand Down