-
-
Notifications
You must be signed in to change notification settings - Fork 6k
/
appium.js
425 lines (380 loc) · 13.3 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
"use strict";
var loggerjs = require('./server/logger.js')
, logger = loggerjs.get('appium')
, UUID = require('uuid-js')
, _ = require('underscore')
, Capabilities = require('./server/capabilities')
, IOS = require('./devices/ios/ios.js')
, Safari = require('./devices/ios/safari.js')
, Android = require('./devices/android/android.js')
, Selendroid = require('./devices/android/selendroid.js')
, Chrome = require('./devices/android/chrome.js')
, FirefoxOs = require('./devices/firefoxos/firefoxos.js')
, jwpResponse = require('./devices/common.js').jwpResponse
, status = require("./server/status.js");
var DT_IOS = "ios"
, DT_SAFARI = "safari"
, DT_ANDROID = "android"
, DT_CHROME = "chrome"
, DT_SELENDROID = "selendroid"
, DT_FIREFOX_OS = "firefoxos";
var Appium = function (args) {
this.args = _.clone(args);
this.serverArgs = _.clone(args);
this.rest = null;
this.webSocket = null;
this.deviceType = null;
this.device = null;
this.sessionId = null;
this.desiredCapabilities = {};
this.oldDesiredCapabilities = {};
this.session = null;
this.preLaunched = false;
this.sessionOverride = this.args.sessionOverride;
this.resetting = false;
this.defCommandTimeoutMs = this.args.defaultCommandTimeout * 1000;
this.commandTimeoutMs = this.defCommandTimeoutMs;
this.commandTimeout = null;
this.args.callbackAddress = this.args.callbackAddress || this.args.address;
this.args.callbackPort = this.args.callbackPort || this.args.port;
};
Appium.prototype.attachTo = function (rest) {
this.rest = rest;
};
Appium.prototype.attachSocket = function (webSocket) {
this.webSocket = webSocket;
};
Appium.prototype.registerConfig = function (configObj) {
this.serverConfig = configObj;
};
Appium.prototype.deviceIsRegistered = function (deviceType) {
if (deviceType === DT_SAFARI) deviceType = DT_IOS;
if (deviceType === DT_CHROME) deviceType = DT_ANDROID;
return _.has(this.serverConfig, deviceType);
};
Appium.prototype.preLaunch = function (cb) {
logger.info("Pre-launching app");
var caps = {};
this.start(caps, function (err) {
if (err) {
cb(err, null);
} else {
this.preLaunched = true;
cb(null, this);
}
}.bind(this));
};
Appium.prototype.setArgFromCap = function (arg, cap) {
if (typeof this.desiredCapabilities[cap] !== "undefined") {
this.args[arg] = this.desiredCapabilities[cap];
}
};
Appium.prototype.updateResetArgsFromCaps = function () {
this.setArgFromCap("noReset", "noReset");
this.setArgFromCap("fullReset", "fullReset");
// user can set noReset or fullReset
var caps = this.desiredCapabilities;
if (caps.noReset === true) this.args.fullReset = false;
if (caps.fullReset === true) this.args.noReset = false;
// not user visible via caps
this.args.fastReset = !this.args.fullReset && !this.args.noReset;
this.args.skipUninstall = this.args.fastReset || this.args.noReset;
};
Appium.prototype.start = function (desiredCaps, cb) {
this.desiredCapabilities = new Capabilities(desiredCaps);
this.updateResetArgsFromCaps();
this.args.webSocket = this.webSocket; // allow to persist over many sessions
if (this.sessionId === null || this.sessionOverride) {
this.configure(this.args, this.desiredCapabilities, function (err) {
if (err) {
logger.debug("Got configuration error, not starting session");
this.cleanupSession();
cb(err, null);
} else {
this.invoke(cb);
}
}.bind(this));
} else {
return cb(new Error("Requested a new session but one was in progress"));
}
};
Appium.prototype.getDeviceType = function (args, caps) {
// TODO: 'deviceName' and 'platformName' caps are now required, so the majority of this code could be removed
var type = this.getDeviceFromMJSONWP(args, caps) ||
this.getDeviceTypeFromArgsOrNamedApp(args, caps) ||
this.getDeviceTypeFromAppOrPackage(args, caps);
if (type) {
return type;
}
throw new Error("Could not determine your device from Appium arguments " +
"or desired capabilities. Please make sure to specify the " +
"'deviceName' capability");
};
Appium.prototype.getDeviceFromMJSONWP = function (args, caps) {
var platform = caps.platformName || args.platformName;
platform = platform ? platform.toString().toLowerCase() : '';
var browser = caps.browserName || args.browserName;
browser = browser ? browser.toString().toLowerCase() : '';
var automation = caps.automationName || args.automationName;
automation = automation ? automation.toString().toLowerCase() : '';
var validPlatforms = ['ios', 'android', 'firefoxos'];
if (platform && !_.contains(validPlatforms, platform)) {
throw new Error("Could not determine your device. You sent in a " +
"platformName capability of '" + platform + "' but that " +
"is not a supported platform. Supported platforms are: " +
"iOS, Android and FirefoxOS");
}
var type = this.getDeviceTypeFromAutomationName(automation) ||
this.getDeviceTypeFromBrowserName(browser) ||
this.getDeviceTypeFromPlatformCap(platform);
if (type) return type;
};
Appium.prototype.getDeviceTypeFromArgsOrNamedApp = function (args, caps) {
var app = args.app || caps.app;
app = app ? app.toString().toLowerCase() : '';
var type = this.getDeviceTypeFromNamedApp(app) ||
this.getDeviceTypeFromArgs(args);
if (type) return type;
};
Appium.prototype.getDeviceTypeFromAppOrPackage = function (args, caps) {
var app = args.app || caps.app;
app = app ? app.toString().toLowerCase() : '';
var pkg = args.androidPackage || caps.appPackage;
pkg = pkg ? pkg.toString().toLowerCase() : '';
var type = this.getDeviceTypeFromApp(app) ||
this.getDeviceTypeFromPackage(pkg);
if (type) return type;
};
Appium.prototype.getDeviceTypeFromAutomationName = function (automation) {
if (automation.indexOf('selendroid') !== -1) return DT_SELENDROID;
};
Appium.prototype.getDeviceTypeFromBrowserName = function (browser) {
if (browser === "safari") {
return DT_SAFARI;
} else if (_.contains(["chrome", "chromium", "chromebeta", "browser"], browser)) {
return DT_CHROME;
}
};
Appium.prototype.getDeviceTypeFromPlatformCap = function (caps) {
var device = null;
switch (caps) {
case 'ios':
device = DT_IOS;
break;
case 'android':
device = DT_ANDROID;
break;
case 'firefoxos':
device = DT_FIREFOX_OS;
break;
}
return device;
};
Appium.prototype.getDeviceTypeFromDeviceCap = function (device) {
if (device.indexOf('iphone') !== -1) {
return DT_IOS;
} else if (device.indexOf('ipad') !== -1) {
return DT_IOS;
} else if (device.indexOf('selendroid') !== -1) {
return DT_SELENDROID;
} else if (device.indexOf('firefox') !== -1) {
return DT_FIREFOX_OS;
} else if (device.indexOf('android') !== -1) {
return DT_ANDROID;
}
};
Appium.prototype.getDeviceTypeFromNamedApp = function (app) {
if (app === "safari") {
return DT_SAFARI;
} else if (app === "settings") {
return DT_IOS;
} else if (_.contains(["chrome", "chromium", "chromebeta", "browser"], app)) {
return DT_CHROME;
}
};
Appium.prototype.getDeviceTypeFromApp = function (app) {
if (/\.app$/.test(app) || /\.app\.zip$/.test(app)) {
return DT_IOS;
} else if (/\.apk$/.test(app) || /\.apk\.zip$/.test(app)) {
return DT_ANDROID;
}
};
Appium.prototype.getDeviceTypeFromPackage = function (pkg) {
var chromePkgs = [
"com.android.chrome"
, "com.chrome.beta"
, "org.chromium.chrome.testshell"
, "com.android.browser"
];
if (_.contains(chromePkgs, pkg)) {
return DT_CHROME;
} else if (pkg) {
return DT_ANDROID;
}
};
Appium.prototype.getDeviceTypeFromArgs = function (args) {
if (args.ipa && /\.ipa$/.test(args.ipa.toString().toLowerCase())) {
return DT_IOS;
} else if (args.safari) {
return DT_SAFARI;
} else if (args.forceIphone || args.forceIpad) {
return DT_IOS;
}
};
Appium.prototype.configure = function (args, desiredCaps, cb) {
var deviceType;
try {
deviceType = this.getDeviceType(args, desiredCaps);
if (!args.launch) desiredCaps.checkValidity(deviceType, args.enforceStrictCaps);
} catch (e) {
logger.error(e.message);
return cb(e);
}
if (!this.deviceIsRegistered(deviceType)) {
logger.error("Trying to run a session for device '" + deviceType + "' " +
"but that device hasn't been configured. Run config");
return cb(new Error("Device " + deviceType + " not configured yet"));
}
this.device = this.getNewDevice(deviceType);
this.device.configure(args, desiredCaps, cb);
// TODO: better collaboration between the Appium and Device objects
this.device.onResetTimeout = function () { this.resetTimeout(); }.bind(this);
};
Appium.prototype.invoke = function (cb) {
this.sessionId = UUID.create().hex;
logger.debug('Creating new appium session ' + this.sessionId);
if (this.device.args.autoLaunch === false) {
// if user has passed in desiredCaps.autoLaunch = false
// meaning they will manage app install / launching
if (typeof this.device.noLaunchSetup === "function") {
this.device.noLaunchSetup(function (err) {
if (err) return cb(err);
cb(null, this.device);
}.bind(this));
} else {
cb(null, this.device);
}
} else {
// the normal case, where we launch the device for folks
var onStart = function (err, sessionIdOverride) {
if (sessionIdOverride) {
this.sessionId = sessionIdOverride;
logger.debug("Overriding session id with " +
JSON.stringify(sessionIdOverride));
}
if (err) return this.cleanupSession(err, cb);
logger.debug("Device launched! Ready for commands");
this.setCommandTimeout(this.desiredCapabilities.newCommandTimeout);
cb(null, this.device);
}.bind(this);
this.device.start(onStart, _.once(this.cleanupSession.bind(this)));
}
};
Appium.prototype.getNewDevice = function (deviceType) {
var DeviceClass = (function () {
switch (deviceType) {
case DT_IOS:
return IOS;
case DT_SAFARI:
return Safari;
case DT_ANDROID:
return Android;
case DT_CHROME:
return Chrome;
case DT_SELENDROID:
return Selendroid;
case DT_FIREFOX_OS:
return FirefoxOs;
default:
throw new Error("Tried to start a device that doesn't exist: " +
deviceType);
}
})();
return new DeviceClass();
};
Appium.prototype.timeoutWaitingForCommand = function () {
logger.debug("Didn't get a new command in " + (this.commandTimeoutMs / 1000) +
" secs, shutting down...");
this.stop(function () {
logger.debug("We shut down because no new commands came in");
}.bind(this));
};
Appium.prototype.cleanupSession = function (err, cb) {
logger.debug("Cleaning up appium session");
if (this.commandTimeout) {
clearTimeout(this.commandTimeout);
this.commandTimeout = null;
}
this.commandTimeoutMs = this.defCommandTimeoutMs;
var dyingSession = this.sessionId;
this.sessionId = null;
this.device = null;
this.args = _.clone(this.serverArgs);
this.oldDesiredCapabilities = _.clone(this.desiredCapabilities.desired);
this.desiredCapabilities = {};
if (cb) {
if (err) return cb(err);
cb(null, {status: status.codes.Success.code, value: null,
sessionId: dyingSession});
}
};
Appium.prototype.resetTimeout = function () {
if (this.commandTimeout) {
clearTimeout(this.commandTimeout);
}
if (this.commandTimeoutMs && this.commandTimeoutMs > 0) {
this.commandTimeout = setTimeout(this.timeoutWaitingForCommand.bind(this),
this.commandTimeoutMs);
}
};
Appium.prototype.setCommandTimeout = function (secs, cb) {
if (typeof secs === "undefined") {
secs = this.defCommandTimeoutMs / 1000;
logger.debug("Setting command timeout to the default of " + secs + " secs");
} else {
logger.debug("Setting command timeout to " + secs + " secs");
}
this.commandTimeoutMs = secs * 1000;
this.resetTimeout();
if (typeof cb === "function") {
jwpResponse(null, secs, cb);
}
};
Appium.prototype.stop = function (cb) {
if (this.sessionId === null || this.device === null) {
logger.debug("Trying to stop appium but there's no session, doing nothing");
return cb();
}
logger.info('Shutting down appium session');
this.device.stop(function (err) {
this.cleanupSession(err, cb);
}.bind(this));
};
Appium.prototype.reset = function (cb) {
logger.debug("Resetting app mid-session");
if (typeof this.device.resetAndStartApp === "function") {
logger.debug("Running device specific reset");
this.device.resetAndStartApp(function (err) {
jwpResponse(err, cb);
});
} else {
logger.debug("Running generic full reset");
var oldImpWait = this.device.implicitWaitMs
, oldCommandTimeoutMs = this.commandTimeoutMs
, oldId = this.sessionId;
this.resetting = true;
this.stop(function () {
logger.debug("Restarting app");
this.start(this.oldDesiredCapabilities, function (err) {
if (err) return cb(err);
this.resetting = false;
this.device.implicitWaitMs = oldImpWait;
this.sessionId = oldId;
this.setCommandTimeout(oldCommandTimeoutMs / 1000, cb);
}.bind(this));
}.bind(this));
}
};
module.exports = function (args) {
return new Appium(args);
};