-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmonkeylib.py
More file actions
207 lines (168 loc) · 5.43 KB
/
monkeylib.py
File metadata and controls
207 lines (168 loc) · 5.43 KB
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
from __future__ import print_function, division
"""
Provide an interface to the android 'monkey.jar' ui control interface.
"""
import socket
import time
import select
import re
"""
Commands implemented by the daemon running on the Android device.
wake
listvar
getvar <varname>
type <string>
touch {down,up,move} <x> <y>
tap <x> <y>
press <keycode> == key down + key up
key {up,down} <keycode>
quit
deferreturn [event] [timeout (ms)] [command]
flip {open,close}
trackball <dx> <dy>
sleep <msec>
listviews
queryview "viewid" <controlid> <command:gettext,getparent,...>
... commands: getlocation gettext getclass getchecked getenabled getselected
setselected getfocused setfocused getparent getchildren getaccessibilityids
getrootview
getviewswithtext "text"
"""
class Monkey:
"""
Class managing a monkey connection.
"""
def __init__(self, port):
self.sock = socket.socket()
self.sock.connect(("127.0.0.1", port))
def send(self, cmd, timeout=0.5):
self.sock.send((cmd + "\n").encode('utf-8'))
res = self.readuntil(b"\n", timeout)
if res:
return res.decode('utf-8')
def readuntil(self, char, timeout):
self.sock.setblocking(0)
buf = b''
foundchar = False
tstart = time.time()
tend = tstart + timeout
while time.time() < tend:
ready = select.select([self.sock], [], [], 0.1)
if ready[0]:
c = self.sock.recv(1)
if c == char:
foundchar = True
break
buf += c
self.sock.setblocking(1)
if foundchar:
return buf
def keyevent(self, key):
res = self.send("press %s" % key)
return res == "OK"
def key(self, type, key):
res = self.send("key %s %s" % (type, key))
return res == "OK"
def sendtext(self, txt):
res = self.send("type %s" % txt)
return res == "OK"
def wake(self):
res = self.send("wake", 1.0)
return res == "OK"
def drag(self, frm, to, duration, steps):
"""
#0 #1 #(steps-1)
--- down <dt> move <dt> move ... <dt> move <dt> up
t: t0 t0+dur
x: x0 x0+dx x1 x1
"""
dx = (to[0]-frm[0]) / steps
dy = (to[1]-frm[1]) / steps
dt = duration / (steps+1)
tstart = time.time()
tend = tstart + duration
pos = frm
self.touch("down", pos)
for _ in range(steps):
time.sleep(dt)
pos = (pos[0]+ dx, pos[1] + dy)
self.touch("move", pos)
time.sleep(dt)
self.touch("up", to)
def touch(self, how, pos):
res = self.send("touch %s %d %d" % (how, pos[0], pos[1]))
return res == "OK"
def tap(self, pos):
res = self.send("tap %d %d" % (pos[0], pos[1]))
return res == "OK"
def listvar(self):
response = self.send("listvar")
if not response.startswith('OK:'):
return
return response[3:].rstrip(" ").split(" ")
def getvar(self, name):
response = self.send("getvar %s" % name)
if not response.startswith('OK:'):
# .. maybe raise exception with error msg ..
return
return response[3:]
@staticmethod
def launchmonkey(adb):
"""
returns a Monkey object
"""
print("fwd->", adb.forward(12345, 12345))
# or 'toybox killall'
killres = adb.shell("killall -v com.android.commands.monkey") # use -v to report signal result
if killres and bool(re.search(r"killall:.*not found", killres)):
pidline = adb.shell("ps | grep commands.monkey")
if pidline:
m = re.match(r'^\w+\s+(\d+)', pidline)
if m:
pid = int(m.group(1))
killres = adb.shell("kill %d" % pid)
else:
killres = 'process not found'
# todo - when killres is None, the 'killall' probably was never executed.
print("kill->", killres)
time.sleep(0.1)
monkeycmd = adb.makeshell("monkey -v --script-log --port 12345")
if not Monkey.wait_for_monkey(monkeycmd):
print("Failed to start monkey")
return
print("monkey active")
for _ in range(4):
mon = Monkey(12345)
try:
if mon.wake():
break
except Exception as e:
print("trying -> %s" % e)
import traceback
traceback.print_exc()
mon = None
time.sleep(0.5)
if not mon:
print("could not connect to Monkey")
return
monkeycmd.close()
return mon
@staticmethod
def wait_for_monkey(monkeycmd):
"""
wait until the monkey tool has launched without an error.
"""
tstart = time.time()
tend = tstart + 10.0
print("=== waiting for monkey")
while time.time() < tend:
resp = monkeycmd.read()
if resp:
print("monkeycmd -> ", resp)
if resp.find('Error') >= 0:
return
if resp.find(":Monkey:")>=0:
print()
return True
time.sleep(0.5)
print()