forked from LordAmit/mobile-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobicomonkey.py
288 lines (228 loc) · 9.65 KB
/
mobicomonkey.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import config_reader as config
from emulator import Emulator
import api_commands
from apk import Apk
import emulator_manager
from xml.dom import minidom # type: ignore
from xml_element import XML_Element
from adb_settings import AdbSettings
from telnet_connector import TelnetAdb
import random
from typing import List
from threading import Thread
from adb_settings import KeyboardEvent
import os
import util
import monkey
from adb_logcat import Logcat, TestType
import mutex
dir = os.path.dirname(__file__)
eventFile = os.path.join(dir, 'test/EventLog')
StopFlagWatcher = os.path.join(dir, 'test/StopFlagWatcher')
eventlog = open(eventFile, 'w')
def reset_emulator(
host: str = config.LOCALHOST,
emulator_port: str = config.EMULATOR_PORT):
print("resetting emulator")
telnet_object = TelnetAdb(host, int(emulator_port))
telnet_object.reset_all()
adb_device = AdbSettings("emulator-" + str(emulator_port))
adb_device.reset_all()
def start_test():
apk = Apk(config.APK_FULL_PATH)
emulator = emulator_manager.get_adb_instance_from_emulators(
config.EMULATOR_NAME)
adb_settings = AdbSettings("emulator-" + str(emulator.port))
activities = []
log = Logcat(emulator, apk, TestType.MobileMonkey)
if config.GUIDED_APPROACH == 3:
monkey.run_contexts_only(
emulator, config.EMULATOR_NAME, config.EMULATOR_PORT,
config.SEED, log)
return
log.start_logcat()
activity = os.path.join(dir, 'test/activity')
activity_list = os.path.join(dir, 'test/activity_list')
if config.GUIDED_APPROACH == 2:
# 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')
os.remove(activity)
print(len(activities))
seed = config.SEED
for activity in activities:
try:
api_commands.adb_start_activity(emulator, apk, activity)
except Exception:
print(Exception)
threads = []
threads.append(Thread(target=monkey.run, args=(
emulator, apk, config.EMULATOR_NAME, config.EMULATOR_PORT,
seed, log)))
# monkey.run(emulator, apk, config.EMULATOR_NAME,
# config.EMULATOR_PORT, seed, log)
# threads.append(Thread(target=test_ui, args=(
# activity, emulator, adb_settings, display_height)))
from adb_monkey import AdbMonkey
ad_monkey = AdbMonkey(emulator, apk, seed, config.DURATION)
# ad_monkey.start_monkey()
threads.append(Thread(target=ad_monkey.start_monkey))
[thread.start() for thread in threads]
[thread.join() for thread in threads]
seed = seed + 1
log.stop_logcat()
eventlog.close()
return
if config.GUIDED_APPROACH == 1:
# file = open('test/activity_list', 'r')
file = open(activity_list, 'r')
for l in file.readlines():
activities.append(l.strip())
print(l.strip())
file.close()
else:
# 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')
# file2 = open('test/activity_list', 'w')
file = open(activity, 'r')
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')
os.remove(activity)
print(len(activities))
display_properties = api_commands.adb_display_properties().decode()
if 'DisplayDeviceInfo' in display_properties:
arr = display_properties.split('height=')[1]
display_height = arr.split(',')[0]
seed = config.SEED
for activity in activities:
try:
api_commands.adb_start_activity(emulator, apk, activity)
except Exception:
print(Exception)
threads = []
threads.append(Thread(target=monkey.run, args=(
emulator, apk, config.EMULATOR_NAME, config.EMULATOR_PORT,
seed, log)))
# monkey.run(emulator, apk, config.EMULATOR_NAME,
# config.EMULATOR_PORT, seed, log)
threads.append(Thread(target=test_ui, args=(
activity, emulator, adb_settings, display_height)))
[thread.start() for thread in threads]
[thread.join() for thread in threads]
seed = seed + 1
log.stop_logcat()
eventlog.close()
def force_update_element_list(emulator: Emulator, adb_settings: AdbSettings):
print("found mutex = " + str(mutex.ROTATION_MUTEX))
print("element list force update here.")
element_list = get_elements_list(emulator, adb_settings)
mutex.ROTATION_MUTEX = 0
print("reset the MUTEX after update.")
return element_list
def test_ui(activity: str, emulator: Emulator, adb_settings: AdbSettings,
display_height: str):
# file = open('test/StopFlagWatcher', 'w')
file = open(StopFlagWatcher, 'w')
file.truncate()
element_list = get_elements_list(emulator, adb_settings)
while len(element_list) > 0:
traverse_elements(activity, element_list, emulator, adb_settings)
previous_elements = element_list
api_commands.adb_display_scroll("{}".format(
int(display_height) - int(display_height) / 10))
element_list = get_elements_list(emulator, adb_settings)
element_list = element_list_compare(previous_elements, element_list)
file.write("1")
def element_list_compare(previous_elements: List[XML_Element],
current_elements: List[XML_Element]):
for previous_item in previous_elements:
for current_item in current_elements:
if previous_item.resource_id == current_item.resource_id:
print('matched')
current_elements.remove(current_item)
return current_elements
def traverse_elements(activity: str, element_list: List[XML_Element],
emulator: Emulator, adb_settings: AdbSettings):
for i in range(0, len(element_list)):
if(mutex.ROTATION_MUTEX):
element_list = element_list_compare(element_list[0:i],
force_update_element_list(
emulator, adb_settings))
for j in range(0, len(element_list)):
input_key_event(
activity, element_list[j], emulator, adb_settings)
break
# print(item.resource_id, item.xpos, item.ypos)
input_key_event(activity, element_list[i], emulator, adb_settings)
def input_key_event(activity: str, item: XML_Element,
emulator: Emulator, adb_settings: AdbSettings):
api_commands.adb_input_tap(emulator, item.xpos, item.ypos)
rand = random.randint(config.MINIMUM_KEYEVENT, config.MAXIMUM_KEYEVENT)
for i in range(rand):
KeyCode = KeyboardEvent(random.randint(0, 40)).name
print("Sending event " + KeyCode)
adb_settings.adb_send_key_event_test(KeyCode)
eventlog.write(util.return_current_time_in_logcat_style() + '\t' +
activity + '\t' + item.resource_id +
'\t' + KeyCode + '\n')
adb_settings.adb_send_key_event_test("KEYCODE_BACK")
def get_elements_list(emulator: Emulator, adb_settings: AdbSettings) -> List:
xmldoc = minidom.parse(api_commands.adb_uiautomator_dump(emulator))
element_list = []
itemlist = xmldoc.getElementsByTagName('node')
for s in itemlist:
if s.attributes['class'].value in ["android.widget.EditText"]:
bounds = s.attributes['bounds'].value.split("][")
Lpos = bounds[0][1:]
Rpos = bounds[1][:len(bounds[1]) - 1]
x1 = int(Lpos.split(",")[0])
y1 = int(Lpos.split(",")[1])
x2 = int(Rpos.split(",")[0])
y2 = int(Rpos.split(",")[1])
x = XML_Element(s.attributes['resource-id'].value,
s.attributes['class'].value,
s.attributes['checkable'].value,
s.attributes['checked'].value,
s.attributes['clickable'].value,
s.attributes['enabled'].value,
s.attributes['focusable'].value,
s.attributes['focused'].value,
s.attributes['scrollable'].value,
s.attributes['long-clickable'].value,
s.attributes['password'].value,
s.attributes['selected'].value,
(x1 + x2) / 2,
(y1 + y2) / 2)
element_list.append(x)
return element_list
if __name__ == '__main__':
file = open(StopFlagWatcher, 'w')
file.truncate()
file.close()
start_test()
reset_emulator()