-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathutils.js
93 lines (83 loc) · 2.36 KB
/
utils.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import _ from 'lodash';
import {errors} from 'appium/driver';
export const ADB_SHELL_FEATURE = 'adb_shell';
export const GET_SERVER_LOGS_FEATURE = 'get_server_logs';
const COLOR_CODE_PATTERN = /\u001b\[(\d+(;\d+)*)?m/g; // eslint-disable-line no-control-regex
/**
* Assert the presence of particular keys in the given object
*
* @param {string|string[]} argNames one or more key names
* @param {any} opts the object to check
* @returns {Record<string, any>} the same given object
*/
export function requireArgs(argNames, opts) {
for (const argName of _.isArray(argNames) ? argNames : [argNames]) {
if (!_.has(opts, argName)) {
throw new errors.InvalidArgumentError(`'${argName}' argument must be provided`);
}
}
return opts;
}
/**
*
* @param {string | string[]} cap
* @returns {string[]}
*/
export function parseArray(cap) {
let parsedCaps;
try {
// @ts-ignore this is fine
parsedCaps = JSON.parse(cap);
} catch {}
if (_.isArray(parsedCaps)) {
return parsedCaps;
} else if (_.isString(cap)) {
return [cap];
}
throw new Error(`must provide a string or JSON Array; received ${cap}`);
}
/**
* @param {import('@appium/types').AppiumServer} server
* @param {string?} [sessionId]
* @returns {Promise<void>}
*/
export async function removeAllSessionWebSocketHandlers(server, sessionId) {
if (!server || !_.isFunction(server.getWebSocketHandlers)) {
return;
}
const activeHandlers = await server.getWebSocketHandlers(sessionId);
for (const pathname of _.keys(activeHandlers)) {
await server.removeWebSocketHandler(pathname);
}
}
/**
*
* @param {Object} x
* @returns {LogEntry}
*/
export function nativeLogEntryToSeleniumEntry (x) {
const msg = _.isEmpty(x.prefix) ? x.message : `[${x.prefix}] ${x.message}`;
return toLogRecord(
/** @type {any} */ (x).timestamp ?? Date.now(),
_.replace(msg, COLOR_CODE_PATTERN, '')
);
}
/**
*
* @see {@link https://github.com/SeleniumHQ/selenium/blob/0d425676b3c9df261dd641917f867d4d5ce7774d/java/client/src/org/openqa/selenium/logging/LogEntry.java}
* @param {number} timestamp
* @param {string} message
* @param {string} [level='ALL']
* @returns {LogEntry}
*/
export function toLogRecord(timestamp, message, level = 'ALL') {
return {
timestamp,
// @ts-ignore It's ok
level,
message,
};
}
/**
* @typedef {import('appium-adb').LogEntry} LogEntry
*/