1
- # UIAButton methods
2
1
module Appium
3
2
module Android
4
3
Button = 'android.widget.Button' . freeze
5
4
ImageButton = 'android.widget.ImageButton' . freeze
6
5
7
- private
8
-
9
- def _button_visible_selectors ( opts = { } )
10
- button_index = opts . fetch :button_index , false
11
- image_button_index = opts . fetch :image_button_index , false
12
-
13
- if button_index && image_button_index
14
- "new UiSelector().className(#{ Button } ).instance(#{ button_index } );" \
15
- "new UiSelector().className(#{ ImageButton } ).instance(#{ image_button_index } );"
16
- else
17
- "new UiSelector().className(#{ Button } );" \
18
- "new UiSelector().className(#{ ImageButton } );"
19
- end
20
- end
21
-
22
- def _button_exact_string ( value )
23
- button = string_visible_exact Button , value
24
- image_button = string_visible_exact ImageButton , value
25
- button + image_button
26
- end
27
-
28
- def _button_contains_string ( value )
29
- button = string_visible_contains Button , value
30
- image_button = string_visible_contains ImageButton , value
31
- button + image_button
32
- end
33
-
34
- public
35
-
36
6
# Find the first button that contains value or by index.
37
7
# @param value [String, Integer] the value to exactly match.
38
8
# If int then the button at that index is returned.
@@ -44,21 +14,10 @@ def button(value)
44
14
index = value
45
15
raise "#{ index } is not a valid index. Must be >= 1" if index <= 0
46
16
47
- unless automation_name_is_uiautomator2?
48
- return find_element :uiautomator , _button_visible_selectors ( index : index )
49
- end
50
-
51
- result = find_elements :uiautomator , _button_visible_selectors ( index : index )
52
- raise _no_such_element if result . empty?
53
- return result [ value - 1 ]
17
+ return find_element :uiautomator , _button_visible_selectors ( index : index )
54
18
end
55
19
56
- if automation_name_is_uiautomator2?
57
- elements = find_elements :uiautomator , _button_contains_string ( value )
58
- raise_no_such_element_if_empty ( elements )
59
- else
60
- find_element :uiautomator , _button_contains_string ( value )
61
- end
20
+ find_element :uiautomator , _button_contains_string ( value )
62
21
end
63
22
64
23
# Find all buttons containing value.
@@ -73,12 +32,7 @@ def buttons(value = false)
73
32
# Find the first button.
74
33
# @return [Button]
75
34
def first_button
76
- if automation_name_is_uiautomator2?
77
- elements = find_elements :uiautomator , _button_visible_selectors ( button_index : 0 , image_button_index : 0 )
78
- raise_no_such_element_if_empty ( elements )
79
- else
80
- find_element :uiautomator , _button_visible_selectors ( button_index : 0 , image_button_index : 0 )
81
- end
35
+ find_element :uiautomator , _button_visible_selectors ( button_index : 0 , image_button_index : 0 )
82
36
end
83
37
84
38
# Find the last button.
@@ -91,28 +45,16 @@ def last_button
91
45
image_button_index = tags ( ImageButton ) . length
92
46
image_button_index -= 1 if image_button_index > 0
93
47
94
- if automation_name_is_uiautomator2?
95
- elements = find_elements :uiautomator ,
96
- _button_visible_selectors ( button_index : button_index ,
97
- image_button_index : image_button_index )
98
- raise_no_such_element_if_empty ( elements )
99
- else
100
- find_element :uiautomator ,
101
- _button_visible_selectors ( button_index : button_index ,
102
- image_button_index : image_button_index )
103
- end
48
+ find_element :uiautomator ,
49
+ _button_visible_selectors ( button_index : button_index ,
50
+ image_button_index : image_button_index )
104
51
end
105
52
106
53
# Find the first button that exactly matches value.
107
54
# @param value [String] the value to match exactly
108
55
# @return [Button]
109
56
def button_exact ( value )
110
- if automation_name_is_uiautomator2?
111
- elements = find_elements :uiautomator , _button_exact_string ( value )
112
- raise_no_such_element_if_empty ( elements )
113
- else
114
- find_element :uiautomator , _button_exact_string ( value )
115
- end
57
+ find_element :uiautomator , _button_exact_string ( value )
116
58
end
117
59
118
60
# Find all buttons that exactly match value.
@@ -128,5 +70,30 @@ def raise_no_such_element_if_empty(elements)
128
70
raise _no_such_element if elements . empty?
129
71
elements . first
130
72
end
73
+
74
+ def _button_visible_selectors ( opts = { } )
75
+ button_index = opts . fetch :button_index , false
76
+ image_button_index = opts . fetch :image_button_index , false
77
+
78
+ if button_index && image_button_index
79
+ "new UiSelector().className(#{ Button } ).instance(#{ button_index } );" \
80
+ "new UiSelector().className(#{ ImageButton } ).instance(#{ image_button_index } );"
81
+ else
82
+ "new UiSelector().className(#{ Button } );" \
83
+ "new UiSelector().className(#{ ImageButton } );"
84
+ end
85
+ end
86
+
87
+ def _button_exact_string ( value )
88
+ button = string_visible_exact Button , value
89
+ image_button = string_visible_exact ImageButton , value
90
+ button + image_button
91
+ end
92
+
93
+ def _button_contains_string ( value )
94
+ button = string_visible_contains Button , value
95
+ image_button = string_visible_contains ImageButton , value
96
+ button + image_button
97
+ end
131
98
end # module Android
132
99
end # module Appium
0 commit comments