@@ -7,20 +7,47 @@ module Android
7
7
private
8
8
9
9
# @private
10
- def _button_visible_selectors
10
+ def _button_visible_selectors_xpath
11
11
"//#{ Button } |#{ ImageButton } "
12
12
end
13
13
14
+ def _button_visible_selectors ( opts = { } )
15
+ button_index = opts . fetch :button_index , false
16
+ image_button_index = opts . fetch :image_button_index , false
17
+
18
+ if button_index && image_button_index
19
+ "new UiSelector().className(#{ Button } ).instance(#{ button_index } );" \
20
+ "new UiSelector().className(#{ ImageButton } ).instance(#{ image_button_index } );"
21
+ else
22
+ "new UiSelector().className(#{ Button } );" \
23
+ "new UiSelector().className(#{ ImageButton } );"
24
+ end
25
+ end
26
+
14
27
# @private
28
+ # For automationName is uiautomator2
29
+ def _button_exact_string_xpath ( value )
30
+ string_visible_exact_xpath ( Button , value ) +
31
+ string_visible_exact_xpath ( ImageButton , value ) . sub ( /\A \/ \/ / , '|' )
32
+ end
33
+
15
34
def _button_exact_string ( value )
16
- string_visible_exact ( Button , value ) +
17
- string_visible_exact ( ImageButton , value ) . sub ( /\A \/ \/ / , '|' )
35
+ button = string_visible_exact Button , value
36
+ image_button = string_visible_exact ImageButton , value
37
+ button + image_button
18
38
end
19
39
20
40
# @private
41
+ # For automationName is uiautomator2
42
+ def _button_contains_string_xpath ( value )
43
+ string_visible_contains_xpath ( Button , value ) +
44
+ string_visible_contains_xpath ( ImageButton , value ) . sub ( /\A \/ \/ / , '|' )
45
+ end
46
+
21
47
def _button_contains_string ( value )
22
- string_visible_contains ( Button , value ) +
23
- string_visible_contains ( ImageButton , value ) . sub ( /\A \/ \/ / , '|' )
48
+ button = string_visible_contains Button , value
49
+ image_button = string_visible_contains ImageButton , value
50
+ button + image_button
24
51
end
25
52
26
53
public
@@ -36,49 +63,88 @@ def button(value)
36
63
index = value
37
64
raise "#{ index } is not a valid index. Must be >= 1" if index <= 0
38
65
39
- result = find_elements ( :xpath , _button_visible_selectors ) [ value - 1 ]
40
- raise Selenium ::WebDriver ::Error ::NoSuchElementError unless result
66
+ if automation_name_is_uiautomator2?
67
+ result = find_elements ( :xpath , _button_visible_selectors_xpath ) [ value - 1 ]
68
+ raise Selenium ::WebDriver ::Error ::NoSuchElementError unless result
69
+ else
70
+ result = find_element :uiautomator , _button_visible_selectors ( index : index )
71
+ end
72
+
41
73
return result
42
74
end
43
75
44
- find_element :xpath , _button_contains_string ( value )
76
+ if automation_name_is_uiautomator2?
77
+ find_element :xpath , _button_contains_string_xpath ( value )
78
+ else
79
+ find_element :uiautomator , _button_contains_string ( value )
80
+ end
45
81
end
46
82
47
83
# Find all buttons containing value.
48
84
# If value is omitted, all buttons are returned.
49
85
# @param value [String] the value to search for
50
86
# @return [Array<Button>]
51
87
def buttons ( value = false )
52
- return find_elements :xpath , _button_visible_selectors unless value
53
- find_elements :xpath , _button_contains_string ( value )
88
+ if automation_name_is_uiautomator2?
89
+ return find_elements :xpath , _button_visible_selectors_xpath unless value
90
+ find_elements :xpath , _button_contains_string_xpath ( value )
91
+ else
92
+ return find_elements :uiautomator , _button_visible_selectors unless value
93
+ find_elements :uiautomator , _button_contains_string ( value )
94
+ end
54
95
end
55
96
56
97
# Find the first button.
57
98
# @return [Button]
58
99
def first_button
59
- find_element :xpath , _button_visible_selectors
100
+ if automation_name_is_uiautomator2?
101
+ find_element :xpath , _button_visible_selectors_xpath
102
+ else
103
+ find_element :uiautomator , _button_visible_selectors ( button_index : 0 , image_button_index : 0 )
104
+ end
60
105
end
61
106
62
107
# Find the last button.
63
108
# @return [Button]
64
109
def last_button
65
- result = find_elements ( :xpath , _button_visible_selectors ) . last
66
- raise Selenium ::WebDriver ::Error ::NoSuchElementError unless result
67
- result
110
+ if automation_name_is_uiautomator2?
111
+ result = find_elements ( :xpath , _button_visible_selectors_xpath ) . last
112
+ raise Selenium ::WebDriver ::Error ::NoSuchElementError unless result
113
+ result
114
+ else
115
+ # uiautomator index doesn't support last
116
+ # and it's 0 indexed
117
+ button_index = tags ( Button ) . length
118
+ button_index -= 1 if button_index > 0
119
+ image_button_index = tags ( ImageButton ) . length
120
+ image_button_index -= 1 if image_button_index > 0
121
+
122
+ find_element :uiautomator ,
123
+ _button_visible_selectors ( button_index : button_index ,
124
+ image_button_index : image_button_index )
125
+ end
68
126
end
69
127
70
128
# Find the first button that exactly matches value.
71
129
# @param value [String] the value to match exactly
72
130
# @return [Button]
73
131
def button_exact ( value )
74
- find_element :xpath , _button_exact_string ( value )
132
+ if automation_name_is_uiautomator2?
133
+ find_element :xpath , _button_exact_string_xpath ( value )
134
+ else
135
+ find_element :uiautomator , _button_exact_string ( value )
136
+ end
75
137
end
76
138
77
139
# Find all buttons that exactly match value.
78
140
# @param value [String] the value to match exactly
79
141
# @return [Array<Button>]
80
142
def buttons_exact ( value )
81
- find_elements :xpath , _button_exact_string ( value )
143
+ if automation_name_is_uiautomator2?
144
+ find_elements :xpath , _button_exact_string_xpath ( value )
145
+ else
146
+ find_elements :uiautomator , _button_exact_string ( value )
147
+ end
82
148
end
83
149
end # module Android
84
150
end # module Appium
0 commit comments