lib/appium_lib/ios/element/generic.rb in appium_lib-0.3.0 vs lib/appium_lib/ios/element/generic.rb in appium_lib-0.3.1

- old
+ new

@@ -17,11 +17,60 @@ 3. value Android name = iOS name & label Android text = iOS value =end + def ios_js_loaded? method_name + @ios_js.include? method_name + end + def ios_js_load method_name + @ios_js.push method_name + end + + def name_contains_js + # execute_script 'au.mainApp.getNameContains("sign")' + # execute_script 'au.mainApp.getNameContains("zzz")' + <<-JS + UIAElement.prototype.getNameContains = function(targetName) { + var target = UIATarget.localTarget(); + target.pushTimeout(0); + var search = "name contains[c] '" + targetName + "' || label contains[c] '" + targetName + "'"; + var searchElements = function(element) { + var children = element.elements(); + var result = children.firstWithPredicate(search); + if (result.type() !== 'UIAElementNil') { + return result; + } + + for ( var a = 0, len = children.length; a < len; a++) { + result = searchElements(children[a]); + if (result.type() !== 'UIAElementNil') { + return result; + } + } + + return result; + }; + var result = searchElements(this); + target.popTimeout(); + + if (result.type() === 'UIAElementNil') { + return { + status: 7, + value: {'message': 'An element could not be located on the page using the given search parameters.'} + }; + } + + return { + status: 0, + value: {ELEMENT: au.getId(result)} + }; + }; + JS + end + # returnElems requires a wrapped $(element). # set to empty array when length is zero to prevent hang. # # UIAElementNil when not matched # @@ -36,11 +85,11 @@ # window 1 is the 1st webview (if it exists) # must break instead of return because it's not a function. # # single element length is undefined when found and 0 when not found. def first_ele_js predicate - %Q( + <<-JS function isNil( a ) { return a.type() === 'UIAElementNil'; } function search( w ) { @@ -82,25 +131,25 @@ } return au._returnElems( $( [] ) ); } run(); - ) + JS end def all_ele_js predicate - %Q( + <<-JS var w = au.mainWindow; var search = "#{predicate}"; var a = w.elements().withPredicate(search).toArray(); if ( a.length === 0 ) { a = []; } au._returnElems($(a)); - ) + JS end # Return the first element matching text. # @param text [String] the text to search for # @return [Element] the first matching element @@ -147,12 +196,15 @@ # on Android name is content description # on iOS name is the accessibility label or the text. # @param name [String] the name to search for # @return [Element] the first matching element def name name - js = first_ele_js "name contains[c] '#{name}' || label contains[c] '#{name}'" + unless ios_js_loaded? 'name_contains_js' + execute_script name_contains_js + ios_js_load 'name_contains_js' + end - execute_script(js).first + execute_script %(au.mainApp.getNameContains("#{name}")) end # Return all elements matching name. # on Android name is content description # on iOS name is the accessibility label or the text. \ No newline at end of file