Skip to content

Commit eb11923

Browse files
Fix generic iOS methods
1 parent e62ac44 commit eb11923

File tree

1 file changed

+9
-69
lines changed

1 file changed

+9
-69
lines changed

lib/appium_lib/ios/element/generic.rb

+9-69
Original file line numberDiff line numberDiff line change
@@ -41,69 +41,17 @@ module Appium::Ios
4141
# @param predicate [String] the predicate
4242
# @return [String] the completed JavaScript program
4343
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}");
9046
JS
9147
end
9248

9349
# @private
9450
# @param predicate [String] the predicate
9551
# @return [String] the completed JavaScript program
9652
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}");
10755
JS
10856
end
10957

@@ -112,10 +60,7 @@ def all_ele_js predicate
11260
# @return [Element] the first matching element
11361
def find text
11462
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
11964
end
12065

12166
# Return all elements matching text.
@@ -125,7 +70,6 @@ def finds text
12570
# returnElems requires a wrapped $(element).
12671
# must call toArray when using withPredicate instead of firstWithPredicate.
12772
js = all_ele_js "name contains[c] '#{text}' || label contains[c] '#{text}' || value contains[c] '#{text}'"
128-
12973
execute_script js
13074
end
13175

@@ -134,8 +78,7 @@ def finds text
13478
# @return [Element] the first matching element
13579
def text text
13680
js = first_ele_js "value contains[c] '#{text}'"
137-
138-
execute_script(js).first
81+
execute_script js
13982
end
14083

14184
# Return all elements matching text.
@@ -144,8 +87,7 @@ def text text
14487
def texts text
14588
# XPath //* is not implemented on iOS
14689
# 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}'"
14991
execute_script js
15092
end
15193

@@ -164,11 +106,9 @@ def name name
164106
# @param name [String] the name to search for
165107
# @return [Array<Element>] all matching elements
166108
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
169110
# 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}'"
172112
execute_script js
173113
end
174114
end # module Appium::Ios

0 commit comments

Comments
 (0)