@@ -7,20 +7,47 @@ module Android
77 private
88
99 # @private
10- def _button_visible_selectors
10+ def _button_visible_selectors_xpath
1111 "//#{ Button } |#{ ImageButton } "
1212 end
1313
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+
1427 # @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+
1534 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
1838 end
1939
2040 # @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+
2147 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
2451 end
2552
2653 public
@@ -36,49 +63,88 @@ def button(value)
3663 index = value
3764 raise "#{ index } is not a valid index. Must be >= 1" if index <= 0
3865
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+
4173 return result
4274 end
4375
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
4581 end
4682
4783 # Find all buttons containing value.
4884 # If value is omitted, all buttons are returned.
4985 # @param value [String] the value to search for
5086 # @return [Array<Button>]
5187 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
5495 end
5596
5697 # Find the first button.
5798 # @return [Button]
5899 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
60105 end
61106
62107 # Find the last button.
63108 # @return [Button]
64109 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
68126 end
69127
70128 # Find the first button that exactly matches value.
71129 # @param value [String] the value to match exactly
72130 # @return [Button]
73131 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
75137 end
76138
77139 # Find all buttons that exactly match value.
78140 # @param value [String] the value to match exactly
79141 # @return [Array<Button>]
80142 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
82148 end
83149 end # module Android
84150end # module Appium
0 commit comments