@appium/execute-driver-plugin 5.0.3 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -2
- package/build/lib/execute-child.d.ts +2 -28
- package/build/lib/execute-child.d.ts.map +1 -1
- package/build/lib/execute-child.js +20 -44
- package/build/lib/execute-child.js.map +1 -1
- package/build/lib/index.d.ts +4 -0
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/index.js +9 -0
- package/build/lib/index.js.map +1 -0
- package/build/lib/plugin.d.ts +22 -14
- package/build/lib/plugin.d.ts.map +1 -1
- package/build/lib/plugin.js +23 -29
- package/build/lib/plugin.js.map +1 -1
- package/build/lib/types.d.ts +22 -0
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/types.js +3 -0
- package/build/lib/types.js.map +1 -0
- package/lib/{execute-child.js → execute-child.ts} +30 -58
- package/lib/index.ts +3 -0
- package/lib/{plugin.js → plugin.ts} +35 -37
- package/lib/types.ts +24 -0
- package/package.json +11 -12
- package/tsconfig.json +3 -2
- package/index.js +0 -1
package/README.md
CHANGED
|
@@ -13,6 +13,9 @@ the only supported driver type is `webdriverio`, therefore the script must also
|
|
|
13
13
|
Running a driver script in a child process adds a degree of parallelisation, which may result in
|
|
14
14
|
faster test execution.
|
|
15
15
|
|
|
16
|
+
> [!WARNING]
|
|
17
|
+
> This plugin enables execution of arbitrary JavaScript code. We recommend only using this plugin in a controlled environment.
|
|
18
|
+
|
|
16
19
|
## Installation
|
|
17
20
|
|
|
18
21
|
```
|
|
@@ -23,9 +26,11 @@ The plugin must be explicitly activated when launching the Appium server. Since
|
|
|
23
26
|
can be arbitrary JavaScript, this is an insecure feature, and must also be explicitly enabled:
|
|
24
27
|
|
|
25
28
|
```
|
|
26
|
-
appium --use-plugins=execute-driver --allow-insecure
|
|
29
|
+
appium --use-plugins=execute-driver --allow-insecure=<driver>:execute_driver_script
|
|
27
30
|
```
|
|
28
31
|
|
|
32
|
+
`<driver>` is the name of the driver whose sessions will have access to the plugin.
|
|
33
|
+
|
|
29
34
|
## Usage
|
|
30
35
|
|
|
31
36
|
```js
|
|
@@ -35,7 +40,15 @@ const {result, logs} = await driver.executeDriverScript(script);
|
|
|
35
40
|
// 'logs' contains everything logged to console during script execution
|
|
36
41
|
```
|
|
37
42
|
|
|
38
|
-
Refer to your Appium client documentation for the exact syntax of
|
|
43
|
+
Refer to your Appium client documentation for the exact syntax of the script execution command.
|
|
44
|
+
|
|
45
|
+
Since plugin version `6.0.0`, scripts can also use the `setTimeout`/`clearTimeout` methods,
|
|
46
|
+
enabling the use of unconditional delays:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
// this will take around one second to execute
|
|
50
|
+
const script = `return await new Promise((resolve) => setTimeout(resolve, 1000));`;
|
|
51
|
+
```
|
|
39
52
|
|
|
40
53
|
## License
|
|
41
54
|
|
|
@@ -1,29 +1,3 @@
|
|
|
1
|
-
export const W3C_ELEMENT_KEY
|
|
2
|
-
export const MJSONWP_ELEMENT_KEY
|
|
3
|
-
export type DriverScriptMessageEvent = {
|
|
4
|
-
/**
|
|
5
|
-
* - the driver options
|
|
6
|
-
*/
|
|
7
|
-
driverOpts: any;
|
|
8
|
-
/**
|
|
9
|
-
* - the javascript to execute
|
|
10
|
-
*/
|
|
11
|
-
script: string;
|
|
12
|
-
/**
|
|
13
|
-
* - script timeout in milliseconds
|
|
14
|
-
*/
|
|
15
|
-
timeoutMs: number;
|
|
16
|
-
};
|
|
17
|
-
export type ScriptResult = {
|
|
18
|
-
success?: any;
|
|
19
|
-
error?: ScriptResultError | undefined;
|
|
20
|
-
};
|
|
21
|
-
export type ScriptResultError = {
|
|
22
|
-
message: any;
|
|
23
|
-
stack: any;
|
|
24
|
-
};
|
|
25
|
-
export type RunScriptResult = {
|
|
26
|
-
result: any;
|
|
27
|
-
logs: object;
|
|
28
|
-
};
|
|
1
|
+
export declare const W3C_ELEMENT_KEY = "element-6066-11e4-a52e-4f735466cecf";
|
|
2
|
+
export declare const MJSONWP_ELEMENT_KEY = "ELEMENT";
|
|
29
3
|
//# sourceMappingURL=execute-child.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"execute-child.d.ts","sourceRoot":"","sources":["../../lib/execute-child.
|
|
1
|
+
{"version":3,"file":"execute-child.d.ts","sourceRoot":"","sources":["../../lib/execute-child.ts"],"names":[],"mappings":"AAWA,eAAO,MAAM,eAAe,wCAAkC,CAAC;AAC/D,eAAO,MAAM,mBAAmB,YAAY,CAAC"}
|
|
@@ -5,13 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.MJSONWP_ELEMENT_KEY = exports.W3C_ELEMENT_KEY = void 0;
|
|
7
7
|
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
-
const bluebird_1 = __importDefault(require("bluebird"));
|
|
9
8
|
const node_vm_1 = __importDefault(require("node:vm"));
|
|
9
|
+
const node_util_1 = require("node:util");
|
|
10
10
|
const support_1 = require("appium/support");
|
|
11
11
|
const log = support_1.logger.getLogger('ExecuteDriver Child');
|
|
12
|
-
/**
|
|
13
|
-
* @type {(res: ScriptResult) => Promise<void>}
|
|
14
|
-
*/
|
|
15
12
|
let send;
|
|
16
13
|
// duplicate defining these keys here so we don't need to re-load a huge appium
|
|
17
14
|
// dependency tree into memory just to run a wdio script
|
|
@@ -19,8 +16,8 @@ exports.W3C_ELEMENT_KEY = support_1.util.W3C_WEB_ELEMENT_IDENTIFIER;
|
|
|
19
16
|
exports.MJSONWP_ELEMENT_KEY = 'ELEMENT';
|
|
20
17
|
/**
|
|
21
18
|
* Run the script in a VM.
|
|
22
|
-
* @param
|
|
23
|
-
* @returns
|
|
19
|
+
* @param eventParams - The driver script message event parameters
|
|
20
|
+
* @returns Promise resolving to the run script result
|
|
24
21
|
* @throws {TypeError}
|
|
25
22
|
*/
|
|
26
23
|
async function runScript(eventParams) {
|
|
@@ -30,22 +27,24 @@ async function runScript(eventParams) {
|
|
|
30
27
|
}
|
|
31
28
|
/**
|
|
32
29
|
* set up fake logger
|
|
33
|
-
* @type {string[]}
|
|
34
30
|
*/
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
31
|
+
const logs = {
|
|
32
|
+
error: [],
|
|
33
|
+
warn: [],
|
|
34
|
+
log: [],
|
|
35
|
+
};
|
|
36
|
+
const consoleFns = {
|
|
37
|
+
error: (...logMsgs) => logs.error.push(...logMsgs),
|
|
38
|
+
warn: (...logMsgs) => logs.warn.push(...logMsgs),
|
|
39
|
+
log: (...logMsgs) => logs.log.push(...logMsgs),
|
|
40
|
+
};
|
|
42
41
|
const { attach } = await import('webdriverio');
|
|
43
42
|
const driver = await attach(driverOpts);
|
|
44
43
|
const fullScript = `(async () => {${script}})();`;
|
|
45
44
|
log.info('Running driver script in Node vm');
|
|
46
|
-
// run the driver script, giving user access to the driver object, a fake
|
|
47
|
-
//
|
|
48
|
-
let result = await node_vm_1.default.runInNewContext(fullScript, { driver, console: consoleFns,
|
|
45
|
+
// run the driver script, giving user access to the driver object, a fake console logger,
|
|
46
|
+
// and standard setTimeout/clearTimeout functions
|
|
47
|
+
let result = await node_vm_1.default.runInNewContext(fullScript, { driver, console: consoleFns, setTimeout, clearTimeout }, { timeout: timeoutMs, breakOnSigint: true });
|
|
49
48
|
result = coerceScriptResult(result);
|
|
50
49
|
log.info('Successfully ensured driver script result is appropriate type for return');
|
|
51
50
|
return { result, logs };
|
|
@@ -56,9 +55,8 @@ async function runScript(eventParams) {
|
|
|
56
55
|
* response. So make sure we convert the things we know about to their
|
|
57
56
|
* appropriate response format, and squash other weird things.
|
|
58
57
|
*
|
|
59
|
-
* @param
|
|
60
|
-
*
|
|
61
|
-
* @return {Object} - safely converted object
|
|
58
|
+
* @param obj - object to convert and sanitize
|
|
59
|
+
* @return safely converted object
|
|
62
60
|
*/
|
|
63
61
|
function coerceScriptResult(obj) {
|
|
64
62
|
// first ensure obj is of a type that can be JSON encoded safely. This will
|
|
@@ -107,12 +105,11 @@ function coerceScriptResult(obj) {
|
|
|
107
105
|
}
|
|
108
106
|
/**
|
|
109
107
|
* Entry point to runScript
|
|
110
|
-
* @param
|
|
108
|
+
* @param eventParams - The driver script message event parameters
|
|
111
109
|
*/
|
|
112
110
|
async function main(eventParams) {
|
|
113
111
|
/**
|
|
114
112
|
* keep the response of runScript
|
|
115
|
-
* @type {ScriptResult}
|
|
116
113
|
*/
|
|
117
114
|
let res;
|
|
118
115
|
log.info('Parameters received from parent process');
|
|
@@ -128,29 +125,8 @@ async function main(eventParams) {
|
|
|
128
125
|
}
|
|
129
126
|
// ensure we're running this script in IPC mode
|
|
130
127
|
if (require.main === module && lodash_1.default.isFunction(process.send)) {
|
|
131
|
-
send =
|
|
128
|
+
send = (0, node_util_1.promisify)(process.send).bind(process);
|
|
132
129
|
log.info('Running driver execution in child process');
|
|
133
130
|
process.on('message', main);
|
|
134
131
|
}
|
|
135
|
-
/**
|
|
136
|
-
* @typedef DriverScriptMessageEvent
|
|
137
|
-
* @property {any} driverOpts - the driver options
|
|
138
|
-
* @property {string} script - the javascript to execute
|
|
139
|
-
* @property {number} timeoutMs - script timeout in milliseconds
|
|
140
|
-
*/
|
|
141
|
-
/**
|
|
142
|
-
* @typedef ScriptResult
|
|
143
|
-
* @property {any} [success]
|
|
144
|
-
* @property {ScriptResultError} [error]
|
|
145
|
-
*/
|
|
146
|
-
/**
|
|
147
|
-
* @typedef ScriptResultError
|
|
148
|
-
* @property {any} message
|
|
149
|
-
* @property {any} stack
|
|
150
|
-
*/
|
|
151
|
-
/**
|
|
152
|
-
* @typedef RunScriptResult
|
|
153
|
-
* @property {any} result
|
|
154
|
-
* @property {object} logs
|
|
155
|
-
*/
|
|
156
132
|
//# sourceMappingURL=execute-child.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"execute-child.js","sourceRoot":"","sources":["../../lib/execute-child.
|
|
1
|
+
{"version":3,"file":"execute-child.js","sourceRoot":"","sources":["../../lib/execute-child.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAuB;AACvB,sDAAyB;AACzB,yCAAoC;AACpC,4CAA4C;AAG5C,MAAM,GAAG,GAAG,gBAAM,CAAC,SAAS,CAAC,qBAAqB,CAAC,CAAC;AACpD,IAAI,IAA0C,CAAC;AAE/C,+EAA+E;AAC/E,wDAAwD;AAC3C,QAAA,eAAe,GAAG,cAAI,CAAC,0BAA0B,CAAC;AAClD,QAAA,mBAAmB,GAAG,SAAS,CAAC;AAE7C;;;;;GAKG;AACH,KAAK,UAAU,SAAS,CAAC,WAAqC;IAC5D,MAAM,EAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAC,GAAG,WAAW,CAAC;IACpD,IAAI,CAAC,gBAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,SAAS,CAAC,oCAAoC,CAAC,CAAC;IAC5D,CAAC;IAED;;OAEG;IACH,MAAM,IAAI,GAA4C;QACpD,KAAK,EAAE,EAAE;QACT,IAAI,EAAE,EAAE;QACR,GAAG,EAAE,EAAE;KACR,CAAC;IACF,MAAM,UAAU,GAAqG;QACnH,KAAK,EAAE,CAAC,GAAG,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;QAClD,IAAI,EAAE,CAAC,GAAG,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;QAChD,GAAG,EAAE,CAAC,GAAG,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;KAC/C,CAAC;IAEF,MAAM,EAAC,MAAM,EAAC,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAC;IAE7C,MAAM,MAAM,GA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AAM,IAAI,CAAC,GAAG,CAAC,CAAC;AAClB,CAAC;AAED,+CAA+C;AAC/C,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,gBAAC,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;IAC1D,IAAI,GAAG,IAAA,qBAAS,EAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7C,GAAG,CAAC,IAAI,CAAC,2CAA2C,CAAC,CAAC;IACtD,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAC,eAAe,EAAE,mBAAmB,EAAC,MAAM,iBAAiB,CAAC;AACrE,mBAAmB,SAAS,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MJSONWP_ELEMENT_KEY = exports.W3C_ELEMENT_KEY = exports.ExecuteDriverPlugin = void 0;
|
|
4
|
+
var plugin_1 = require("./plugin");
|
|
5
|
+
Object.defineProperty(exports, "ExecuteDriverPlugin", { enumerable: true, get: function () { return plugin_1.ExecuteDriverPlugin; } });
|
|
6
|
+
var execute_child_1 = require("./execute-child");
|
|
7
|
+
Object.defineProperty(exports, "W3C_ELEMENT_KEY", { enumerable: true, get: function () { return execute_child_1.W3C_ELEMENT_KEY; } });
|
|
8
|
+
Object.defineProperty(exports, "MJSONWP_ELEMENT_KEY", { enumerable: true, get: function () { return execute_child_1.MJSONWP_ELEMENT_KEY; } });
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":";;;AAAA,mCAA6C;AAArC,6GAAA,mBAAmB,OAAA;AAC3B,iDAAqE;AAA7D,gHAAA,eAAe,OAAA;AAAE,oHAAA,mBAAmB,OAAA"}
|
package/build/lib/plugin.d.ts
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
export default class ExecuteDriverPlugin extends BasePlugin {
|
|
2
|
-
static newMethodMap: {
|
|
3
|
-
readonly '/session/:sessionId/appium/execute_driver': {
|
|
4
|
-
readonly POST: {
|
|
5
|
-
readonly command: "executeDriverScript";
|
|
6
|
-
readonly payloadParams: {
|
|
7
|
-
readonly required: readonly ["script"];
|
|
8
|
-
readonly optional: readonly ["type", "timeout"];
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
executeDriverScript(next: import("@appium/types").NextPluginCallback<unknown>, driver: import("@appium/types").ExternalDriver<import("@appium/types").Constraints, string, import("@appium/types").StringRecord, import("@appium/types").StringRecord, import("@appium/types").DefaultCreateSessionResult<import("@appium/types").Constraints>, void, import("@appium/types").StringRecord>, script: string, scriptType: string | undefined, timeoutMs: number | undefined): Promise<unknown>;
|
|
14
|
-
}
|
|
15
1
|
import { BasePlugin } from 'appium/plugin';
|
|
2
|
+
import type { ExternalDriver, MethodMap, PluginCommand } from '@appium/types';
|
|
3
|
+
export declare class ExecuteDriverPlugin extends BasePlugin {
|
|
4
|
+
static newMethodMap: MethodMap<ExecuteDriverPlugin>;
|
|
5
|
+
/**
|
|
6
|
+
* This method takes a string which is executed as javascript in the context of
|
|
7
|
+
* a new nodejs VM, and which has available a webdriverio driver object, having
|
|
8
|
+
* already been attached to the currently running session.
|
|
9
|
+
*
|
|
10
|
+
* @param next - standard behaviour for executeDriverScript
|
|
11
|
+
* @param driver - Appium driver handling this command
|
|
12
|
+
* @param script - the string representing the driver script to run
|
|
13
|
+
* @param scriptType - the name of the driver script library (currently only webdriverio is supported). Defaults to `'webdriverio'`.
|
|
14
|
+
* @param timeoutMs - timeout for the script process. Defaults to `3600000`.
|
|
15
|
+
* @returns a JSONifiable object representing the return value of the script
|
|
16
|
+
* @throws {Error}
|
|
17
|
+
*/
|
|
18
|
+
executeDriverScript: PluginCommand<ExternalDriver, [
|
|
19
|
+
script: string,
|
|
20
|
+
scriptType?: string,
|
|
21
|
+
timeoutMs?: number
|
|
22
|
+
], any>;
|
|
23
|
+
}
|
|
16
24
|
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../lib/plugin.
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../lib/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,eAAe,CAAC;AAIzC,OAAO,KAAK,EAAC,cAAc,EAAsB,SAAS,EAAE,aAAa,EAAC,MAAM,eAAe,CAAC;AAMhG,qBAAa,mBAAoB,SAAQ,UAAU;IACjD,MAAM,CAAC,YAAY,EAAE,SAAS,CAAC,mBAAmB,CAAC,CAOxC;IAEX;;;;;;;;;;;;OAYG;IACH,mBAAmB,EAAE,aAAa,CAChC,cAAc,EACd;QAAC,MAAM,EAAE,MAAM;QAAE,UAAU,CAAC,EAAE,MAAM;QAAE,SAAS,CAAC,EAAE,MAAM;KAAC,EACzD,GAAG,CACJ,CA2HC;CACH"}
|
package/build/lib/plugin.js
CHANGED
|
@@ -6,39 +6,34 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.ExecuteDriverPlugin = void 0;
|
|
7
7
|
const plugin_1 = require("appium/plugin");
|
|
8
8
|
const lodash_1 = __importDefault(require("lodash"));
|
|
9
|
-
const
|
|
9
|
+
const node_child_process_1 = __importDefault(require("node:child_process"));
|
|
10
10
|
const support_1 = require("appium/support");
|
|
11
|
-
const bluebird_1 = __importDefault(require("bluebird"));
|
|
12
11
|
const FEAT_FLAG = 'execute_driver_script';
|
|
13
12
|
const DEFAULT_SCRIPT_TIMEOUT_MS = 1000 * 60 * 60; // default to 1 hour timeout
|
|
14
13
|
const SCRIPT_TYPE_WDIO = 'webdriverio';
|
|
15
14
|
class ExecuteDriverPlugin extends plugin_1.BasePlugin {
|
|
16
|
-
static newMethodMap =
|
|
15
|
+
static newMethodMap = {
|
|
17
16
|
'/session/:sessionId/appium/execute_driver': {
|
|
18
17
|
POST: {
|
|
19
18
|
command: 'executeDriverScript',
|
|
20
19
|
payloadParams: { required: ['script'], optional: ['type', 'timeout'] },
|
|
21
20
|
},
|
|
22
21
|
},
|
|
23
|
-
}
|
|
22
|
+
};
|
|
24
23
|
/**
|
|
25
24
|
* This method takes a string which is executed as javascript in the context of
|
|
26
25
|
* a new nodejs VM, and which has available a webdriverio driver object, having
|
|
27
26
|
* already been attached to the currently running session.
|
|
28
27
|
*
|
|
29
|
-
* @param
|
|
30
|
-
* @param
|
|
31
|
-
* @param
|
|
32
|
-
* @param
|
|
33
|
-
*
|
|
34
|
-
* @
|
|
35
|
-
*
|
|
36
|
-
* @returns {Promise<any>} - a JSONifiable object representing the return value of
|
|
37
|
-
* the script
|
|
38
|
-
* @type {import('@appium/types').PluginCommand<import('@appium/types').ExternalDriver, [script: string, scriptType: string?, timeoutMs: number?]>}
|
|
28
|
+
* @param next - standard behaviour for executeDriverScript
|
|
29
|
+
* @param driver - Appium driver handling this command
|
|
30
|
+
* @param script - the string representing the driver script to run
|
|
31
|
+
* @param scriptType - the name of the driver script library (currently only webdriverio is supported). Defaults to `'webdriverio'`.
|
|
32
|
+
* @param timeoutMs - timeout for the script process. Defaults to `3600000`.
|
|
33
|
+
* @returns a JSONifiable object representing the return value of the script
|
|
39
34
|
* @throws {Error}
|
|
40
35
|
*/
|
|
41
|
-
async
|
|
36
|
+
executeDriverScript = async (next, driver, script, scriptType = 'webdriverio', timeoutMs = DEFAULT_SCRIPT_TIMEOUT_MS) => {
|
|
42
37
|
if (!driver.isFeatureEnabled(FEAT_FLAG)) {
|
|
43
38
|
throw new Error(`Execute driver script functionality is not available ` +
|
|
44
39
|
`unless server is started with --allow-insecure including ` +
|
|
@@ -69,11 +64,11 @@ class ExecuteDriverPlugin extends plugin_1.BasePlugin {
|
|
|
69
64
|
isMobile: true,
|
|
70
65
|
capabilities: driver.caps,
|
|
71
66
|
};
|
|
72
|
-
this.
|
|
67
|
+
this.log.info(`Constructed webdriverio driver options; W3C mode is ${driverOpts.isW3C ? 'on' : 'off'}`);
|
|
73
68
|
// fork the execution script as a child process
|
|
74
69
|
const childScript = require.resolve('./execute-child.js');
|
|
75
|
-
this.
|
|
76
|
-
const scriptProc =
|
|
70
|
+
this.log.info(`Forking process to run webdriver script as child using ${childScript}`);
|
|
71
|
+
const scriptProc = node_child_process_1.default.fork(childScript);
|
|
77
72
|
// keep track of whether we have canceled the script timeout, so we can stop
|
|
78
73
|
// waiting for it and allow this process to finish gracefully
|
|
79
74
|
let timeoutCanceled = false;
|
|
@@ -82,10 +77,10 @@ class ExecuteDriverPlugin extends plugin_1.BasePlugin {
|
|
|
82
77
|
timer.start();
|
|
83
78
|
// promise that deals with the result from the child process
|
|
84
79
|
const waitForResult = async () => {
|
|
85
|
-
const res = await new
|
|
86
|
-
scriptProc.
|
|
80
|
+
const res = await new Promise((resolve) => {
|
|
81
|
+
scriptProc.once('message', resolve); // this is node IPC
|
|
87
82
|
});
|
|
88
|
-
this.
|
|
83
|
+
this.log.info('Received execute driver script result from child process, shutting it down');
|
|
89
84
|
if (res.error) {
|
|
90
85
|
throw new Error(res.error.message);
|
|
91
86
|
}
|
|
@@ -96,7 +91,7 @@ class ExecuteDriverPlugin extends plugin_1.BasePlugin {
|
|
|
96
91
|
// child script
|
|
97
92
|
const waitForTimeout = async () => {
|
|
98
93
|
while (!timeoutCanceled && timer.getDuration().asMilliSeconds < timeoutMs) {
|
|
99
|
-
await
|
|
94
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
100
95
|
}
|
|
101
96
|
if (timeoutCanceled) {
|
|
102
97
|
return;
|
|
@@ -106,10 +101,10 @@ class ExecuteDriverPlugin extends plugin_1.BasePlugin {
|
|
|
106
101
|
};
|
|
107
102
|
// now that the child script is alive, send it the data it needs to start
|
|
108
103
|
// running the driver script
|
|
109
|
-
this.
|
|
104
|
+
this.log.info('Sending driver and script data to child');
|
|
110
105
|
scriptProc.send({ driverOpts, script, timeoutMs });
|
|
111
106
|
// and set up a race between the response from the child and the timeout
|
|
112
|
-
return await
|
|
107
|
+
return await Promise.race([waitForResult(), waitForTimeout()]);
|
|
113
108
|
}
|
|
114
109
|
catch (err) {
|
|
115
110
|
throw new Error(`Could not execute driver script. Original error was: ${err}`);
|
|
@@ -119,19 +114,18 @@ class ExecuteDriverPlugin extends plugin_1.BasePlugin {
|
|
|
119
114
|
// spinning and allows this process to die gracefully
|
|
120
115
|
timeoutCanceled = true;
|
|
121
116
|
if (scriptProc.connected) {
|
|
122
|
-
this.
|
|
117
|
+
this.log.info('Disconnecting from child proc');
|
|
123
118
|
scriptProc.disconnect();
|
|
124
119
|
}
|
|
125
120
|
if (scriptProc.exitCode === null) {
|
|
126
|
-
this.
|
|
121
|
+
this.log.info('Disconnecting from and killing driver script child proc');
|
|
127
122
|
scriptProc.kill();
|
|
128
123
|
}
|
|
129
124
|
else {
|
|
130
|
-
this.
|
|
125
|
+
this.log.info('Script already ended on its own, no need to kill it');
|
|
131
126
|
}
|
|
132
127
|
}
|
|
133
|
-
}
|
|
128
|
+
};
|
|
134
129
|
}
|
|
135
|
-
exports.default = ExecuteDriverPlugin;
|
|
136
130
|
exports.ExecuteDriverPlugin = ExecuteDriverPlugin;
|
|
137
131
|
//# sourceMappingURL=plugin.js.map
|
package/build/lib/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../lib/plugin.
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../lib/plugin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CAAC,+BAA+B,CAAC,CAAC;gBAC/C,UAAU,CAAC,UAAU,EAAE,CAAC;YAC1B,CAAC;YAED,IAAI,UAAU,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;gBACjC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,yDAAyD,CAAC,CAAC;gBACzE,UAAU,CAAC,IAAI,EAAE,CAAC;YACpB,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,qDAAqD,CAAC,CAAC;YACvE,CAAC;QACH,CAAC;IACH,CAAC,CAAC;;AAtJJ,kDAuJC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface DriverScriptMessageEvent {
|
|
2
|
+
driverOpts: any;
|
|
3
|
+
script: string;
|
|
4
|
+
timeoutMs: number;
|
|
5
|
+
}
|
|
6
|
+
export interface ScriptResultError {
|
|
7
|
+
message: any;
|
|
8
|
+
stack: any;
|
|
9
|
+
}
|
|
10
|
+
export interface ScriptResult {
|
|
11
|
+
success?: RunScriptResult;
|
|
12
|
+
error?: ScriptResultError;
|
|
13
|
+
}
|
|
14
|
+
export interface RunScriptResult {
|
|
15
|
+
result: any;
|
|
16
|
+
logs: {
|
|
17
|
+
error: any[];
|
|
18
|
+
warn: any[];
|
|
19
|
+
log: any[];
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../lib/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,UAAU,EAAE,GAAG,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,GAAG,CAAC;IACb,KAAK,EAAE,GAAG,CAAC;CACZ;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,EAAE,eAAe,CAAC;IAC1B,KAAK,CAAC,EAAE,iBAAiB,CAAC;CAC3B;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,GAAG,CAAC;IACZ,IAAI,EAAE;QACJ,KAAK,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,EAAE,GAAG,EAAE,CAAC;QACZ,GAAG,EAAE,GAAG,EAAE,CAAC;KACZ,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../lib/types.ts"],"names":[],"mappings":""}
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import _ from 'lodash';
|
|
2
|
-
import B from 'bluebird';
|
|
3
2
|
import vm from 'node:vm';
|
|
3
|
+
import {promisify} from 'node:util';
|
|
4
4
|
import {logger, util} from 'appium/support';
|
|
5
|
+
import type {DriverScriptMessageEvent, ScriptResult, RunScriptResult} from './types';
|
|
5
6
|
|
|
6
7
|
const log = logger.getLogger('ExecuteDriver Child');
|
|
7
|
-
|
|
8
|
-
* @type {(res: ScriptResult) => Promise<void>}
|
|
9
|
-
*/
|
|
10
|
-
let send;
|
|
8
|
+
let send: (res: ScriptResult) => Promise<void>;
|
|
11
9
|
|
|
12
10
|
// duplicate defining these keys here so we don't need to re-load a huge appium
|
|
13
11
|
// dependency tree into memory just to run a wdio script
|
|
@@ -16,11 +14,11 @@ export const MJSONWP_ELEMENT_KEY = 'ELEMENT';
|
|
|
16
14
|
|
|
17
15
|
/**
|
|
18
16
|
* Run the script in a VM.
|
|
19
|
-
* @param
|
|
20
|
-
* @returns
|
|
17
|
+
* @param eventParams - The driver script message event parameters
|
|
18
|
+
* @returns Promise resolving to the run script result
|
|
21
19
|
* @throws {TypeError}
|
|
22
20
|
*/
|
|
23
|
-
async function runScript(eventParams) {
|
|
21
|
+
async function runScript(eventParams: DriverScriptMessageEvent): Promise<RunScriptResult> {
|
|
24
22
|
const {driverOpts, script, timeoutMs} = eventParams;
|
|
25
23
|
if (!_.isNumber(timeoutMs)) {
|
|
26
24
|
throw new TypeError('Timeout parameter must be a number');
|
|
@@ -28,15 +26,17 @@ async function runScript(eventParams) {
|
|
|
28
26
|
|
|
29
27
|
/**
|
|
30
28
|
* set up fake logger
|
|
31
|
-
* @type {string[]}
|
|
32
29
|
*/
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
const logs: {error: any[]; warn: any[]; log: any[]} = {
|
|
31
|
+
error: [],
|
|
32
|
+
warn: [],
|
|
33
|
+
log: [],
|
|
34
|
+
};
|
|
35
|
+
const consoleFns: {error: (...args: any[]) => void; warn: (...args: any[]) => void; log: (...args: any[]) => void} = {
|
|
36
|
+
error: (...logMsgs) => logs.error.push(...logMsgs),
|
|
37
|
+
warn: (...logMsgs) => logs.warn.push(...logMsgs),
|
|
38
|
+
log: (...logMsgs) => logs.log.push(...logMsgs),
|
|
39
|
+
};
|
|
40
40
|
|
|
41
41
|
const {attach} = await import('webdriverio');
|
|
42
42
|
|
|
@@ -46,12 +46,12 @@ async function runScript(eventParams) {
|
|
|
46
46
|
|
|
47
47
|
log.info('Running driver script in Node vm');
|
|
48
48
|
|
|
49
|
-
// run the driver script, giving user access to the driver object, a fake
|
|
50
|
-
//
|
|
49
|
+
// run the driver script, giving user access to the driver object, a fake console logger,
|
|
50
|
+
// and standard setTimeout/clearTimeout functions
|
|
51
51
|
let result = await vm.runInNewContext(
|
|
52
52
|
fullScript,
|
|
53
|
-
{driver, console: consoleFns,
|
|
54
|
-
{timeout: timeoutMs, breakOnSigint: true}
|
|
53
|
+
{driver, console: consoleFns, setTimeout, clearTimeout},
|
|
54
|
+
{timeout: timeoutMs, breakOnSigint: true}
|
|
55
55
|
);
|
|
56
56
|
|
|
57
57
|
result = coerceScriptResult(result);
|
|
@@ -65,11 +65,10 @@ async function runScript(eventParams) {
|
|
|
65
65
|
* response. So make sure we convert the things we know about to their
|
|
66
66
|
* appropriate response format, and squash other weird things.
|
|
67
67
|
*
|
|
68
|
-
* @param
|
|
69
|
-
*
|
|
70
|
-
* @return {Object} - safely converted object
|
|
68
|
+
* @param obj - object to convert and sanitize
|
|
69
|
+
* @return safely converted object
|
|
71
70
|
*/
|
|
72
|
-
function coerceScriptResult(obj) {
|
|
71
|
+
function coerceScriptResult(obj: any): any {
|
|
73
72
|
// first ensure obj is of a type that can be JSON encoded safely. This will
|
|
74
73
|
// get rid of custom objects, functions, etc... and turn them into POJOs
|
|
75
74
|
try {
|
|
@@ -77,12 +76,12 @@ function coerceScriptResult(obj) {
|
|
|
77
76
|
} catch {
|
|
78
77
|
log.warn(
|
|
79
78
|
'Could not convert executeDriverScript to safe response!' +
|
|
80
|
-
`Result was: ${JSON.stringify(obj)}. Will make it null
|
|
79
|
+
`Result was: ${JSON.stringify(obj)}. Will make it null`
|
|
81
80
|
);
|
|
82
81
|
return null;
|
|
83
82
|
}
|
|
84
83
|
|
|
85
|
-
let res;
|
|
84
|
+
let res: any;
|
|
86
85
|
|
|
87
86
|
// now we begin our recursive case options
|
|
88
87
|
if (_.isPlainObject(obj)) {
|
|
@@ -125,19 +124,18 @@ function coerceScriptResult(obj) {
|
|
|
125
124
|
|
|
126
125
|
/**
|
|
127
126
|
* Entry point to runScript
|
|
128
|
-
* @param
|
|
127
|
+
* @param eventParams - The driver script message event parameters
|
|
129
128
|
*/
|
|
130
|
-
async function main(eventParams) {
|
|
129
|
+
async function main(eventParams: DriverScriptMessageEvent): Promise<void> {
|
|
131
130
|
/**
|
|
132
131
|
* keep the response of runScript
|
|
133
|
-
* @type {ScriptResult}
|
|
134
132
|
*/
|
|
135
|
-
let res;
|
|
133
|
+
let res: ScriptResult;
|
|
136
134
|
log.info('Parameters received from parent process');
|
|
137
135
|
try {
|
|
138
136
|
res = {success: await runScript(eventParams)};
|
|
139
137
|
log.info('runScript success');
|
|
140
|
-
} catch (error) {
|
|
138
|
+
} catch (error: any) {
|
|
141
139
|
log.info('runScript error');
|
|
142
140
|
res = {error: {message: error.message, stack: error.stack}};
|
|
143
141
|
}
|
|
@@ -146,33 +144,7 @@ async function main(eventParams) {
|
|
|
146
144
|
|
|
147
145
|
// ensure we're running this script in IPC mode
|
|
148
146
|
if (require.main === module && _.isFunction(process.send)) {
|
|
149
|
-
send =
|
|
147
|
+
send = promisify(process.send).bind(process);
|
|
150
148
|
log.info('Running driver execution in child process');
|
|
151
149
|
process.on('message', main);
|
|
152
150
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* @typedef DriverScriptMessageEvent
|
|
157
|
-
* @property {any} driverOpts - the driver options
|
|
158
|
-
* @property {string} script - the javascript to execute
|
|
159
|
-
* @property {number} timeoutMs - script timeout in milliseconds
|
|
160
|
-
*/
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* @typedef ScriptResult
|
|
164
|
-
* @property {any} [success]
|
|
165
|
-
* @property {ScriptResultError} [error]
|
|
166
|
-
*/
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* @typedef ScriptResultError
|
|
170
|
-
* @property {any} message
|
|
171
|
-
* @property {any} stack
|
|
172
|
-
*/
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* @typedef RunScriptResult
|
|
176
|
-
* @property {any} result
|
|
177
|
-
* @property {object} logs
|
|
178
|
-
*/
|
package/lib/index.ts
ADDED
|
@@ -1,47 +1,47 @@
|
|
|
1
1
|
import {BasePlugin} from 'appium/plugin';
|
|
2
2
|
import _ from 'lodash';
|
|
3
|
-
import cp from 'child_process';
|
|
3
|
+
import cp from 'node:child_process';
|
|
4
4
|
import {timing} from 'appium/support';
|
|
5
|
-
import
|
|
5
|
+
import type {ExternalDriver, NextPluginCallback, MethodMap, PluginCommand} from '@appium/types';
|
|
6
6
|
|
|
7
7
|
const FEAT_FLAG = 'execute_driver_script';
|
|
8
8
|
const DEFAULT_SCRIPT_TIMEOUT_MS = 1000 * 60 * 60; // default to 1 hour timeout
|
|
9
9
|
const SCRIPT_TYPE_WDIO = 'webdriverio';
|
|
10
10
|
|
|
11
|
-
export
|
|
12
|
-
static newMethodMap =
|
|
11
|
+
export class ExecuteDriverPlugin extends BasePlugin {
|
|
12
|
+
static newMethodMap: MethodMap<ExecuteDriverPlugin> = {
|
|
13
13
|
'/session/:sessionId/appium/execute_driver': {
|
|
14
14
|
POST: {
|
|
15
15
|
command: 'executeDriverScript',
|
|
16
16
|
payloadParams: {required: ['script'], optional: ['type', 'timeout']},
|
|
17
17
|
},
|
|
18
18
|
},
|
|
19
|
-
}
|
|
19
|
+
} as const;
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* This method takes a string which is executed as javascript in the context of
|
|
23
23
|
* a new nodejs VM, and which has available a webdriverio driver object, having
|
|
24
24
|
* already been attached to the currently running session.
|
|
25
25
|
*
|
|
26
|
-
* @param
|
|
27
|
-
* @param
|
|
28
|
-
* @param
|
|
29
|
-
* @param
|
|
30
|
-
*
|
|
31
|
-
* @
|
|
32
|
-
*
|
|
33
|
-
* @returns {Promise<any>} - a JSONifiable object representing the return value of
|
|
34
|
-
* the script
|
|
35
|
-
* @type {import('@appium/types').PluginCommand<import('@appium/types').ExternalDriver, [script: string, scriptType: string?, timeoutMs: number?]>}
|
|
26
|
+
* @param next - standard behaviour for executeDriverScript
|
|
27
|
+
* @param driver - Appium driver handling this command
|
|
28
|
+
* @param script - the string representing the driver script to run
|
|
29
|
+
* @param scriptType - the name of the driver script library (currently only webdriverio is supported). Defaults to `'webdriverio'`.
|
|
30
|
+
* @param timeoutMs - timeout for the script process. Defaults to `3600000`.
|
|
31
|
+
* @returns a JSONifiable object representing the return value of the script
|
|
36
32
|
* @throws {Error}
|
|
37
33
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
34
|
+
executeDriverScript: PluginCommand<
|
|
35
|
+
ExternalDriver,
|
|
36
|
+
[script: string, scriptType?: string, timeoutMs?: number],
|
|
37
|
+
any
|
|
38
|
+
> = async (
|
|
39
|
+
next: NextPluginCallback,
|
|
40
|
+
driver: ExternalDriver,
|
|
41
|
+
script: string,
|
|
42
|
+
scriptType: string = 'webdriverio',
|
|
43
|
+
timeoutMs: number = DEFAULT_SCRIPT_TIMEOUT_MS
|
|
44
|
+
) => {
|
|
45
45
|
if (!driver.isFeatureEnabled(FEAT_FLAG)) {
|
|
46
46
|
throw new Error(
|
|
47
47
|
`Execute driver script functionality is not available ` +
|
|
@@ -80,13 +80,13 @@ export default class ExecuteDriverPlugin extends BasePlugin {
|
|
|
80
80
|
isMobile: true,
|
|
81
81
|
capabilities: driver.caps,
|
|
82
82
|
};
|
|
83
|
-
this.
|
|
83
|
+
this.log.info(
|
|
84
84
|
`Constructed webdriverio driver options; W3C mode is ${driverOpts.isW3C ? 'on' : 'off'}`
|
|
85
85
|
);
|
|
86
86
|
|
|
87
87
|
// fork the execution script as a child process
|
|
88
88
|
const childScript = require.resolve('./execute-child.js');
|
|
89
|
-
this.
|
|
89
|
+
this.log.info(`Forking process to run webdriver script as child using ${childScript}`);
|
|
90
90
|
const scriptProc = cp.fork(childScript);
|
|
91
91
|
|
|
92
92
|
// keep track of whether we have canceled the script timeout, so we can stop
|
|
@@ -99,11 +99,11 @@ export default class ExecuteDriverPlugin extends BasePlugin {
|
|
|
99
99
|
|
|
100
100
|
// promise that deals with the result from the child process
|
|
101
101
|
const waitForResult = async () => {
|
|
102
|
-
const res = await new
|
|
103
|
-
scriptProc.
|
|
102
|
+
const res = await new Promise<{error?: {message: string}; success?: any}>((resolve) => {
|
|
103
|
+
scriptProc.once('message', resolve); // this is node IPC
|
|
104
104
|
});
|
|
105
105
|
|
|
106
|
-
this.
|
|
106
|
+
this.log.info(
|
|
107
107
|
'Received execute driver script result from child process, shutting it down'
|
|
108
108
|
);
|
|
109
109
|
|
|
@@ -119,7 +119,7 @@ export default class ExecuteDriverPlugin extends BasePlugin {
|
|
|
119
119
|
// child script
|
|
120
120
|
const waitForTimeout = async () => {
|
|
121
121
|
while (!timeoutCanceled && timer.getDuration().asMilliSeconds < timeoutMs) {
|
|
122
|
-
await
|
|
122
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
if (timeoutCanceled) {
|
|
@@ -134,12 +134,12 @@ export default class ExecuteDriverPlugin extends BasePlugin {
|
|
|
134
134
|
|
|
135
135
|
// now that the child script is alive, send it the data it needs to start
|
|
136
136
|
// running the driver script
|
|
137
|
-
this.
|
|
137
|
+
this.log.info('Sending driver and script data to child');
|
|
138
138
|
scriptProc.send({driverOpts, script, timeoutMs});
|
|
139
139
|
|
|
140
140
|
// and set up a race between the response from the child and the timeout
|
|
141
|
-
return await
|
|
142
|
-
} catch (err) {
|
|
141
|
+
return await Promise.race([waitForResult(), waitForTimeout()]);
|
|
142
|
+
} catch (err: any) {
|
|
143
143
|
throw new Error(`Could not execute driver script. Original error was: ${err}`);
|
|
144
144
|
} finally {
|
|
145
145
|
// ensure we always cancel the timeout so that the timeout promise stops
|
|
@@ -147,18 +147,16 @@ export default class ExecuteDriverPlugin extends BasePlugin {
|
|
|
147
147
|
timeoutCanceled = true;
|
|
148
148
|
|
|
149
149
|
if (scriptProc.connected) {
|
|
150
|
-
this.
|
|
150
|
+
this.log.info('Disconnecting from child proc');
|
|
151
151
|
scriptProc.disconnect();
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
if (scriptProc.exitCode === null) {
|
|
155
|
-
this.
|
|
155
|
+
this.log.info('Disconnecting from and killing driver script child proc');
|
|
156
156
|
scriptProc.kill();
|
|
157
157
|
} else {
|
|
158
|
-
this.
|
|
158
|
+
this.log.info('Script already ended on its own, no need to kill it');
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
|
-
}
|
|
161
|
+
};
|
|
162
162
|
}
|
|
163
|
-
|
|
164
|
-
export {ExecuteDriverPlugin};
|
package/lib/types.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface DriverScriptMessageEvent {
|
|
2
|
+
driverOpts: any;
|
|
3
|
+
script: string;
|
|
4
|
+
timeoutMs: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface ScriptResultError {
|
|
8
|
+
message: any;
|
|
9
|
+
stack: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ScriptResult {
|
|
13
|
+
success?: RunScriptResult;
|
|
14
|
+
error?: ScriptResultError;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface RunScriptResult {
|
|
18
|
+
result: any;
|
|
19
|
+
logs: {
|
|
20
|
+
error: any[];
|
|
21
|
+
warn: any[];
|
|
22
|
+
log: any[];
|
|
23
|
+
};
|
|
24
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appium/execute-driver-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "Plugin for batching and executing Appium driver commands",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"automation",
|
|
@@ -23,25 +23,24 @@
|
|
|
23
23
|
},
|
|
24
24
|
"license": "Apache-2.0",
|
|
25
25
|
"author": "https://github.com/appium",
|
|
26
|
-
"
|
|
26
|
+
"main": "./build/lib/index.js",
|
|
27
|
+
"types": "./build/lib/index.d.ts",
|
|
27
28
|
"files": [
|
|
28
29
|
"build",
|
|
29
30
|
"lib",
|
|
30
|
-
"index.js",
|
|
31
31
|
"tsconfig.json",
|
|
32
|
-
"!build/tsconfig.tsbuildinfo"
|
|
32
|
+
"!build/tsconfig.tsbuildinfo",
|
|
33
|
+
"!build/test"
|
|
33
34
|
],
|
|
34
35
|
"scripts": {
|
|
35
36
|
"test": "npm run test:unit",
|
|
36
|
-
"test:e2e": "mocha --exit -t 160s --slow 20s \"./test/e2e/**/*.spec
|
|
37
|
-
"test:smoke": "node ./index.js",
|
|
38
|
-
"test:unit": "mocha \"./test/unit/**/*.spec
|
|
37
|
+
"test:e2e": "mocha --exit -t 160s --slow 20s \"./test/e2e/**/*.spec.*ts\"",
|
|
38
|
+
"test:smoke": "node ./build/lib/index.js",
|
|
39
|
+
"test:unit": "mocha \"./test/unit/**/*.spec.*ts\""
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"source-map-support": "0.5.21",
|
|
44
|
-
"webdriverio": "9.21.0"
|
|
42
|
+
"lodash": "4.17.23",
|
|
43
|
+
"webdriverio": "9.24.0"
|
|
45
44
|
},
|
|
46
45
|
"peerDependencies": {
|
|
47
46
|
"appium": "^3.0.0-beta.0"
|
|
@@ -57,5 +56,5 @@
|
|
|
57
56
|
"pluginName": "execute-driver",
|
|
58
57
|
"mainClass": "ExecuteDriverPlugin"
|
|
59
58
|
},
|
|
60
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "980a121804ae006db879fb6860f627ac36174c15"
|
|
61
60
|
}
|
package/tsconfig.json
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"outDir": "build",
|
|
4
4
|
"checkJs": true,
|
|
5
|
-
"esModuleInterop": true
|
|
5
|
+
"esModuleInterop": true,
|
|
6
|
+
"types": ["mocha", "chai", "chai-as-promised"]
|
|
6
7
|
},
|
|
7
8
|
"extends": "@appium/tsconfig/tsconfig.plugin.json",
|
|
8
|
-
"include": ["lib"]
|
|
9
|
+
"include": ["lib", "test"]
|
|
9
10
|
}
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('./build/lib/plugin');
|