forked from appium/appium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
appium.js
100 lines (85 loc) · 2.33 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
// Appium webserver controller methods
// https://github.com/hugs/appium/blob/master/appium/appium.py
"use strict";
var routing = require('./routing')
, logger = require('../logger').get('appium')
, UUID = require('uuid-js')
, _ = require('underscore')
, ios = require('./ios');
var Appium = function(args) {
this.args = args;
if (!this.args.verbose) {
logger.transports.console.level = 'warn';
}
this.rest = null;
this.devices = {};
this.active = null;
this.device = null;
this.sessionId = null;
this.desiredCapabilities = {};
this.sessions = [];
this.counter = -1;
this.progress = -1;
};
Appium.prototype.attachTo = function(rest, cb) {
this.rest = rest;
// Import the routing rules
routing(this);
if (cb) {
cb();
}
};
Appium.prototype.start = function(desiredCaps, cb) {
this.desiredCapabilities = desiredCaps;
this.sessions[++this.counter] = { sessionId: '', callback: cb };
this.invoke();
};
Appium.prototype.invoke = function() {
var me = this;
if (this.progress >= this.counter) {
return;
}
if (this.sessionId === null) {
this.sessionId = UUID.create().hex;
logger.info('Creating new appium session ' + this.sessionId);
// in future all the blackberries go here.
this.active = 'iOS';
if (typeof this.devices[this.active] === 'undefined') {
this.devices[this.active] = ios(this.rest, this.args.app, this.args.udid, this.args.verbose, this.args.remove);
}
this.device = this.devices[this.active];
this.device.start(function(err) {
me.progress++;
me.sessions[me.progress].sessionId = me.sessionId;
me.sessions[me.progress].callback(err, me.device);
}, _.bind(me.onDeviceDie, me));
}
};
Appium.prototype.onDeviceDie = function(code, cb) {
var dyingSession = this.sessionId;
this.sessionId = null;
if (code !== null) {
this.devices = {};
this.device = null;
}
if (cb) {
if (this.active !== null) {
this.active = null;
this.invoke();
}
cb(null, {status: 0, value: null, sessionId: dyingSession});
}
};
Appium.prototype.stop = function(cb) {
if (this.sessionId === null) {
return;
}
var me = this;
logger.info('Shutting down appium session...');
this.device.stop(function(code) {
me.onDeviceDie(code, cb);
});
};
module.exports = function(args) {
return new Appium(args);
};