-
Notifications
You must be signed in to change notification settings - Fork 26
/
interactions.py
312 lines (283 loc) · 10.1 KB
/
interactions.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import os
import time
import threading
from xml.dom import minidom
import random
ELEVATED = False
def Keycode(number, udid):
runCommand("sudo adb -s {} shell input keyevent {}".format(udid, number))
def InstallAPK(udid, apk):
runCommand('sudo adb -s {} install {}'.format(udid, apk))
def CurrentApp(udid):
app = UiAutomatorToList(udid)[0][3]
return app
def currentApps(device):
return getResponse('adb -s {} shell dumpsys activity'.format(device))
def Scroll(udid, sx, sy, ex, ey, Delay=None):
if Delay == None:
runCommand('sudo adb -s {} shell input swipe {} {} {} {}'.format(udid, sx, sy, ex, ey))
else:
runCommand('sudo adb -s {} shell input swipe {} {} {} {} {}'.format(udid, sx, sy, ex, ey, Delay))
def grabScreenResolution(udid):
a = getResponse('sudo adb -s {} shell dumpsys window | grep "mUnrestrictedScreen"'.format(udid))
a = a.partition(") ")[2]
a, b = a.split("x")
return [int(a), int(b)]
def ConnectedDevices(sudo=None):
connected = []
Command = 'adb kill-server'
runCommand(str(Command))
runCommand('adb devices')
runCommand('adb devices > t.txt')
with open('t.txt') as f:
lines = f.read().splitlines()
lines.remove(lines[-1])
lines.remove(lines[0])
for i in lines:
a = str(i).partition('\tdevic')[0]
connected.append(a)
os.remove('t.txt')
try:
return connected
except:
return
def TakeScreenshot(udid, fileName=None):
if fileName == None:
screenshot = str(udid) + '.png'
else:
screenshot = fileName
Command = "sudo adb -s " + str(udid) + " shell screencap -p | sed 's/\r$//' > " + str(screenshot)
runCommand(str(Command))
return screenshot
def RebootDevice(udid):
timeout = time.time() + 60*3
runCommandcu('sudo adb -s {} reboot'.format(udid))
while CheckConnected(udid) == False:
time.sleep(5)
if time.time() > timeout:
print('Problem Rebooting')
break
def SetBrightness(udid, brightness):
runCommand('sudo adb -s {} shell settings put system screen_brightness {}'.format(udid, brightness))
def GrabUiAutomator(udid):
SavedFile = 'XML/' + str(udid) + '.xml'
Command = "sudo adb -s {} pull $(adb -s {} shell uiautomator dump | grep -oP '[^ ]+.xml') {} > /dev/null".format(udid, udid, SavedFile)
runCommand(Command)
return SavedFile
def getResponse(command):
os.system('{} > tmp'.format(command))
return open('tmp', 'r').read()
def closeKeyboard(udid):
if 'mInputShown=true' in str(getResponse('adb -s {} shell dumpsys input_method | grep mInputShown'.format(udid))):
runCommand('adb -s {} shell input keyevent 111'.format(udid))
time.sleep(1)
def get_num(x):
return int(''.join(ele for ele in x if ele.isdigit()))
def closeKeyboard(udid):
if 'mInputShown=true' in str(getResponse('adb -s {} shell dumpsys input_method | grep mInputShown'.format(udid))):
runCommand('adb -s {} shell input keyevent 111'.format(udid))
time.sleep(1)
def grabText(udid, contains):
closeKeyboard(udid)
filename = GrabUiAutomator(udid)
for items in open(filename, 'r').read().split('<node '):
if str(contains) in str(items):
return str(items).partition('text="')[2].partition('" resource')[0]
def click(udid, ss=False, addx=0, addy=0, resultNum=None, text=None, resource_id=None, class_name=None, index_num=None, package=None, content_desc=None, checkable=None, checked=None, clickable=None, enabled=None, focusable=None, focused=None, scrollable=None, long_clickable=None, password=None, selected=None, bounds=None):
closeKeyboard(udid)
if ss != False:
TakeScreenshot(udid)
filename = GrabUiAutomator(udid)
xmldoc = minidom.parse(filename)
itemlist = xmldoc.getElementsByTagName('node')
ListOfBounds = {}
rotAmt = 0
PreviousCorrect = 0
f = []
for e in locals().keys():
if locals()[e] != None:
f.append(locals()[e])
print("Finding {}".format(f[0]))
for i in range(len(itemlist)):
Correct = 0
if text != None:
if str(text) == itemlist[i].attributes['text'].value:
Correct += 1
if class_name != None:
if str(class_name) == itemlist[i].attributes['class'].value:
Correct += 1
if index_num != None:
if str(index_num) == itemlist[i].attributes['index'].value:
Correct += 1
if resource_id != None:
if str(resource_id) == itemlist[i].attributes['resource-id'].value:
Correct += 1
if package != None:
if str(package) == itemlist[i].attributes['package'].value:
Correct += 1
if content_desc != None:
if str(content_desc) == itemlist[i].attributes['content-desc'].value:
Correct += 1
if checkable != None:
if str(checkable) == itemlist[i].attributes['checkable'].value:
Correct += 1
if checked != None:
if str(checked) == itemlist[i].attributes['checked'].value:
Correct += 1
if clickable != None:
if str(clickable) == itemlist[i].attributes['clickable'].value:
Correct += 1
if enabled != None:
if str(enabled) == itemlist[i].attributes['enabled'].value:
Correct += 1
if focusable != None:
if str(focusable) == itemlist[i].attributes['focusable'].value:
Correct += 1
if focused != None:
if str(focused) == itemlist[i].attributes['focused'].value:
Correct += 1
if scrollable != None:
if str(scrollable) == itemlist[i].attributes['scrollable'].value:
Correct += 1
if long_clickable != None:
if str(long_clickable) == itemlist[i].attributes['long-clickable'].value:
Correct += 1
if password != None:
if str(password) == itemlist[i].attributes['password'].value:
Correct += 1
if selected != None:
if str(selected) == itemlist[i].attributes['selected'].value:
Correct += 1
if Correct > 0 and Correct > PreviousCorrect:
Bounds = itemlist[i].attributes['bounds'].value
if Correct > 0 and resultNum != None:
rotAmt = rotAmt + 1
if rotAmt == resultNum:
Bounds = GetBounds(Bounds)
InputRandomBound(udid, Bounds, addx=addx, addy=addy)
return Bounds
PreviousCorrect = Correct
try:
print Bounds
except:
print("Error on {}".format(f[0]))
print("Retrying...")
time.sleep(10)
filename = GrabUiAutomator(udid)
xmldoc = minidom.parse(filename)
itemlist = xmldoc.getElementsByTagName('node')
ListOfBounds = {}
rotAmt = 0
PreviousCorrect = 0
for i in range(len(itemlist)):
Correct = 0
if text != None:
if str(text) == itemlist[i].attributes['text'].value:
Correct += 1
if class_name != None:
if str(class_name) == itemlist[i].attributes['class'].value:
Correct += 1
if index_num != None:
if str(index_num) == itemlist[i].attributes['index'].value:
Correct += 1
if resource_id != None:
if str(resource_id) == itemlist[i].attributes['resource-id'].value:
Correct += 1
if package != None:
if str(package) == itemlist[i].attributes['package'].value:
Correct += 1
if content_desc != None:
if str(content_desc) == itemlist[i].attributes['content-desc'].value:
Correct += 1
if checkable != None:
if str(checkable) == itemlist[i].attributes['checkable'].value:
Correct += 1
if checked != None:
if str(checked) == itemlist[i].attributes['checked'].value:
Correct += 1
if clickable != None:
if str(clickable) == itemlist[i].attributes['clickable'].value:
Correct += 1
if enabled != None:
if str(enabled) == itemlist[i].attributes['enabled'].value:
Correct += 1
if focusable != None:
if str(focusable) == itemlist[i].attributes['focusable'].value:
Correct += 1
if focused != None:
if str(focused) == itemlist[i].attributes['focused'].value:
Correct += 1
if scrollable != None:
if str(scrollable) == itemlist[i].attributes['scrollable'].value:
Correct += 1
if long_clickable != None:
if str(long_clickable) == itemlist[i].attributes['long-clickable'].value:
Correct += 1
if password != None:
if str(password) == itemlist[i].attributes['password'].value:
Correct += 1
if selected != None:
if str(selected) == itemlist[i].attributes['selected'].value:
Correct += 1
if Correct > 0 and Correct > PreviousCorrect:
Bounds = itemlist[i].attributes['bounds'].value
if Correct > 0 and resultNum != None:
rotAmt = rotAmt + 1
if rotAmt == resultNum:
Bounds = GetBounds(Bounds)
InputRandomBound(udid, Bounds, addy)
return Bounds
PreviousCorrect = Correct
Bounds = GetBounds(Bounds)
InputRandomBound(udid, Bounds, addy)
return Bounds
def GetBounds(tt):
first, ignore, second = tt.partition('][')
first = first.split(',')
second = second.split(',')
AA = int(get_num(first[0]))
AB = int(get_num(first[1]))
BA = int(get_num(second[0]))
BB = int(get_num(second[1]))
Coordinates = [AA, AB, BA, BB]
return Coordinates
def InputRandomBound(udid, Bounds, addy=0, addx=0):
print ("Addx: {} Addy: {}".format(addx, addy))
A = str(random.randint(Bounds[1], Bounds[3]) - addy)
B = str(random.randint(Bounds[0], Bounds[2]) + addx)
print('sudo adb -s {} shell input tap {} {}'.format(udid, B, A))
runCommand('sudo adb -s {} shell input tap {} {}'.format(udid, B, A))
def runCommand(command, forcesudo=False):
if 'sudo' in command.lower():
command = command.replace('sudo ', '')
if ELEVATED == True or forcesudo == True:
os.system('sudo {}'.format(command))
else:
os.system('{}'.format(command))
def InputText(udid, text, delay=False):
if delay != True:
text = str(text).replace("\n", " ")
text = str(text).replace(' ', '%s')
runCommand('adb -s {} shell input text {}'.format(udid, text))
else:
for letters in text:
runCommand('adb -s {} shell input text {}'.format(udid, letters))
time.sleep(random.randint(10, 30) * .01)
def turnDownVolume(udid):
for i in range(10):
runCommand('sudo adb -s {} shell input keyevent 25'.format(udid))
def ClearCache(udid, app):
runCommand('sudo adb -s {} shell pm clear {}'.format(udid, app))
def ForceClose(udid, app):
runCommand('sudo adb -s {} shell am force-stop {}'.format(udid, app))
def StartApplication(udid, app):
runCommand("sudo adb -s {} shell monkey -p {} -c android.intent.category.LAUNCHER 1".format(udid, app))
def resetApplication(udid, app):
if app in str(currentApps(udid)):
ForceClose(udid, app)
runCommand("sudo adb -s {} shell monkey -p {} -c android.intent.category.LAUNCHER 1".format(udid, app))
def valOnScreen(udid, string):
if string not in str(interactions.GrabUiAutomator(udid)):
return False
else:
return True