15
15
1. name
16
16
2. label (implied by name)
17
17
3. value
18
+
19
+ Android name = iOS name & label
20
+ Android text = iOS value
18
21
=end
19
22
20
- # Return the first element matching text.
21
- # @param text [String] the text to search for
22
- # @return [Element] the first matching element
23
- def find text
23
+ def first_ele_js predicate
24
24
# returnElems requires a wrapped $(element).
25
25
# set to empty array when length is zero to prevent hang.
26
26
#
@@ -30,13 +30,13 @@ def find text
30
30
# 2. textFields
31
31
# 3. buttons
32
32
# 4. elements
33
- js = %Q(
33
+ %Q(
34
34
function isNil( a ) {
35
35
return a.type() === 'UIAElementNil';
36
36
}
37
37
38
38
var w = au.mainWindow;
39
- var search = "name contains[c] ' #{ text } ' || label contains[c] ' #{ text } ' || value contains[c] ' #{ text } '" ;
39
+ var search = #{ predicate } ;
40
40
var a = w.secureTextFields().firstWithPredicate(search);
41
41
if ( isNil(a) ) {
42
42
a = w.textFields().firstWithPredicate(search);
@@ -54,6 +54,27 @@ def find text
54
54
55
55
au._returnElems($(a));
56
56
)
57
+ end
58
+
59
+ def all_ele_js predicate
60
+ %Q(
61
+ var w = au.mainWindow;
62
+ var search = #{ predicate } ;
63
+ var a = w.elements().withPredicate(search).toArray();
64
+
65
+ if ( a.length === 0 ) {
66
+ a = [];
67
+ }
68
+
69
+ au._returnElems($(a));
70
+ )
71
+ end
72
+
73
+ # Return the first element matching text.
74
+ # @param text [String] the text to search for
75
+ # @return [Element] the first matching element
76
+ def find text
77
+ js = first_ele_js "name contains[c] '#{ text } ' || label contains[c] '#{ text } ' || value contains[c] '#{ text } '"
57
78
58
79
execute_script ( js ) . first
59
80
end
@@ -64,17 +85,54 @@ def find text
64
85
def finds text
65
86
# returnElems requires a wrapped $(element).
66
87
# must call toArray when using withPredicate instead of firstWithPredicate.
67
- js = %Q(
68
- var w = au.mainWindow;
69
- var search = "name contains[c] '#{ text } ' || label contains[c] '#{ text } ' || value contains[c] '#{ text } '";
70
- var a = w.elements().withPredicate(search).toArray();
88
+ js = all_ele_js "name contains[c] '#{ text } ' || label contains[c] '#{ text } ' || value contains[c] '#{ text } '"
71
89
72
- if ( a.length === 0 ) {
73
- a = [];
74
- }
90
+ execute_script js
91
+ end
75
92
76
- au._returnElems($(a));
77
- )
93
+ # Return the first element matching text.
94
+ # @param text [String] the text to search for
95
+ # @return [Element] the first matching element
96
+ def text text
97
+ # TODO: Use XPath index once it's implemented
98
+ # https://github.com/appium/appium/issues/295
99
+ js = first_ele_js "value contains[c] '#{ text } '"
100
+
101
+ execute_script ( js ) . first
102
+ end
103
+
104
+ # Return all elements matching text.
105
+ # @param text [String] the text to search for
106
+ # @return [Array<Element>] all matching elements
107
+ def texts text
108
+ # XPath //* is not implemented on iOS
109
+ # https://github.com/appium/appium/issues/430
110
+ js = all_ele_js "value contains[c] '#{ text } '"
111
+
112
+ execute_script js
113
+ end
114
+
115
+ # Return the first element matching name.
116
+ # on Android name is content description
117
+ # on iOS name is the accessibility label or the text.
118
+ # @param name [String] the name to search for
119
+ # @return [Element] the first matching element
120
+ def name text
121
+ js = first_ele_js "name contains[c] '#{ text } ' || label contains[c] '#{ text } '"
122
+
123
+ execute_script ( js ) . first
124
+ end
125
+
126
+ # Return all elements matching name.
127
+ # on Android name is content description
128
+ # on iOS name is the accessibility label or the text.
129
+ # @param name [String] the name to search for
130
+ # @return [Array<Element>] all matching elements
131
+ def names text
132
+ # find_elements :name is not the same as on Android.
133
+ # it's case sensitive and exact on iOS and not on Android.
134
+ # https://github.com/appium/appium/issues/379
135
+ js = all_ele_js "name contains[c] '#{ text } ' || label contains[c] '#{ text } ''"
78
136
79
137
execute_script js
80
138
end
0 commit comments