Skip to content

Commit 4f035d3

Browse files
authored
Use uiautomator for uiautomator2 (#561)
* use uiautomator strategy for uiautomator2 * remove xpath related methods * fix rubocop * add comments and define raise method
1 parent 71d8eb6 commit 4f035d3

2 files changed

Lines changed: 47 additions & 67 deletions

File tree

lib/appium_lib/android/element/button.rb

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -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
150132
end # module Appium

lib/appium_lib/android/helper.rb

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ def resource_id(string, on_match)
281281
# Returns a string that matches the first element that contains value
282282
# For automationName is uiautomator2
283283
# example: string_visible_contains_xpath 'UIATextField', 'sign in'
284+
# note for XPath: https://github.com/appium/ruby_lib/pull/561
284285
#
285286
# @param class_name [String] the class name for the element
286287
# @param value [String] the value to search for
@@ -300,6 +301,7 @@ def string_visible_contains_xpath(class_name, value)
300301
# Returns a string that matches the first element that contains value
301302
# For automationName is Appium
302303
# example: string_visible_contains 'UIATextField', 'sign in'
304+
# note for XPath: https://github.com/appium/ruby_lib/pull/561
303305
#
304306
# @param class_name [String] the class name for the element
305307
# @param value [String] the value to search for
@@ -319,27 +321,25 @@ def string_visible_contains(class_name, value)
319321
end
320322

321323
# Find the first element that contains value
322-
# @param element [String] the class name for the element
324+
# @param class_name [String] the class name for the element
323325
# @param value [String] the value to search for
324326
# @return [Element]
325-
def complex_find_contains(element, value)
327+
def complex_find_contains(class_name, value)
326328
if automation_name_is_uiautomator2?
327-
find_element :xpath, string_visible_contains_xpath(element, value)
329+
elements = find_elements :uiautomator, string_visible_contains(class_name, value)
330+
raise _no_such_element if elements.empty?
331+
elements.first
328332
else
329-
find_element :uiautomator, string_visible_contains(element, value)
333+
find_element :uiautomator, string_visible_contains(class_name, value)
330334
end
331335
end
332336

333337
# Find all elements containing value
334-
# @param element [String] the class name for the element
338+
# @param class_name [String] the class name for the element
335339
# @param value [String] the value to search for
336340
# @return [Array<Element>]
337-
def complex_finds_contains(element, value)
338-
if automation_name_is_uiautomator2?
339-
find_elements :xpath, string_visible_contains_xpath(element, value)
340-
else
341-
find_elements :uiautomator, string_visible_contains(element, value)
342-
end
341+
def complex_finds_contains(class_name, value)
342+
find_elements :uiautomator, string_visible_contains(class_name, value)
343343
end
344344

345345
# @private
@@ -384,7 +384,9 @@ def string_visible_exact(class_name, value)
384384
# @return [Element]
385385
def complex_find_exact(class_name, value)
386386
if automation_name_is_uiautomator2?
387-
find_element :xpath, string_visible_exact_xpath(class_name, value)
387+
elements = find_elements :uiautomator, string_visible_exact(class_name, value)
388+
raise _no_such_element if elements.empty?
389+
elements.first
388390
else
389391
find_element :uiautomator, string_visible_exact(class_name, value)
390392
end
@@ -395,11 +397,7 @@ def complex_find_exact(class_name, value)
395397
# @param value [String] the value to search for
396398
# @return [Element]
397399
def complex_finds_exact(class_name, value)
398-
if automation_name_is_uiautomator2?
399-
find_elements :xpath, string_visible_exact_xpath(class_name, value)
400-
else
401-
find_elements :uiautomator, string_visible_exact(class_name, value)
402-
end
400+
find_elements :uiautomator, string_visible_exact(class_name, value)
403401
end
404402

405403
# Returns XML string for the current page

0 commit comments

Comments
 (0)