Skip to content

feat: add the ability to run adb shell commands #251

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
merged 3 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
16 changes: 16 additions & 0 deletions lib/appium-driver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,20 @@ export declare class AppiumDriver {
* This is convenient to use for some gestures on the screen
*/
getScreenViewPort(): IRectangle;
/**
* Android ONLY! Input key event via ADB.
* @param keyEvent The event number
*/
adbKeyEvent(keyEvent: number): Promise<void>;
/**
* Android ONLY! Send text via ADB.
* @param text The string to send
*/
adbSendText(text: string): Promise<void>;
/**
* Android ONLY! Execute shell command via ADB.
* @param command The command name
* @param args Additional arguments
*/
adbShellCommand(command: string, args: Array<any>): Promise<void>;
}
28 changes: 27 additions & 1 deletion lib/appium-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import {
getStorage,
encodeImageToBase64,
ensureReportsDirExists,
checkImageLogType
checkImageLogType,
adbShellCommand
} from "./utils";

import { INsCapabilities } from "./interfaces/ns-capabilities";
Expand Down Expand Up @@ -1022,4 +1023,29 @@ export class AppiumDriver {
}
}
}

/**
* Android ONLY! Input key event via ADB.
* @param keyEvent The event number
*/
public async adbKeyEvent(keyEvent: number) {
await this.adbShellCommand("input", ["keyevent", keyEvent]);
}

/**
* Android ONLY! Send text via ADB.
* @param text The string to send
*/
public async adbSendText(text: string) {
await this.adbShellCommand("input", ["text", text]);
}

/**
* Android ONLY! Execute shell command via ADB.
* @param command The command name
* @param args Additional arguments
*/
public async adbShellCommand(command: string, args: Array<any>) {
await adbShellCommand(this._driver, command, args);
}
}
10 changes: 8 additions & 2 deletions lib/ui-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ export declare class UIElement {
* Send keys to field or other UI component
* @param text
* @param shouldClearText, default value is true
* @param useAdb, default value is false. Usable for Android ONLY !
*/
sendKeys(text: string, shouldClearText?: boolean): Promise<void>;
sendKeys(text: string, shouldClearText?: boolean, useAdb?: boolean): Promise<void>;
/**
* Type text to field or other UI component
* @param text
Expand All @@ -142,9 +143,14 @@ export declare class UIElement {
*/
pressKeycode(keyCode: number): Promise<void>;
/**
* Clears text form ui element
* Clears text from ui element
*/
clearText(): Promise<void>;
/**
* Clears text from ui element with ADB. Android ONLY !
* @param charactersCount Characters count to delete. (Optional - default value 10)
*/
adbDeleteText(charactersCount?: number): Promise<void>;
log(): Promise<void>;
refetch(): Promise<any>;
/**
Expand Down
35 changes: 28 additions & 7 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 } from "./utils";
import { calculateOffset, adbShellCommand } from "./utils";
import { AndroidKeyEvent } from "mobile-devices-controller";

export class UIElement {
Expand Down Expand Up @@ -382,12 +382,21 @@ export class UIElement {
* Send keys to field or other UI component
* @param text
* @param shouldClearText, default value is true
* @param useAdb, default value is false. Usable for Android ONLY !
*/
public async sendKeys(text: string, shouldClearText: boolean = true) {
if (shouldClearText) {
await this.clearText();
public async sendKeys(text: string, shouldClearText: boolean = true, useAdb: boolean = false) {
if (useAdb && this._args.isAndroid) {
if (shouldClearText) {
await this.adbDeleteText();
}
await this.click();
await adbShellCommand(this._driver, "input", ["text", text]);
} else {
if (shouldClearText) {
await this.clearText();
}
await this._element.sendKeys(text);
}
await this._element.sendKeys(text);
}

/**
Expand All @@ -407,17 +416,29 @@ export class UIElement {
* @param key code
*/
public async pressKeycode(keyCode: number) {
await this._driver.pressKeycode(keyCode);
await this._driver.pressKeyCode(keyCode);
}

/**
* Clears text form ui element
* Clears text from ui element
*/
public async clearText() {
await this.click();
await this._element.clear();
}

/**
* Clears text from ui element with ADB. Android ONLY !
* @param charactersCount Characters count to delete. (Optional - default value 10)
*/
public async adbDeleteText(charactersCount: number = 10) {
await this.click();
for (let index = 0; index < charactersCount; index++) {
// Keyevent 67 Delete (backspace)
await adbShellCommand(this._driver, "input", ["keyevent", AndroidKeyEvent.KEYCODE_DEL]);
}
}

public async log() {
const el = await this.element();
console.dir(el);
Expand Down
1 change: 1 addition & 0 deletions lib/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export declare function logWarn(info: any, obj?: any): void;
export declare function logError(info: any, obj?: any): void;
export declare function log(message: any, verbose: any): void;
export declare const logColorized: (bgColor: ConsoleColor, frontColor: ConsoleColor, info: any) => void;
export declare function adbShellCommand(wd: any, command: string, args: Array<any>): Promise<void>;
declare enum ConsoleColor {
Reset = "\u001B[0m",
Bright = "\u001B[1m",
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,11 @@ export const logColorized = (bgColor: ConsoleColor, frontColor: ConsoleColor, in
console.log(`${ConsoleColor.BgYellow}${ConsoleColor.FgBlack}%s${ConsoleColor.Reset}`, info);
}


export async function adbShellCommand(wd: any, command: string, args: Array<any>) {
await wd.execute('mobile: shell', {"command": command, "args": args});
}

enum ConsoleColor {
Reset = "\x1b[0m",
Bright = "\x1b[1m",
Expand Down