@@ -7,27 +7,43 @@ module Android
7
7
private
8
8
9
9
# @private
10
- def _button_visible_string opts = { }
11
- index = opts . fetch :index , false
12
- if index
13
- %Q(//#{ Button } [#{ index } ] | //#{ ImageButton } [#{ index } ])
10
+ def _button_visible_selectors opts = { }
11
+ button_index = opts . fetch :button_index , false
12
+ image_button_index = opts . fetch :image_button_index , false
13
+
14
+ # complex_find(...)
15
+ # 4 = className(String className)
16
+ # 9 = instance(final int instance)
17
+
18
+ if button_index && image_button_index
19
+ [
20
+ # className().instance()
21
+ [ [ 4 , Button ] , [ 9 , button_index ] ] ,
22
+ # className().instance()
23
+ [ [ 4 , ImageButton ] , [ 9 , image_button_index ] ]
24
+ ]
14
25
else
15
- %Q(//#{ Button } | //#{ ImageButton } )
26
+ [
27
+ # className()
28
+ [ [ 4 , Button ] ] ,
29
+ # className()
30
+ [ [ 4 , ImageButton ] ]
31
+ ]
16
32
end
17
33
end
18
34
19
35
# @private
20
36
def _button_exact_string value
21
37
button = string_visible_exact Button , value
22
38
image_button = string_visible_exact ImageButton , value
23
- " #{ button } | #{ image_button } "
39
+ button + image_button
24
40
end
25
41
26
42
# @private
27
43
def _button_contains_string value
28
44
button = string_visible_contains Button , value
29
45
image_button = string_visible_contains ImageButton , value
30
- " #{ button } | #{ image_button } "
46
+ button + image_button
31
47
end
32
48
33
49
public
@@ -41,47 +57,54 @@ def button value
41
57
# Android needs to combine button and image button to match iOS.
42
58
if value . is_a? Numeric
43
59
index = value
44
- raise "#{ index } is not a valid xpath index. Must be >= 1" if index <= 0
60
+ raise "#{ index } is not a valid index. Must be >= 1" if index <= 0
45
61
46
- return xpath _button_visible_string index : index
62
+ return complex_find _button_visible_selectors index : index
47
63
end
48
64
49
- xpath _button_contains_string value
65
+ complex_find _button_contains_string value
50
66
end
51
67
52
68
# Find all buttons containing value.
53
69
# If value is omitted, all buttons are returned.
54
70
# @param value [String] the value to search for
55
71
# @return [Array<Button>]
56
72
def buttons value = false
57
- return xpaths _button_visible_string unless value
58
- xpaths _button_contains_string value
73
+ return complex_find mode : 'all' , selectors : _button_visible_selectors unless value
74
+ complex_find mode : 'all' , selectors : _button_contains_string ( value )
59
75
end
60
76
61
77
# Find the first button.
62
78
# @return [Button]
63
79
def first_button
64
- xpath _button_visible_string
80
+ complex_find _button_visible_selectors button_index : 0 , image_button_index : 0
65
81
end
66
82
67
83
# Find the last button.
68
84
# @return [Button]
69
85
def last_button
70
- xpath _button_visible_string index : 'last()'
86
+ # uiautomator index doesn't support last
87
+ # and it's 0 indexed
88
+ button_index = tags ( Button ) . length
89
+ button_index -= 1 if button_index >= 0
90
+ image_button_index = tags ( ImageButton ) . length
91
+ image_button_index -= 1 if image_button_index >= 0
92
+
93
+ complex_find _button_visible_selectors button_index : button_index , image_button_index : image_button_index
71
94
end
72
95
73
96
# Find the first button that exactly matches value.
74
97
# @param value [String] the value to match exactly
75
98
# @return [Button]
76
99
def button_exact value
77
- xpath _button_exact_string value
100
+ complex_find _button_exact_string value
78
101
end
79
102
80
103
# Find all buttons that exactly match value.
81
104
# @param value [String] the value to match exactly
82
105
# @return [Array<Button>]
83
106
def buttons_exact value
84
- xpaths _button_exact_string value
107
+ complex_find mode : 'all' , selectors : _button_exact_string ( value )
85
108
end
86
109
end # module Android
87
110
end # module Appium
0 commit comments