-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelement.py
79 lines (61 loc) · 2.26 KB
/
element.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
from py_adb.device_manipulations import DeviceManipulations
class Element(object):
"""
Class to represent the Android element
"""
def __init__(self, raw_xml_element):
self._id = raw_xml_element.get("resource-id")
self._bounds = Element._bounds_parser(raw_xml_element.get("bounds"))
self._center_point = Element.get_element_center_point(self._bounds)
# TODO: Check for text value in the element and convert it
self._content_desc = raw_xml_element.get("content-desc")
self._is_enable = raw_xml_element.get("enabled")
self._is_clickable = raw_xml_element.get("clickable")
# TODO: Check tags in the mobile
self._tag = "TODO"
def __repr__(self):
return "Element: \nResource-ID - <{id}> \nContent desc - <{content_desc}> "\
.format(id=self._id, content_desc=self._content_desc)
@property
def x(self):
"""
Return center x of Element
"""
return self._center_point.get("x")
@property
def y(self):
"""
Return center y of Element
"""
return self._center_point.get("y")
@classmethod
def get_element_center_point(cls, raw_bounds):
"""
Helper method to get a center points for the element
# [X1-616, Y1-48][X2-720, Y2-144]
The center counting by - X1 + (X1 + X2)/2
"""
x = int(raw_bounds[0] + ((raw_bounds[2] - raw_bounds[0]) / 2))
y = int(raw_bounds[1] + ((raw_bounds[3] - raw_bounds[1]) / 2))
return {"x": x, "y": y}
@staticmethod
def _bounds_parser(str_bounds: str) -> list:
"""
Helper method to parce string like [8,56][712,165]
:param str_bounds:
:return list of int [8, 56, 712, 165]:
"""
raw = str_bounds.replace("[", "").replace("]", ",").split(",")
return list(map(int, list(filter(None, raw))))
def tap(self, dev_id: str):
"""
Method to tap on the element
"""
DeviceManipulations.tap(dev_id, self.x, self.y)
def type_text(self, dev_id: str, text: str):
"""
Method send text to the Element
:param dev_id:
:param text:
"""
DeviceManipulations.send_text(dev_id, text)