-
Notifications
You must be signed in to change notification settings - Fork 0
/
guide_mobile.py
341 lines (278 loc) · 10.5 KB
/
guide_mobile.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import re
import os
import pytest
import textwrap
from appium import webdriver
from PIL import Image, ImageDraw, ImageFont
import io
import time
from IPython.display import display
from appium.webdriver.common.touch_action import TouchAction
# def load_driver():
# # calling_request = request._pyfuncitem.name
# driver = webdriver.Remote(
# command_executor= 'http://127.0.0.1:4723/wd/hub',
# desired_capabilities={
# 'platformName': 'Android',
# 'automationName': 'UIAutomator2',
# 'platformVersion': '8.0',
# 'deviceName': '0045816737',
# "appPackage": "com.google.android.calculator",
# "appActivity": "com.android.calculator2.Calculator",
# "noReset": "true",
# "full-reset": "false"
# }
# )
# driver.implicitly_wait(10)
# return driver
def load_driver():
# calling_request = request._pyfuncitem.name
driver = webdriver.Remote(
command_executor= 'http://127.0.0.1:4723/wd/hub',
desired_capabilities={
'automationName': 'UIAutomator2',
'platformName': 'Android',
'platformVersion': '8.0',
"noReset": "true",
"full-reset": "false",
"deviceName": "0045816737",
"appPackage": "com.google.android.apps.translate",
"appActivity": "com.google.android.apps.translate.TranslateActivity"
}
)
driver.implicitly_wait(10)
return driver
driver = None
actions = None
# Comandos basicos
def init():
global driver
driver = load_driver()
actions = TouchAction(driver)
def clickById(id):
global driver
driver.find_element_by_id(id).click()
def clickByAccessibilityId(accessibilityId):
global driver
driver.find_element_by_accessibility_id(accessibilityId).click()
def clickByXPath(xpath):
global driver
driver.find_element_by_xpath(xpath).click()
def sendKeysById(id, text):
global driver
driver.find_element_by_id(id).send_keys(text)
def sendKeysByAccessibilityId(accessibilityId, text):
global driver
driver.find_element_by_accessibility_id(accessibilityId).send_keys(text)
def sendKeysByXpath(xpath, text):
global driver
driver.find_element_by_xpath(xpath).send_keys(text)
def getTextById(id):
global driver
return driver.find_element_by_id(id).text
def getTextByAccessibilityId(accessibilityId):
global driver
return driver.find_element_by_accessibility_id(accessibilityId).text
def getTextByXpath(xpath):
global driver
return driver.find_element_by_xpath(xpath).text
def scrolltoElementById(id):
global driver, actions
actions = TouchAction(driver)
element = driver.find_element_by_id(id)
print (element)
actions.move_to(el=element).perform()
def scrolltoElementByAccessibilityId(accessibilityId):
global driver, actions
actions = TouchAction(driver)
element = driver.find_element_by_accessibility_id(accessibilityId)
print (element)
actions.move_to(el=element).perform()
def scrolltoElementByXpath(xpath):
global driver, actions
actions = TouchAction(driver)
element = driver.find_element_by_xpath(xpath)
print (element)
actions.move_to(el=element).perform()
def quit():
global driver
driver.quit()
# Comandos para captura de tela
def takeScreenshot():
img = Image.open(io.BytesIO(driver.get_screenshot_as_png()))
basewidth = 250
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
display(img)
def takeScreenshotElementById(id):
global driver
element = driver.find_element_by_id(id)
location = element.location
size = element.size
png = driver.get_screenshot_as_png()
img = Image.open(io.BytesIO(png))
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
img = img.crop((int(left), int(top), int(right), int(bottom)))
display(img)
def takeScreenshotElementAccessibilityId(accessibilityId):
global driver
element = driver.find_element_by_accessibility_id(accessibilityId)
location = element.location
size = element.size
png = driver.get_screenshot_as_png()
img = Image.open(io.BytesIO(png))
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
img = img.crop((int(left), int(top), int(right), int(bottom)))
display(img)
def takeScreenshotElementXpath(id):
global driver
element = driver.find_element_by_xpath(id)
location = element.location
size = element.size
png = driver.get_screenshot_as_png()
img = Image.open(io.BytesIO(png))
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
img = img.crop((int(left), int(top), int(right), int(bottom)))
display(img)
def highlightElementById(id, rectangleWidht = 5):
global driver
idList = id.split(',')
img = Image.open(io.BytesIO(driver.get_screenshot_as_png())).convert('RGB')
for idItem in idList:
element = driver.find_element_by_id(idItem.strip())
location = element.location
size = element.size
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
draw = ImageDraw.Draw(img)
draw.rectangle(((int(left), int(top)), (int(right), int(bottom))), outline="#FF0000", width=rectangleWidht)
basewidth = 250
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
display(img)
def highlightElementByAccessibilityId(accessibilityId, rectangleWidht = 5):
global driver
idList = accessibilityId.split(',')
img = Image.open(io.BytesIO(driver.get_screenshot_as_png())).convert('RGB')
for idItem in idList:
element = driver.find_element_by_accessibility_id(idItem.strip())
location = element.location
size = element.size
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
draw = ImageDraw.Draw(img)
draw.rectangle(((int(left), int(top)), (int(right), int(bottom))), outline="#FF0000", width=rectangleWidht)
basewidth = 250
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
display(img)
def highlightElementByXpath(xpath, rectangleWidht = 5):
global driver
xpathList = xpath.split(',')
img = Image.open(io.BytesIO(driver.get_screenshot_as_png())).convert('RGB')
for xpathItem in xpathList:
element = driver.find_element_by_xpath(xpathItem.strip())
location = element.location
size = element.size
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
draw = ImageDraw.Draw(img)
draw.rectangle(((int(left), int(top)), (int(right), int(bottom))), outline="#FF0000", width=rectangleWidht)
basewidth = 250
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
display(img)
# Operations with touch
# tap
def tapById(id, pCount=1):
global driver, actions
actions = TouchAction(driver)
actions.tap(driver.find_element_by_id(id), count=pCount)
actions.release()
actions.perform()
def tapByAccessibilityId(accessibilityId, pCount=1):
global driver, actions
actions = TouchAction(driver)
actions.tap(driver.find_element_by_accessibility_id(accessibilityId), count=pCount)
actions.release()
actions.perform()
def tapByAccessibilityXpath(xpath, pCount=1):
global driver, actions
actions = TouchAction(driver)
actions.tap(driver.find_element_by_xpath(xpath), count=pCount)
actions.release()
actions.perform()
# press
def pressById(id):
global driver, actions
actions.press(driver.find_element_by_id(id))
actions.release()
actions.perform()
def pressByAccessibilityId(accessibilityId):
global driver, actions
actions.press(driver.find_element_by_accessibility_id(accessibilityId))
actions.release()
actions.perform()
def pressByXpath(xpath):
global driver, actions
actions.press(driver.find_element_by_xpath(xpath))
actions.release()
actions.perform()
# longPress
def longPressById(id, pDuration = 1):
global driver, actions
actions.long_press(driver.find_element_by_id(id), duration=pDuration)
actions.release()
actions.perform()
def longPressByAccessibilityId(accessibilityId, pDuration = 1):
global driver, actions
actions.long_press(driver.find_element_by_accessibility_id(accessibilityId), duration=pDuration)
actions.release()
actions.perform()
def longPressByXpath(id, pDuration = 1):
global driver, actions
actions.long_press(driver.find_element_by_xpath(id), duration=pDuration)
actions.release()
actions.perform()
# moveToDirection
def moveToDirectionById(id, idDirection):
actions.press(driver.find_element_by_id(id))
actions.move_to(driver.find_element_by_id(idDirection))
actions.release()
actions.perform()
def moveToDirectionByAccessibilityId(accessibilityId, idDirection):
actions.press(driver.find_element_by_accessibility_id(accessibilityId))
actions.move_to(driver.find_element_by_accessibility_id(idDirection))
actions.release()
actions.perform()
def moveToDirectionByXpath(xpath, xpathDirection):
actions.press(driver.find_element_by_xpath(xpath))
actions.move_to(driver.find_element_by_xpath(xpathDirection))
actions.release()
actions.perform()
# Funcoes adicionais
def setLandscapeOrientation():
driver.orientation = "LANDSCAPE"
def setPortraitOrientation():
driver.orientation = "PORTRAIT"
def sleep(time_steep):
time.sleep(time_steep)