appium-android-driver
Version:
Android UiAutomator and Chrome support for Appium
68 lines • 2.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.execute = execute;
const lodash_1 = __importDefault(require("lodash"));
const driver_1 = require("appium/driver");
const support_1 = require("@appium/support");
const EXECUTE_SCRIPT_PREFIX = 'mobile:';
/**
* @this {AndroidDriver}
* @param {string} script
* @param {ExecuteMethodArgs} [args]
* @returns {Promise<any>}
*/
async function execute(script, args) {
if (lodash_1.default.startsWith(script, EXECUTE_SCRIPT_PREFIX)) {
const formattedScript = script.trim().replace(/^mobile:\s*/, `${EXECUTE_SCRIPT_PREFIX} `);
const executeMethodArgs = preprocessExecuteMethodArgs(args);
return await this.executeMethod(formattedScript, [executeMethodArgs]);
}
if (!this.isWebContext()) {
throw new driver_1.errors.NotImplementedError();
}
const endpoint =
/** @type {import('appium-chromedriver').Chromedriver} */ (this.chromedriver).jwproxy
.downstreamProtocol === driver_1.PROTOCOLS.MJSONWP
? '/execute'
: '/execute/sync';
return await /** @type {import('appium-chromedriver').Chromedriver} */ (this.chromedriver).jwproxy.command(endpoint, 'POST', {
script,
args,
});
}
// #region Internal Helpers
/**
* Massages the arguments going into an execute method.
*
* @param {ExecuteMethodArgs} [args]
* @returns {StringRecord}
*/
function preprocessExecuteMethodArgs(args) {
const executeMethodArgs = /** @type {StringRecord} */ ((lodash_1.default.isArray(args) ? lodash_1.default.first(args) : args) ?? {});
/**
* Renames the deprecated `element` key to `elementId`. Historically,
* all of the pre-Execute-Method-Map execute methods accepted an `element` _or_ and `elementId` param.
* This assigns the `element` value to `elementId` if `elementId` is not already present.
*/
if (!('elementId' in executeMethodArgs) && 'element' in executeMethodArgs) {
executeMethodArgs.elementId = executeMethodArgs.element;
}
/**
* Automatically unwraps the `elementId` prop _if and only if_ the execute method expects it.
*/
if ('elementId' in executeMethodArgs) {
executeMethodArgs.elementId = support_1.util.unwrapElement(
/** @type {import('@appium/types').Element|string} */ (executeMethodArgs.elementId));
}
return executeMethodArgs;
}
// #endregion
/**
* @typedef {import('../driver').AndroidDriver} AndroidDriver
* @typedef {import('@appium/types').StringRecord} StringRecord
* @typedef {readonly any[] | readonly [StringRecord] | Readonly<StringRecord>} ExecuteMethodArgs
*/
//# sourceMappingURL=execute.js.map