15151. name
16162. label (implied by name)
17173. value
18+
19+ Android name = iOS name & label
20+ Android text = iOS value
1821=end
1922
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
2424 # returnElems requires a wrapped $(element).
2525 # set to empty array when length is zero to prevent hang.
2626 #
@@ -30,13 +30,13 @@ def find text
3030 # 2. textFields
3131 # 3. buttons
3232 # 4. elements
33- js = %Q(
33+ %Q(
3434 function isNil( a ) {
3535 return a.type() === 'UIAElementNil';
3636 }
3737
3838 var w = au.mainWindow;
39- var search = "name contains[c] ' #{ text } ' || label contains[c] ' #{ text } ' || value contains[c] ' #{ text } '" ;
39+ var search = #{ predicate } ;
4040 var a = w.secureTextFields().firstWithPredicate(search);
4141 if ( isNil(a) ) {
4242 a = w.textFields().firstWithPredicate(search);
@@ -54,6 +54,27 @@ def find text
5454
5555 au._returnElems($(a));
5656 )
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 } '"
5778
5879 execute_script ( js ) . first
5980end
@@ -64,17 +85,54 @@ def find text
6485def finds text
6586 # returnElems requires a wrapped $(element).
6687 # 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 } '"
7189
72- if ( a.length === 0 ) {
73- a = [];
74- }
90+ execute_script js
91+ end
7592
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 } ''"
78136
79137 execute_script js
80138end
0 commit comments