forked from microdog/wechat_automated_jump_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonkeyrunner.py
129 lines (95 loc) · 3.38 KB
/
monkeyrunner.py
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
import random
import urllib2
import sys
import subprocess
import signal
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyView
device_id = None
remote_url = None
def connect():
print 'Connecting', device_id
device = MonkeyRunner.waitForConnection(timeout=60, deviceId=device_id)
print 'Connected'
return device
def main(device):
raw_input('Press enter to start')
while True:
print 'Taking snapshot...'
snapshot = device.takeSnapshot()
img_data = snapshot.convertToBytes('png')
print 'Requesting...', remote_url
req = urllib2.Request(remote_url, img_data)
req.add_header('Content-Length', str(len(img_data)))
req.add_header('Content-Type', 'application/octet-stream')
try:
res = urllib2.urlopen(req)
except urllib2.HTTPError:
break
press_time = float(res.read()) / 1000.0
res.close()
print 'Long press: %ss' % press_time
start = (random.randint(400, 600), random.randint(400, 600))
end = (random.randint(400, 600), random.randint(400, 600))
print 'Will drag from %s to %s' % (start, end)
device.drag(start, end, press_time, 2)
sleep = 1.75 + 0.75 * random.random()
print('Sleep %ss' % (sleep,))
MonkeyRunner.sleep(sleep)
def exit_handler(signum, frame):
print 'Exiting...'
try:
subprocess.call(
"adb -s %s shell kill -9 $(adb -s %s shell ps | grep monkey | awk '{print $2}')"
% (device_id, device_id),
shell=True)
except Exception, e:
print e
sys.exit(0)
def parse_args():
"""Jython does not provide argparse package, parse arguments by hand."""
def usage(error=None):
if error is not None:
print error
print ''
print """\
usage: monkeyrunner monkeyrunner.py [-h] [--jitter JITTER_FLOAT]
DEVICE_ID REMOTE_URL
arguments:
DEVICE_ID target device id
REMOTE_URL remote server url
optional arguments:
-h, --help show this help message and exit
--jitter JITTER_FLOAT
if set, press time will be multiplied by a random
value in range [1 - JITTER_FLOAT, 1 + JITTER_FLOAT]"""
sys.exit(1 if error is not None else 0)
argv = sys.argv[1:]
if '-h' in argv or '--help' in argv:
usage()
jitter = None
if '--jitter' in argv:
jitter_idx = argv.index('--jitter')
if jitter_idx == len(argv) - 1:
usage('--jitter expects a float number')
try:
jitter = float(argv[jitter_idx + 1])
except ValueError:
usage('--jitter expects a float number, %r got\n' %
(argv[jitter_idx + 1], ))
del argv[jitter_idx:jitter_idx + 2]
if len(argv) != 2:
usage('missing arguments')
device_id, remote_url = argv
return {'jitter': jitter, 'device_id': device_id, 'remote_url': remote_url}
if __name__ == '__main__':
args = parse_args()
device_id = args['device_id']
remote_url = args['remote_url']
print 'Device id:', device_id
print 'Remote url:', remote_url
if args['jitter']:
remote_url += '?' if '?' not in remote_url else '&'
remote_url += 'jitter=%s' % (args['jitter'], )
signal.signal(signal.SIGINT, exit_handler)
device = connect()
main(device)