forked from openatx/uiautomator2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
57 lines (52 loc) · 1.59 KB
/
main.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
'use strict'
var Promise = require('bluebird')
var adb = require('adbkit')
var client = adb.createClient()
var util = require('util')
const { spawn } = require("child_process")
var argv = require('minimist')(process.argv.slice(2))
const serverAddr = argv.server; // Usage: node main.js --server $SERVER_ADDR
function initDevice(device) {
if (device.type != 'device') {
return
}
client.shell(device.id, 'am start -a android.intent.action.VIEW -d http://www.stackoverflow.com')
.then(adb.util.readAll)
.then(function(output) {
var args = ["-m", "uiautomator2", "init", "--serial", device.id]
if (serverAddr) {
args.push("--server", serverAddr);
}
const child = spawn("python", args);
child.stdout.on("data", data => {
process.stdout.write(data)
})
child.stderr.on("data", data => {
process.stderr.write(data)
})
child.on('close', code => {
util.log(`child process exited with code ${code}`);
});
})
}
util.log("tracking device")
if (serverAddr) {
util.log("server %s", serverAddr)
}
client.trackDevices()
.then(function(tracker) {
tracker.on('add', function(device) {
util.log("Device %s(%s) was plugged in", device.id, device.type)
initDevice(device)
})
tracker.on('remove', function(device) {
util.log('Device %s was unplugged', device.id)
})
tracker.on("change", function(device) {
util.log('Device %s was changed to %s', device.id, device.type)
initDevice(device)
})
tracker.on('end', function() {
util.log('Tracking stopped')
})
})