forked from appium/appium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappium.js
592 lines (528 loc) · 20.9 KB
/
appium.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import _ from 'lodash';
import log from './logger';
import { getBuildInfo, updateBuildInfo, APPIUM_VER } from './config';
import { BaseDriver, errors, isSessionCommand } from 'appium-base-driver';
import B from 'bluebird';
import AsyncLock from 'async-lock';
import { parseCapsForInnerDriver, getPackageVersion, pullSettings } from './utils';
import semver from 'semver';
import wrap from 'word-wrap';
import { EOL } from 'os';
import { util } from 'appium-support';
const PLATFORMS = {
FAKE: 'fake',
ANDROID: 'android',
IOS: 'ios',
APPLE_TVOS: 'tvos',
WINDOWS: 'windows',
MAC: 'mac',
TIZEN: 'tizen',
LINUX: 'linux',
ROKU: 'roku',
WEBOS: 'webos'
};
const AUTOMATION_NAMES = {
APPIUM: 'Appium',
UIAUTOMATOR2: 'UiAutomator2',
UIAUTOMATOR1: 'UiAutomator1',
XCUITEST: 'XCUITest',
YOUIENGINE: 'YouiEngine',
ESPRESSO: 'Espresso',
TIZEN: 'Tizen',
FAKE: 'Fake',
INSTRUMENTS: 'Instruments',
WINDOWS: 'Windows',
MAC: 'Mac',
MAC2: 'Mac2',
FLUTTER: 'Flutter',
SAFARI: 'Safari',
GECKO: 'Gecko',
ROKU: 'Roku',
WEBOS: 'WebOS'
};
const DRIVER_MAP = {
[AUTOMATION_NAMES.UIAUTOMATOR2.toLowerCase()]: {
driverClassName: 'AndroidUiautomator2Driver',
driverPackage: 'appium-uiautomator2-driver',
},
[AUTOMATION_NAMES.XCUITEST.toLowerCase()]: {
driverClassName: 'XCUITestDriver',
driverPackage: 'appium-xcuitest-driver',
},
[AUTOMATION_NAMES.YOUIENGINE.toLowerCase()]: {
driverClassName: 'YouiEngineDriver',
driverPackage: 'appium-youiengine-driver',
},
[AUTOMATION_NAMES.FAKE.toLowerCase()]: {
driverClassName: 'FakeDriver',
driverPackage: 'appium-fake-driver',
},
[AUTOMATION_NAMES.UIAUTOMATOR1.toLowerCase()]: {
driverClassName: 'AndroidDriver',
driverPackage: 'appium-android-driver',
},
[AUTOMATION_NAMES.INSTRUMENTS.toLowerCase()]: {
driverClassName: 'IosDriver',
driverPackage: 'appium-ios-driver',
},
[AUTOMATION_NAMES.WINDOWS.toLowerCase()]: {
driverClassName: 'WindowsDriver',
driverPackage: 'appium-windows-driver',
},
[AUTOMATION_NAMES.MAC.toLowerCase()]: {
driverClassName: 'MacDriver',
driverPackage: 'appium-mac-driver',
},
[AUTOMATION_NAMES.MAC2.toLowerCase()]: {
driverClassName: 'Mac2Driver',
driverPackage: 'appium-mac2-driver',
},
[AUTOMATION_NAMES.ESPRESSO.toLowerCase()]: {
driverClassName: 'EspressoDriver',
driverPackage: 'appium-espresso-driver',
},
[AUTOMATION_NAMES.TIZEN.toLowerCase()]: {
driverClassName: 'TizenDriver',
driverPackage: 'appium-tizen-driver',
},
[AUTOMATION_NAMES.FLUTTER.toLowerCase()]: {
driverClassName: 'FlutterDriver',
driverPackage: 'appium-flutter-driver'
},
[AUTOMATION_NAMES.SAFARI.toLowerCase()]: {
driverClassName: 'SafariDriver',
driverPackage: 'appium-safari-driver'
},
[AUTOMATION_NAMES.GECKO.toLowerCase()]: {
driverClassName: 'GeckoDriver',
driverPackage: 'appium-geckodriver'
},
[AUTOMATION_NAMES.ROKU.toLowerCase()]: {
driverClassName: 'RokuDriver',
driverPackage: 'appium-roku-driver'
},
[AUTOMATION_NAMES.WEBOS.toLowerCase()]: {
driverClassName: 'WebOSDriver',
driverPackage: 'appium-webos-driver'
},
};
const PLATFORMS_MAP = {
[PLATFORMS.FAKE]: () => AUTOMATION_NAMES.FAKE,
[PLATFORMS.ANDROID]: () => {
// Warn users that default automation is going to change to UiAutomator2 for 1.14
// and will become required on Appium 2.0
const logDividerLength = 70; // Fit in command line
const automationWarning = [
`The 'automationName' capability was not provided in the desired capabilities for this Android session`,
`Setting 'automationName=UiAutomator2' by default and using the UiAutomator2 Driver`,
`The next major version of Appium (2.x) will **require** the 'automationName' capability to be set for all sessions on all platforms`,
`In previous versions (Appium <= 1.13.x), the default was 'automationName=UiAutomator1'`,
`If you wish to use that automation instead of UiAutomator2, please add 'automationName=UiAutomator1' to your desired capabilities`,
`For more information about drivers, please visit http://appium.io/docs/en/about-appium/intro/ and explore the 'Drivers' menu`
];
let divider = `${EOL}${_.repeat('=', logDividerLength)}${EOL}`;
let automationWarningString = divider;
automationWarningString += ` DEPRECATION WARNING:` + EOL;
for (let log of automationWarning) {
automationWarningString += EOL + wrap(log, {width: logDividerLength - 2}) + EOL;
}
automationWarningString += divider;
// Recommend users to upgrade to UiAutomator2 if they're using Android >= 6
log.warn(automationWarningString);
return AUTOMATION_NAMES.UIAUTOMATOR2;
},
[PLATFORMS.IOS]: (caps) => {
const platformVersion = semver.valid(semver.coerce(caps.platformVersion));
log.warn(`DeprecationWarning: 'automationName' capability was not provided. ` +
`Future versions of Appium will require 'automationName' capability to be set for iOS sessions.`);
if (platformVersion && semver.satisfies(platformVersion, '>=10.0.0')) {
log.info('Requested iOS support with version >= 10, ' +
`using '${AUTOMATION_NAMES.XCUITEST}' ` +
'driver instead of UIAutomation-based driver, since the ' +
'latter is unsupported on iOS 10 and up.');
return AUTOMATION_NAMES.XCUITEST;
}
return AUTOMATION_NAMES.INSTRUMENTS;
},
[PLATFORMS.APPLE_TVOS]: () => AUTOMATION_NAMES.XCUITEST,
[PLATFORMS.WINDOWS]: () => AUTOMATION_NAMES.WINDOWS,
[PLATFORMS.MAC]: () => AUTOMATION_NAMES.MAC,
[PLATFORMS.TIZEN]: () => AUTOMATION_NAMES.TIZEN,
[PLATFORMS.LINUX]: () => AUTOMATION_NAMES.GECKO,
[PLATFORMS.ROKU]: () => AUTOMATION_NAMES.ROKU,
[PLATFORMS.WEBOS]: () => AUTOMATION_NAMES.WEBOS
};
const desiredCapabilityConstraints = {
automationName: {
presence: false,
isString: true,
inclusionCaseInsensitive: _.values(AUTOMATION_NAMES),
},
platformName: {
presence: true,
isString: true,
inclusionCaseInsensitive: _.keys(PLATFORMS_MAP),
},
};
const sessionsListGuard = new AsyncLock();
const pendingDriversGuard = new AsyncLock();
class AppiumDriver extends BaseDriver {
constructor (args) {
// It is necessary to set `--tmp` here since it should be set to
// process.env.APPIUM_TMP_DIR once at an initial point in the Appium lifecycle.
// The process argument will be referenced by BaseDriver.
// Please call appium-support.tempDir module to apply this benefit.
if (args.tmpDir) {
process.env.APPIUM_TMP_DIR = args.tmpDir;
}
super(args);
this.desiredCapConstraints = desiredCapabilityConstraints;
// the main Appium Driver has no new command timeout
this.newCommandTimeoutMs = 0;
this.args = Object.assign({}, args);
// Access to sessions list must be guarded with a Semaphore, because
// it might be changed by other async calls at any time
// It is not recommended to access this property directly from the outside
this.sessions = {};
// Access to pending drivers list must be guarded with a Semaphore, because
// it might be changed by other async calls at any time
// It is not recommended to access this property directly from the outside
this.pendingDrivers = {};
// allow this to happen in the background, so no `await`
updateBuildInfo();
}
/**
* Cancel commands queueing for the umbrella Appium driver
*/
get isCommandsQueueEnabled () {
return false;
}
sessionExists (sessionId) {
const dstSession = this.sessions[sessionId];
return dstSession && dstSession.sessionId !== null;
}
driverForSession (sessionId) {
return this.sessions[sessionId];
}
getDriverAndVersionForCaps (caps) {
if (!_.isString(caps.platformName)) {
throw new Error('You must include a platformName capability');
}
const platformName = caps.platformName.toLowerCase();
// we don't necessarily have an `automationName` capability
let automationNameCap = caps.automationName;
if (!_.isString(automationNameCap) || automationNameCap.toLowerCase() === 'appium') {
const driverSelector = PLATFORMS_MAP[platformName];
if (driverSelector) {
automationNameCap = driverSelector(caps);
}
}
automationNameCap = _.toLower(automationNameCap);
let failureVerb = 'find';
let suggestion = 'Please check your desired capabilities';
if (_.isPlainObject(DRIVER_MAP[automationNameCap])) {
try {
const {driverPackage, driverClassName} = DRIVER_MAP[automationNameCap];
const driver = require(driverPackage)[driverClassName];
return {
driver,
version: this.getDriverVersion(driver.name, driverPackage),
};
} catch (e) {
log.debug(e);
failureVerb = 'load';
suggestion = 'Please verify your Appium installation';
}
}
const msg = _.isString(caps.automationName)
? `Could not ${failureVerb} a driver for automationName '${caps.automationName}' and platformName ` +
`'${caps.platformName}'`
: `Could not ${failureVerb} a driver for platformName '${caps.platformName}'`;
throw new Error(`${msg}. ${suggestion}`);
}
getDriverVersion (driverName, driverPackage) {
const version = getPackageVersion(driverPackage);
if (version) {
return version;
}
log.warn(`Unable to get version of driver '${driverName}'`);
}
async getStatus () { // eslint-disable-line require-await
return {
build: _.clone(getBuildInfo()),
};
}
async getSessions () {
const sessions = await sessionsListGuard.acquire(AppiumDriver.name, () => this.sessions);
return _.toPairs(sessions)
.map(([id, driver]) => ({id, capabilities: driver.caps}));
}
printNewSessionAnnouncement (driverName, driverVersion) {
const introString = driverVersion
? `Appium v${APPIUM_VER} creating new ${driverName} (v${driverVersion}) session`
: `Appium v${APPIUM_VER} creating new ${driverName} session`;
log.info(introString);
}
/**
* Create a new session
* @param {Object} jsonwpCaps JSONWP formatted desired capabilities
* @param {Object} reqCaps Required capabilities (JSONWP standard)
* @param {Object} w3cCapabilities W3C capabilities
* @return {Array} Unique session ID and capabilities
*/
async createSession (jsonwpCaps, reqCaps, w3cCapabilities) {
const defaultCapabilities = _.cloneDeep(this.args.defaultCapabilities);
const defaultSettings = pullSettings(defaultCapabilities);
jsonwpCaps = _.cloneDeep(jsonwpCaps);
const jwpSettings = Object.assign({}, defaultSettings, pullSettings(jsonwpCaps));
w3cCapabilities = _.cloneDeep(w3cCapabilities);
// It is possible that the client only provides caps using JSONWP standard,
// although firstMatch/alwaysMatch properties are still present.
// In such case we assume the client understands W3C protocol and merge the given
// JSONWP caps to W3C caps
const w3cSettings = Object.assign({}, jwpSettings);
Object.assign(w3cSettings, pullSettings((w3cCapabilities || {}).alwaysMatch || {}));
for (const firstMatchEntry of ((w3cCapabilities || {}).firstMatch || [])) {
Object.assign(w3cSettings, pullSettings(firstMatchEntry));
}
let protocol;
let innerSessionId, dCaps;
try {
// Parse the caps into a format that the InnerDriver will accept
const parsedCaps = parseCapsForInnerDriver(
jsonwpCaps,
w3cCapabilities,
this.desiredCapConstraints,
defaultCapabilities
);
const {desiredCaps, processedJsonwpCapabilities, processedW3CCapabilities, error} = parsedCaps;
protocol = parsedCaps.protocol;
// If the parsing of the caps produced an error, throw it in here
if (error) {
throw error;
}
const {driver: InnerDriver, version: driverVersion} = this.getDriverAndVersionForCaps(desiredCaps);
this.printNewSessionAnnouncement(InnerDriver.name, driverVersion);
if (this.args.sessionOverride) {
await this.deleteAllSessions();
}
let runningDriversData, otherPendingDriversData;
const d = new InnerDriver(this.args);
// We want to assign security values directly on the driver. The driver
// should not read security values from `this.opts` because those values
// could have been set by a malicious user via capabilities, whereas we
// want a guarantee the values were set by the appium server admin
if (this.args.relaxedSecurityEnabled) {
log.info(`Applying relaxed security to '${InnerDriver.name}' as per ` +
`server command line argument. All insecure features will be ` +
`enabled unless explicitly disabled by --deny-insecure`);
d.relaxedSecurityEnabled = true;
}
if (!_.isEmpty(this.args.denyInsecure)) {
log.info('Explicitly preventing use of insecure features:');
this.args.denyInsecure.map((a) => log.info(` ${a}`));
d.denyInsecure = this.args.denyInsecure;
}
if (!_.isEmpty(this.args.allowInsecure)) {
log.info('Explicitly enabling use of insecure features:');
this.args.allowInsecure.map((a) => log.info(` ${a}`));
d.allowInsecure = this.args.allowInsecure;
}
// This assignment is required for correct web sockets functionality inside the driver
d.server = this.server;
try {
runningDriversData = await this.curSessionDataForDriver(InnerDriver);
} catch (e) {
throw new errors.SessionNotCreatedError(e.message);
}
await pendingDriversGuard.acquire(AppiumDriver.name, () => {
this.pendingDrivers[InnerDriver.name] = this.pendingDrivers[InnerDriver.name] || [];
otherPendingDriversData = this.pendingDrivers[InnerDriver.name].map((drv) => drv.driverData);
this.pendingDrivers[InnerDriver.name].push(d);
});
try {
[innerSessionId, dCaps] = await d.createSession(
processedJsonwpCapabilities,
reqCaps,
processedW3CCapabilities,
[...runningDriversData, ...otherPendingDriversData]
);
protocol = d.protocol;
await sessionsListGuard.acquire(AppiumDriver.name, () => {
this.sessions[innerSessionId] = d;
});
} finally {
await pendingDriversGuard.acquire(AppiumDriver.name, () => {
_.pull(this.pendingDrivers[InnerDriver.name], d);
});
}
this.attachUnexpectedShutdownHandler(d, innerSessionId);
log.info(`New ${InnerDriver.name} session created successfully, session ` +
`${innerSessionId} added to master session list`);
// set the New Command Timeout for the inner driver
d.startNewCommandTimeout();
// apply initial values to Appium settings (if provided)
if (d.isW3CProtocol() && !_.isEmpty(w3cSettings)) {
log.info(`Applying the initial values to Appium settings parsed from W3C caps: ` +
JSON.stringify(w3cSettings));
await d.updateSettings(w3cSettings);
} else if (d.isMjsonwpProtocol() && !_.isEmpty(jwpSettings)) {
log.info(`Applying the initial values to Appium settings parsed from MJSONWP caps: ` +
JSON.stringify(jwpSettings));
await d.updateSettings(jwpSettings);
}
} catch (error) {
return {
protocol,
error,
};
}
return {
protocol,
value: [innerSessionId, dCaps, protocol]
};
}
attachUnexpectedShutdownHandler (driver, innerSessionId) {
const removeSessionFromMasterList = (cause = new Error('Unknown error')) => {
log.warn(`Closing session, cause was '${cause.message}'`);
log.info(`Removing session '${innerSessionId}' from our master session list`);
delete this.sessions[innerSessionId];
};
// eslint-disable-next-line promise/prefer-await-to-then
if (_.isFunction((driver.onUnexpectedShutdown || {}).then)) {
// TODO: Remove this block after all the drivers use base driver above v 5.0.0
// Remove the session on unexpected shutdown, so that we are in a position
// to open another session later on.
driver.onUnexpectedShutdown
// eslint-disable-next-line promise/prefer-await-to-then
.then(() => {
// if we get here, we've had an unexpected shutdown, so error
throw new Error('Unexpected shutdown');
})
.catch((e) => {
// if we cancelled the unexpected shutdown promise, that means we
// no longer care about it, and can safely ignore it
if (!(e instanceof B.CancellationError)) {
removeSessionFromMasterList(e);
}
}); // this is a cancellable promise
} else if (_.isFunction(driver.onUnexpectedShutdown)) {
// since base driver v 5.0.0
driver.onUnexpectedShutdown(removeSessionFromMasterList);
} else {
log.warn(`Failed to attach the unexpected shutdown listener. ` +
`Is 'onUnexpectedShutdown' method available for '${driver.constructor.name}'?`);
}
}
async curSessionDataForDriver (InnerDriver) {
const sessions = await sessionsListGuard.acquire(AppiumDriver.name, () => this.sessions);
const data = _.values(sessions)
.filter((s) => s.constructor.name === InnerDriver.name)
.map((s) => s.driverData);
for (let datum of data) {
if (!datum) {
throw new Error(`Problem getting session data for driver type ` +
`${InnerDriver.name}; does it implement 'get ` +
`driverData'?`);
}
}
return data;
}
async deleteSession (sessionId) {
let protocol;
try {
let otherSessionsData = null;
let dstSession = null;
await sessionsListGuard.acquire(AppiumDriver.name, () => {
if (!this.sessions[sessionId]) {
return;
}
const curConstructorName = this.sessions[sessionId].constructor.name;
otherSessionsData = _.toPairs(this.sessions)
.filter(([key, value]) => value.constructor.name === curConstructorName && key !== sessionId)
.map(([, value]) => value.driverData);
dstSession = this.sessions[sessionId];
protocol = dstSession.protocol;
log.info(`Removing session ${sessionId} from our master session list`);
// regardless of whether the deleteSession completes successfully or not
// make the session unavailable, because who knows what state it might
// be in otherwise
delete this.sessions[sessionId];
});
return {
protocol,
value: await dstSession.deleteSession(sessionId, otherSessionsData),
};
} catch (e) {
log.error(`Had trouble ending session ${sessionId}: ${e.message}`);
return {
protocol,
error: e,
};
}
}
async deleteAllSessions (opts = {}) {
const sessionsCount = _.size(this.sessions);
if (0 === sessionsCount) {
log.debug('There are no active sessions for cleanup');
return;
}
const {
force = false,
reason,
} = opts;
log.debug(`Cleaning up ${util.pluralize('active session', sessionsCount, true)}`);
const cleanupPromises = force
? _.values(this.sessions).map((drv) => drv.startUnexpectedShutdown(reason && new Error(reason)))
: _.keys(this.sessions).map((id) => this.deleteSession(id));
for (const cleanupPromise of cleanupPromises) {
try {
await cleanupPromise;
} catch (e) {
log.debug(e);
}
}
}
async executeCommand (cmd, ...args) {
// getStatus command should not be put into queue. If we do it as part of super.executeCommand, it will be added to queue.
// There will be lot of status commands in queue during createSession command, as createSession can take up to or more than a minute.
if (cmd === 'getStatus') {
return await this.getStatus();
}
if (isAppiumDriverCommand(cmd)) {
return await super.executeCommand(cmd, ...args);
}
const sessionId = _.last(args);
const dstSession = await sessionsListGuard.acquire(AppiumDriver.name, () => this.sessions[sessionId]);
if (!dstSession) {
throw new Error(`The session with id '${sessionId}' does not exist`);
}
let res = {
protocol: dstSession.protocol
};
try {
res.value = await dstSession.executeCommand(cmd, ...args);
} catch (e) {
res.error = e;
}
return res;
}
proxyActive (sessionId) {
const dstSession = this.sessions[sessionId];
return dstSession && _.isFunction(dstSession.proxyActive) && dstSession.proxyActive(sessionId);
}
getProxyAvoidList (sessionId) {
const dstSession = this.sessions[sessionId];
return dstSession ? dstSession.getProxyAvoidList() : [];
}
canProxy (sessionId) {
const dstSession = this.sessions[sessionId];
return dstSession && dstSession.canProxy(sessionId);
}
}
// help decide which commands should be proxied to sub-drivers and which
// should be handled by this, our umbrella driver
function isAppiumDriverCommand (cmd) {
return !isSessionCommand(cmd) || cmd === 'deleteSession';
}
export { AppiumDriver };