forked from stoneman/appium-windows-driver
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathexecute.js
More file actions
46 lines (41 loc) · 1.39 KB
/
execute.js
File metadata and controls
46 lines (41 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import _ from 'lodash';
import {POWER_SHELL_FEATURE} from '../constants';
const POWER_SHELL_SCRIPT = 'powerShell';
const EXECUTE_SCRIPT_PREFIX = 'windows:';
/**
*
* @this {WindowsDriver}
* @param {string} script
* @param {ExecuteMethodArgs} [args]
* @returns {Promise<any>}
*/
export async function execute(script, args) {
if (script === POWER_SHELL_SCRIPT) {
this.assertFeatureEnabled(POWER_SHELL_FEATURE);
return await this.execPowerShell(
/** @type {import('./powershell').ExecPowerShellOptions} */ (
preprocessExecuteMethodArgs(args)
),
);
}
this.log.info(`Executing extension command '${script}'`);
const formattedScript = _.isString(script)
? script.trim().replace(/^windows:\s*/, `${EXECUTE_SCRIPT_PREFIX} `)
: String(script);
const preprocessedArgs = preprocessExecuteMethodArgs(args);
return await this.executeMethod(formattedScript, [preprocessedArgs]);
}
/**
* Massages the arguments going into an execute method.
*
* @param {ExecuteMethodArgs} [args]
* @returns {StringRecord}
*/
function preprocessExecuteMethodArgs(args) {
return /** @type {StringRecord} */ ((_.isArray(args) ? _.first(args) : args) ?? {});
}
/**
* @typedef {import('../driver').WindowsDriver} WindowsDriver
* @typedef {import('@appium/types').StringRecord} StringRecord
* @typedef {readonly any[] | readonly [StringRecord] | Readonly<StringRecord>} ExecuteMethodArgs
*/