forked from filmaj/appium-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultidroid.js
109 lines (99 loc) · 3.93 KB
/
multidroid.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
var native_test = require('./tests/android-native'),
chrome_test = require('./tests/android-chrome'),
path = require('path'),
n_callbacks = require('ncallbacks'),
spawn = require('child_process').spawn,
exec = require('child_process').exec;
// Path to appium source code.
var appium = '/Users/filmaj/src/appium';
var server = path.resolve(path.join(appium, 'server.js'));
// Start of port number
var port_start = 4273;
// Holds appium instances
var servers = {};
// Cleanup method: kill spawned Appium processes.
function doneskis() {
var count = 0;
Object.keys(servers).forEach(function(id) {
servers[id].kill();
count++;
});
if (count) console.log(count + ' Appium instances shut down, goodbye!');
}
// Hook in cleanup if use Ctrl+C's out of node
process.on('SIGINT', doneskis);
// Run `adb devices` to get a list of connected devices.
exec('adb devices', function(err, stdout, stderr) {
if (err) {
console.error('ERROAR running `adb devices`!');
console.error(err);
} else {
var devices = stdout.split('\n').filter(function(line) {
return line.match(/device$/i);
}).map(function(line) {
return line.split('\t')[0];
});
console.log('Detected ' + devices.length + ' devices.');
var device_list = [];
var end = n_callbacks(devices.length, function() {
start_appium_servers(device_list);
});
// Figure out which devices are rooted.
devices.forEach(function(id) {
exec('adb -s ' + id + ' root', function(err, stdout, stderr) {
if (err) {
console.error('ERROAR running `adb -s <id> root`!');
console.error(err);
} else {
if (stdout.length) {
if (stdout.indexOf('cannot run as root') > -1) {
// No root access :(
console.log('Device ' + id + ' is not rooted, will only be able to deploy native apps.');
device_list.push({id:id,rooted:false});
} else if (stdout.indexOf('disabled') > -1) {
// Root access but it is disabled, be noisy.
console.log('Device ' + id + ' _IS_ rooted but it is not enabled! Enable it under Settings -> Developer Options! In the mean time, will only deploy native apps to it.');
device_list.push({id:id,rooted:false});
} else if (stdout.indexOf('restarting adbd') > -1 || stdout.indexOf('already running as root') >-1) {
// Yay root access!
console.log('Device ' + id + ' _IS_ rooted, yay! We can run Chrome tests on it :D');
device_list.push({id:id,rooted:true});
} else {
console.warn('WARNING! Weird output from `adb root`:');
console.warn(stdout, stderr);
}
end();
}
}
});
});
}
});
function start_appium_servers(devices) {
console.log('Kicking up ' + devices.length + ' appium instances...');
var end = n_callbacks(devices.length, function() {
console.log(devices.length + ' tests successfully finished!');
doneskis();
});
for (var i = 0, n = devices.length; i < n; i++) (function(device) {
var id = device.id;
var started = false;
var appium_port = port_start + (i * 2);
var device_port = port_start + (i * 2) + 1;
servers[id] = spawn('node', [server, '--udid', id, '-p', appium_port, '-dp', device_port]);
servers[id].stderr.on('data', function(data) { console.error(data.toString()); });
servers[id].stdout.on('data', function(data) {
// console.log(id + ' | ' + data);
if (!started && data.toString().indexOf('Appium REST http interface listener started') > -1) {
started = true;
if (device.rooted) {
console.log('Starting Chrome test on device ' + id + '...');
chrome_test(appium_port, end);
} else {
console.log('Starting native Android test on device ' + id + '...');
native_test(appium_port, end);
}
}
});
})(devices[i]);
}