@@ -41,69 +41,17 @@ module Appium::Ios
41
41
# @param predicate [String] the predicate
42
42
# @return [String] the completed JavaScript program
43
43
def first_ele_js predicate
44
- <<-JS
45
- function isNil( a ) {
46
- return a.type() === 'UIAElementNil';
47
- }
48
-
49
- function search( w ) {
50
- var search = "#{ predicate } ";
51
- var a = w.secureTextFields().firstWithPredicate(search);
52
- if ( isNil(a) ) {
53
- a = w.textFields().firstWithPredicate(search);
54
- if ( isNil(a) ) {
55
- a = w.buttons().firstWithPredicate(search);
56
- if ( isNil(a) ) {
57
- a = w.elements().firstWithPredicate(search);
58
- }
59
- }
60
- }
61
-
62
- return a;
63
- }
64
-
65
- function search_web( windowIndex ) {
66
- var a = undefined;
67
-
68
- try {
69
- a = UIATarget.localTarget().frontMostApp().windows()[windowIndex].scrollViews()[0].webViews()[0].elements().firstWithPredicate("#{ predicate } ");
70
- } catch(e) {}
71
-
72
- return a;
73
- }
74
-
75
- function run() {
76
- var windows = au.mainApp.windows();
77
- for (var i = 0, len = windows.length; i < len; i++) {
78
- var result = search_web( i );
79
- if ( isNil( result ) ) {
80
- result = search( windows[ i ] );
81
- }
82
- if ( ! isNil( result ) ) {
83
- return au._returnElems( $( [ result ] ) );
84
- }
85
- }
86
- return au._returnElems( $( [] ) );
87
- }
88
-
89
- run();
44
+ ( <<-JS ) . strip # remove trailing newline
45
+ au.mainApp.getFirstWithPredicateWeighted("#{ predicate } ");
90
46
JS
91
47
end
92
48
93
49
# @private
94
50
# @param predicate [String] the predicate
95
51
# @return [String] the completed JavaScript program
96
52
def all_ele_js predicate
97
- <<-JS
98
- var w = au.mainWindow;
99
- var search = "#{ predicate } ";
100
- var a = w.elements().withPredicate(search).toArray();
101
-
102
- if ( a.length === 0 ) {
103
- a = [];
104
- }
105
-
106
- au._returnElems($(a));
53
+ ( <<-JS ) . strip # remove trailing newline
54
+ au.mainApp.getAllWithPredicate("#{ predicate } ");
107
55
JS
108
56
end
109
57
@@ -112,10 +60,7 @@ def all_ele_js predicate
112
60
# @return [Element] the first matching element
113
61
def find text
114
62
js = first_ele_js "name contains[c] '#{ text } ' || label contains[c] '#{ text } ' || value contains[c] '#{ text } '"
115
-
116
- ele = execute_script ( js ) . first
117
- raise Selenium ::WebDriver ::Error ::NoSuchElementError , '' if ele . nil?
118
- ele
63
+ execute_script js
119
64
end
120
65
121
66
# Return all elements matching text.
@@ -125,7 +70,6 @@ def finds text
125
70
# returnElems requires a wrapped $(element).
126
71
# must call toArray when using withPredicate instead of firstWithPredicate.
127
72
js = all_ele_js "name contains[c] '#{ text } ' || label contains[c] '#{ text } ' || value contains[c] '#{ text } '"
128
-
129
73
execute_script js
130
74
end
131
75
@@ -134,8 +78,7 @@ def finds text
134
78
# @return [Element] the first matching element
135
79
def text text
136
80
js = first_ele_js "value contains[c] '#{ text } '"
137
-
138
- execute_script ( js ) . first
81
+ execute_script js
139
82
end
140
83
141
84
# Return all elements matching text.
@@ -144,8 +87,7 @@ def text text
144
87
def texts text
145
88
# XPath //* is not implemented on iOS
146
89
# https://github.com/appium/appium/issues/430
147
- js = all_ele_js "value contains[c] '#{ text } '"
148
-
90
+ js = all_ele_js "value contains[c] '#{ text } '"
149
91
execute_script js
150
92
end
151
93
@@ -164,11 +106,9 @@ def name name
164
106
# @param name [String] the name to search for
165
107
# @return [Array<Element>] all matching elements
166
108
def names name
167
- # find_elements :name is not the same as on Android.
168
- # it's case sensitive and exact on iOS and not on Android.
109
+ # :name is not consistent across iOS and Android so use custom JavaScript
169
110
# https://github.com/appium/appium/issues/379
170
- js = all_ele_js "name contains[c] '#{ name } ' || label contains[c] '#{ name } ''"
171
-
111
+ js = all_ele_js "name contains[c] '#{ name } ' || label contains[c] '#{ name } '"
172
112
execute_script js
173
113
end
174
114
end # module Appium::Ios
0 commit comments