appium-android-driver
Version:
Android UiAutomator and Chrome support for Appium
59 lines • 2.63 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mobileShell = mobileShell;
const support_1 = require("@appium/support");
const driver_1 = require("appium/driver");
const lodash_1 = __importDefault(require("lodash"));
const teen_process_1 = require("teen_process");
const utils_1 = require("../utils");
/**
* Executes a shell command on the device via ADB.
*
* This method runs an arbitrary shell command on the Android device and returns
* the output. The command is executed using `adb shell` with the specified
* command and arguments.
*
* Requirements:
* - The adb_shell feature must be enabled
*
* @param command The shell command to execute (e.g., 'pm', 'dumpsys', 'getprop').
* @param args Optional array of command arguments.
* @param timeout The maximum time in milliseconds to wait for command execution.
* Defaults to 20000ms (20 seconds).
* @param includeStderr If `true`, returns both stdout and stderr in an object.
* If `false` or undefined, returns only stdout as a string.
* @returns If `includeStderr` is `true`, returns `{ stdout: string, stderr: string }`.
* Otherwise, returns the command output as a string.
* @throws {errors.InvalidArgumentError} If `command` is not a string.
* @throws {Error} If the command execution fails or times out.
*/
async function mobileShell(command, args = [], timeout = 20000, includeStderr) {
this.assertFeatureEnabled(utils_1.ADB_SHELL_FEATURE);
if (!lodash_1.default.isString(command)) {
throw new driver_1.errors.InvalidArgumentError(`The 'command' argument is mandatory`);
}
const adbArgs = [...this.adb.executable.defaultArgs, 'shell', command, ...lodash_1.default.castArray(args)];
this.log.debug(`Running '${this.adb.executable.path} ${support_1.util.quote(adbArgs)}'`);
try {
const { stdout, stderr } = await (0, teen_process_1.exec)(this.adb.executable.path, adbArgs, { timeout });
if (includeStderr) {
// @ts-ignore We know what we are doing here
return {
stdout,
stderr,
};
}
// @ts-ignore We know what we are doing here
return stdout;
}
catch (e) {
const err = e;
throw this.log.errorWithException(`Cannot execute the '${command}' shell command. ` +
`Original error: ${err.message}. ` +
`StdOut: ${err.stdout}. StdErr: ${err.stderr}`);
}
}
//# sourceMappingURL=shell.js.map