forked from LordAmit/mobile-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adb_monkey.py
119 lines (104 loc) · 3.77 KB
/
adb_monkey.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
'''
adb monkey interface
'''
import subprocess
import time
import config_reader as config
import api_commands
from emulator import Emulator
from apk import Apk
PRINT_FLAG = False
class AdbMonkey:
'''
AdbMonkey executes the monkey adb tool
'''
adb = config.adb
def __init__(self, emulator: Emulator, apk: Apk, seed: int,
number_of_events: int)-> None:
api_commands.adb_start_server_safe()
if seed > 500 or seed < 0:
raise ValueError("seed must be within the range of 0 to 500")
if number_of_events > 4200 or number_of_events < 10:
raise ValueError(
"number of events must be at least 10 seconds and at most 4200")
# adb shell pm list packages | grep com.ebooks.ebookreader
self.number_of_events = number_of_events
self.seed = seed
# output = subprocess.check_output(
# [self.adb, 'shell', 'pm', 'list', 'packages', '|', 'grep',
# app_package_name]).decode().strip().split('\r\n')
# util.debug_print(output, len(output), flag=PRINT_FLAG)
output = api_commands.adb_is_package_present(
emulator, apk.package_name)
if output < 1:
raise ValueError("Provided {} not found.".format(apk.package_name))
elif output > 1:
raise ValueError(
"Provided {} is too unspecific," +
" multiple entries found.".format(apk.package_name))
self.app_package_name = apk.package_name
def start_monkey(self):
'''
starts monkey
'''
# adb shell monkey -p com.eatl.appstore -v -v -v -s 314 --throttle
# 1000 100
command = "{} shell monkey -p {} --throttle {} -s {} \
-v -v -v {}".format(
self.adb, self.app_package_name, config.MONKEY_INTERVAL, self.seed,
str(self.number_of_events)
)
print(command)
# subprocess.check_call([
# self.adb, 'shell', 'monkey',
# '-p', self.app_package_name,
# '--throttle', config.MONKEY_INTERVAL,
# # '--pct-touch', '60',
# # '--pct-trackball', '20',
# # '--pct-motion', '20',
# '-s', str(self.seed),
# '-v', '-v', '-v',
# str(self.number_of_events)])
import shlex
subprocess.check_call(shlex.split(command))
print('Monkey finished at: {}'.format(time.ctime()))
def main():
import config_reader as config
from apk import Apk
from adb_settings import AdbSettings
import emulator_manager
from adb_logcat import Logcat, TestType
# print("resetting emulator")
import os
import api_commands
import os
dir = os.path.dirname(__file__)
activity = os.path.join(dir, 'test/activity')
activity_list = os.path.join(dir, 'test/activity_list')
activities = []
apk = Apk(config.APK_FULL_PATH)
emulator = emulator_manager.get_adb_instance_from_emulators(
config.EMULATOR_NAME)
# file = open("test/activity", "w")
file = open(activity, 'w')
file.write(api_commands.adb_get_activity_list(emulator, apk))
file.close()
# file = open('test/activity', 'r')
file = open(activity, 'r')
# file2 = open('test/activity_list', 'w')
file2 = open(activity_list, 'w')
for l in file.readlines():
if 'A: android:name' in l and 'Activity' in l:
arr = l.split('"')
activities.append(arr[1])
file2.write(arr[1] + "\n")
print(arr[1])
file.close()
file2.close()
os.remove('test/activity')
# log = Logcat(emulator, apk, TestType.MobileMonkey)
# log.start_logcat()
AdbMonkey(emulator, apk, config.SEED, config.DURATION*10).start_monkey()
# log.stop_logcat()
if __name__ == '__main__':
main()