@@ -6,11 +6,6 @@ module Android
66
77 private
88
9- # @private
10- def _button_visible_selectors_xpath
11- "//#{ Button } |#{ ImageButton } "
12- end
13-
149 def _button_visible_selectors ( opts = { } )
1510 button_index = opts . fetch :button_index , false
1611 image_button_index = opts . fetch :image_button_index , false
@@ -24,26 +19,12 @@ def _button_visible_selectors(opts = {})
2419 end
2520 end
2621
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-
3422 def _button_exact_string ( value )
3523 button = string_visible_exact Button , value
3624 image_button = string_visible_exact ImageButton , value
3725 button + image_button
3826 end
3927
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-
4728 def _button_contains_string ( value )
4829 button = string_visible_contains Button , value
4930 image_button = string_visible_contains ImageButton , value
@@ -63,18 +44,18 @@ def button(value)
6344 index = value
6445 raise "#{ index } is not a valid index. Must be >= 1" if index <= 0
6546
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 )
47+ unless automation_name_is_uiautomator2?
48+ return find_element :uiautomator , _button_visible_selectors ( index : index )
7149 end
7250
73- return result
51+ result = find_elements :uiautomator , _button_visible_selectors ( index : index )
52+ raise _no_such_element if result . empty?
53+ return result [ value - 1 ]
7454 end
7555
7656 if automation_name_is_uiautomator2?
77- find_element :xpath , _button_contains_string_xpath ( value )
57+ elements = find_elements :uiautomator , _button_contains_string ( value )
58+ raise_no_such_element_if_empty ( elements )
7859 else
7960 find_element :uiautomator , _button_contains_string ( value )
8061 end
@@ -85,20 +66,16 @@ def button(value)
8566 # @param value [String] the value to search for
8667 # @return [Array<Button>]
8768 def buttons ( value = false )
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
69+ return find_elements :uiautomator , _button_visible_selectors unless value
70+ find_elements :uiautomator , _button_contains_string ( value )
9571 end
9672
9773 # Find the first button.
9874 # @return [Button]
9975 def first_button
10076 if automation_name_is_uiautomator2?
101- find_element :xpath , _button_visible_selectors_xpath
77+ elements = find_elements :uiautomator , _button_visible_selectors ( button_index : 0 , image_button_index : 0 )
78+ raise_no_such_element_if_empty ( elements )
10279 else
10380 find_element :uiautomator , _button_visible_selectors ( button_index : 0 , image_button_index : 0 )
10481 end
@@ -107,18 +84,19 @@ def first_button
10784 # Find the last button.
10885 # @return [Button]
10986 def last_button
87+ # uiautomator index doesn't support last
88+ # and it's 0 indexed
89+ button_index = tags ( Button ) . length
90+ button_index -= 1 if button_index > 0
91+ image_button_index = tags ( ImageButton ) . length
92+ image_button_index -= 1 if image_button_index > 0
93+
11094 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
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 )
11499 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-
122100 find_element :uiautomator ,
123101 _button_visible_selectors ( button_index : button_index ,
124102 image_button_index : image_button_index )
@@ -130,7 +108,8 @@ def last_button
130108 # @return [Button]
131109 def button_exact ( value )
132110 if automation_name_is_uiautomator2?
133- find_element :xpath , _button_exact_string_xpath ( value )
111+ elements = find_elements :uiautomator , _button_exact_string ( value )
112+ raise_no_such_element_if_empty ( elements )
134113 else
135114 find_element :uiautomator , _button_exact_string ( value )
136115 end
@@ -140,11 +119,14 @@ def button_exact(value)
140119 # @param value [String] the value to match exactly
141120 # @return [Array<Button>]
142121 def buttons_exact ( 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
122+ find_elements :uiautomator , _button_exact_string ( value )
123+ end
124+
125+ private
126+
127+ def raise_no_such_element_if_empty ( elements )
128+ raise _no_such_element if elements . empty?
129+ elements . first
148130 end
149131 end # module Android
150132end # module Appium
0 commit comments