module Appium::Ios
Constants
- UIAButton
- UIASecureTextField
- UIAStaticText
- UIATextField
- UIA_BUTTON
- UIA_SECURE_TEXT_FIELD
- UIA_STATIC_TEXT
- UIA_TEXT_FIELD
- XCUIELEMENT_TYPE_BUTTON
- XCUIELEMENT_TYPE_SECURE_TEXT_FIELD
- XCUIELEMENT_TYPE_STATIC_TEXT
- XCUIELEMENT_TYPE_TEXT_FIELD
- XCUIElementTypeButton
- XCUIElementTypeSecureTextField
- XCUIElementTypeStaticText
- XCUIElementTypeTextField
Public Instance Methods
Source
# File lib/appium_lib/ios/common/helper.rb, line 377 def _all_pred(opts) predicate = opts[:predicate] raise ArgumentError, 'predicate must be provided' unless predicate visible = opts.fetch :visible, true %($.mainApp().getAllWithPredicate("#{predicate}", #{visible});) end
predicate - the predicate to evaluate on the main app
visible - if true, only visible elements are returned. default true
Source
# File lib/appium_lib/ios/common/helper.rb, line 456 def _by_json(opts) valid_keys = %i(typeArray onlyFirst onlyVisible name label value) unknown_keys = opts.keys - valid_keys raise ArgumentError, "Unknown keys: #{unknown_keys}" unless unknown_keys.empty? type_array = opts[:typeArray] raise ArgumentError, 'typeArray must be an array' unless type_array.is_a? Array only_first = opts[:onlyFirst] raise ArgumentError, 'onlyFirst must be a boolean' unless [true, false].include? only_first only_visible = opts[:onlyVisible] raise ArgumentError, 'onlyVisible must be a boolean' unless [true, false].include? only_visible # name/label/value are optional. when searching for class only, then none # will be present. _validate_object opts[:name], opts[:label], opts[:value] # NOTE: that mainWindow is sometimes nil so it's passed as a param # $._elementOrElementsByType will validate that the window isn't nil element_or_elements_by_type = <<-JS (function() { var opts = #{opts.to_json}; var result = false; try { result = $._elementOrElementsByType($.mainWindow(), opts); } catch (e) { } return result; })(); JS res = execute_script element_or_elements_by_type res || raise(Appium::Ios::CommandError, 'mainWindow is nil') end
For Appium(automation name), not XCUITest typeArray - array of string types to search for. Example: [“UIAStaticText”] onlyFirst - boolean. returns only the first result if true. Example: true onlyVisible - boolean. returns only visible elements if true. Example: true target - string. the target value to search for. Example: “Buttons, Various uses of UIButton” substring - boolean. matches on substrings if true otherwise an exact mathc is required. Example: true insensitive - boolean. ignores case sensitivity if true otherwise it’s case sensitive. Example: true
opts = {
typeArray: ["UIAStaticText"], onlyFirst: true, onlyVisible: true, name: { target: "Buttons, Various uses of UIButton", substring: false, insensitive: false, }, label: { target: "Buttons, Various uses of UIButton", substring: false, insensitive: false, }, value: { target: "Buttons, Various uses of UIButton", substring: false, insensitive: false, }
}
Source
# File lib/appium_lib/ios/common/helper.rb, line 406 def _validate_object(*objects) raise ArgumentError, 'objects must be an array' unless objects.is_a? Array objects.each do |obj| next unless obj # obj may be nil. if so, ignore. valid_keys = %i[target substring insensitive] unknown_keys = obj.keys - valid_keys raise ArgumentError, "Unknown keys: #{unknown_keys}" unless unknown_keys.empty? target = obj[:target] raise ArgumentError, 'target must be a string' unless target.is_a? String substring = obj[:substring] raise ArgumentError, 'substring must be a boolean' unless [true, false].include? substring insensitive = obj[:insensitive] raise ArgumentError, 'insensitive must be a boolean' unless [true, false].include? insensitive end end
Source
# File lib/appium_lib/ios/element/alert.rb, line 19 def alert_accept # @driver.switch_to.alert.accept # ".switch_to.alert" calls alert_text so use bridge directly driver.send(:bridge).accept_alert end
Accept the alert. @return [void]
Source
# File lib/appium_lib/ios/element/alert.rb, line 27 def alert_dismiss # @driver.switch_to.alert.dismiss # ".switch_to.alert" calls alert_text so use bridge directly driver.send(:bridge).dismiss_alert end
Dismiss the alert. @return [void]
Source
# File lib/appium_lib/ios/common/helper.rb, line 512 def ele_by_json(opts) opts[:onlyFirst] = true result = _by_json(opts).first raise _no_such_element if result.nil? result end
see eles_by_json
Source
# File lib/appium_lib/ios/common/helper.rb, line 320 def ele_by_json_visible_contains(element, value) ele_by_json string_visible_contains element, value end
Find the first element that contains value. For Appium(automation name), not XCUITest @param element [String] the class name for the element @param value [String] the value to search for @return [Element]
Source
# File lib/appium_lib/ios/common/helper.rb, line 359 def ele_by_json_visible_exact(element, value) ele_by_json string_visible_exact element, value end
Find the first element exactly matching value For Appium(automation name), not XCUITest @param element [String] the class name for the element @param value [String] the value to search for @return [Element]
Source
# File lib/appium_lib/ios/common/helper.rb, line 109 def ele_index(class_name, index) raise ArgumentError, 'Index must be >= 1' unless index == 'last()' || (index.is_a?(Integer) && index >= 1) elements = tags(class_name) if index == 'last()' result = elements.last else # elements array is 0 indexed index -= 1 result = elements[index] end raise _no_such_element if result.nil? result end
Get the element of type class_name at matching index. @param class_name [String] the class name to find @param index [Integer] the index @return [Element]
Source
# File lib/appium_lib/ios/common/helper.rb, line 391 def ele_with_pred(opts) # true = return only visible find_element(:uiautomation, _all_pred(opts)) end
returns element matching predicate contained in the main app
predicate - the predicate to evaluate on the main app
visible - if true, only visible elements are returned. default true @return [Element]
Source
# File lib/appium_lib/ios/common/helper.rb, line 506 def eles_by_json(opts) opts[:onlyFirst] = false _by_json opts end
For Appium(automation name), not XCUITest example usage:
typeArray: ["UIAStaticText"], onlyVisible: true, name: { target: "Buttons, Various uses of UIButton", substring: false, insensitive: false, },
})
Source
# File lib/appium_lib/ios/common/helper.rb, line 329 def eles_by_json_visible_contains(element, value) eles_by_json string_visible_contains element, value end
Find all elements containing value For Appium(automation name), not XCUITest @param element [String] the class name for the element @param value [String] the value to search for @return [Array<Element>]
Source
# File lib/appium_lib/ios/common/helper.rb, line 368 def eles_by_json_visible_exact(element, value) eles_by_json string_visible_exact element, value end
Find all elements exactly matching value For Appium(automation name), not XCUITest @param element [String] the class name for the element @param value [String] the value to search for @return [Element]
Source
# File lib/appium_lib/ios/common/helper.rb, line 402 def eles_with_pred(opts) find_elements(:uiautomation, _all_pred(opts)) end
returns elements matching predicate contained in the main app
predicate - the predicate to evaluate on the main app
visible - if true, only visible elements are returned. default true @return [Array<Element>]
Source
# File lib/appium_lib/ios/element/generic.rb, line 20 def find(value) ele_by_json_visible_contains '*', value end
Find the first element containing value @param value [String] the value to search for @return [Element]
Source
# File lib/appium_lib/ios/common/helper.rb, line 139 def find_ele_by_attr(class_name, attr, value) @driver.find_element :xpath, string_attr_exact(class_name, attr, value) end
Find the first element exactly matching class and attribute value. Note: Uses XPath Note: For XCUITest, this method return ALL elements include displayed or not displayed elements. @param class_name [String] the class name to search for @param attr [String] the attribute to inspect @param value [String] the expected value of the attribute @return [Element]
Source
# File lib/appium_lib/ios/common/helper.rb, line 191 def find_ele_by_attr_include(class_name, attr, value) @driver.find_element :xpath, string_attr_include(class_name, attr, value) end
Get the first tag by attribute that exactly matches value. Note: Uses XPath @param class_name [String] the tag name to match @param attr [String] the attribute to compare @param value [String] the value of the attribute that the element must include @return [Element] the element of type tag who’s attribute includes value
Source
# File lib/appium_lib/ios/common/helper.rb, line 163 def find_ele_by_predicate(class_name: '*', value:) elements = find_eles_by_predicate(class_name: class_name, value: value) raise _no_such_element if elements.empty? elements.first end
Find the first element exactly matching attribute case insensitive value. Note: Uses Predicate @param value [String] the expected value of the attribute @return [Element]
Source
# File lib/appium_lib/ios/common/helper.rb, line 209 def find_ele_by_predicate_include(class_name: '*', value:) elements = find_eles_by_predicate_include(class_name: class_name, value: value) raise _no_such_element if elements.empty? elements.first end
Get the first elements that include insensitive value. Note: Uses Predicate @param value [String] the value of the attribute that the element must include @return [Element] the element of type tag who’s attribute includes value
Source
# File lib/appium_lib/ios/common/helper.rb, line 150 def find_eles_by_attr(class_name, attr, value) @driver.find_elements :xpath, string_attr_exact(class_name, attr, value) end
Find all elements exactly matching class and attribute value. Note: Uses XPath Note: For XCUITest, this method return ALL elements include displayed or not displayed elements. @param class_name [String] the class name to match @param attr [String] the attribute to compare @param value [String] the value of the attribute that the element must have @return [Array<Element>]
Source
# File lib/appium_lib/ios/common/helper.rb, line 201 def find_eles_by_attr_include(class_name, attr, value) @driver.find_elements :xpath, string_attr_include(class_name, attr, value) end
Get tags by attribute that include value. Note: Uses XPath @param class_name [String] the tag name to match @param attr [String] the attribute to compare @param value [String] the value of the attribute that the element must include @return [Array<Element>] the elements of type tag who’s attribute includes value
Source
# File lib/appium_lib/ios/common/helper.rb, line 175 def find_eles_by_predicate(class_name: '*', value:) predicate = if class_name == '*' %(name == "#{value}" || label == "#{value}" || value == "#{value}") else %(type == "#{class_name}" && ) + %((name == "#{value}" || label == "#{value}" || value == "#{value}")) end @driver.find_elements :predicate, predicate end
Find all elements exactly matching attribute case insensitive value. Note: Uses Predicate @param value [String] the value of the attribute that the element must have @param class_name [String] the tag name to match @return [Array<Element>]
Source
# File lib/appium_lib/ios/common/helper.rb, line 221 def find_eles_by_predicate_include(class_name: '*', value:) predicate = if class_name == '*' %(name contains[c] "#{value}" || label contains[c] "#{value}" || value contains[c] "#{value}") else %(type == "#{class_name}" && ) + %((name contains[c] "#{value}" || label contains[c] "#{value}" || value contains[c] "#{value}")) end @driver.find_elements :predicate, predicate end
Get elements that include case insensitive value. Note: Uses Predicate @param value [String] the value of the attribute that the element must include @param class_name [String] the tag name to match @return [Array<Element>] the elements of type tag who’s attribute includes value
Source
# File lib/appium_lib/ios/element/generic.rb, line 34 def find_exact(value) ele_by_json_visible_exact '*', value end
Find the first element exactly matching value @param value [String] the value to search for @return [Element]
Source
# File lib/appium_lib/ios/element/generic.rb, line 27 def finds(value) eles_by_json_visible_contains '*', value end
Find all elements containing value @param value [String] the value to search for @return [Array<Element>]
Source
# File lib/appium_lib/ios/element/generic.rb, line 41 def finds_exact(value) eles_by_json_visible_exact '*', value end
Find all elements exactly matching value @param value [String] the value to search for @return [Array<Element>]
Source
# File lib/appium_lib/ios/common/helper.rb, line 234 def first_ele(class_name) ele_index class_name, 1 end
Get the first tag that matches class_name @param class_name [String] the tag to match @return [Element]
Source
# File lib/appium_lib/ios/element/text.rb, line 51 def first_text first_ele static_text_class end
Find the first UIAStaticText|XCUIElementTypeStaticText. @return [UIA_STATIC_TEXT|XCUIELEMENT_TYPE_STATIC_TEXT]
Source
# File lib/appium_lib/ios/element/textfield.rb, line 69 def first_textfield ele_by_json _textfield_visible end
Find the first TextField. @return [TextField]
Source
# File lib/appium_lib/ios/common/helper.rb, line 101 def id(id) find_element(:id, id) end
Find by id @param id [String] the id to search for @return [Element]
Source
# File lib/appium_lib/ios/common/helper.rb, line 58 def ios_password(length = 1) 8226.chr('UTF-8') * length end
iOS only. On Android
uiautomator always returns an empty string for EditText password.
Password character returned from value of UIASecureTextField
@param length [Integer] the length of the password to generate @return [String] the returned string is of size length
Source
# File lib/appium_lib/ios/common/helper.rb, line 241 def last_ele(class_name) ele_index class_name, 'last()' end
Get the last tag that matches class_name @param class_name [String] the tag to match @return [Element]
Source
# File lib/appium_lib/ios/element/text.rb, line 57 def last_text last_ele static_text_class end
Find the last UIAStaticText|XCUIElementTypeStaticText. @return [UIA_STATIC_TEXT|XCUIELEMENT_TYPE_STATIC_TEXT]
Source
# File lib/appium_lib/ios/element/textfield.rb, line 75 def last_textfield result = eles_by_json(_textfield_visible).last raise _no_such_element if result.nil? result end
Find the last TextField. @return [TextField]
Source
# File lib/appium_lib/ios/common/helper.rb, line 74 def page(opts = {}) class_name = opts.is_a?(Hash) ? opts.fetch(:class, nil) : opts source = get_source # current_context may be nil which breaks start_with if current_context&.start_with?('WEBVIEW') parser = @android_html_parser ||= Nokogiri::HTML::SAX::Parser.new(Appium::Common::HTMLElements.new) parser.document.reset parser.document.filter = class_name parser.parse source result = parser.document.result puts result result else parser = Nokogiri::XML::SAX::Parser.new(UITestElementsPrinter.new) if class_name parser.document.filter = class_name.is_a?(Symbol) ? class_name.to_s : class_name end parser.parse source nil end end
Prints a string of interesting elements to the console.
@example
```ruby page class: :UIAButton # filter on buttons page class: :UIAButton, window: 1 ```
@option visible [Symbol] visible value to filter on @option class [Symbol] class name to filter on
@return [void]
Source
# File lib/appium_lib/ios/element/textfield.rb, line 33 def secure_text_field_class UIA_SECURE_TEXT_FIELD end
@return [String] Class name for secure text field
Source
# File lib/appium_lib/ios/element/text.rb, line 25 def static_text_class UIA_STATIC_TEXT end
@return [String] Class name for text
Source
# File lib/appium_lib/ios/common/helper.rb, line 128 def string_attr_exact(class_name, attr, value) %(//#{class_name}[@visible="true" and @#{attr}="#{value}"]) end
@private
Source
# File lib/appium_lib/ios/common/helper.rb, line 155 def string_attr_include(class_name, attr, value) %(//#{class_name}[@visible="true" and contains(translate(@#{attr},"#{value.upcase}", "#{value}"), "#{value}")]) end
@private
Source
# File lib/appium_lib/ios/common/helper.rb, line 299 def string_visible_contains(element, value) contains = { target: value, substring: true, insensitive: true } { typeArray: [element], onlyVisible: true, name: contains, label: contains, value: contains } end
@private Returns an object that matches the first element that contains value
example: ele_by_json_visible_contains
‘UIATextField’, ‘sign in’
@param element [String] the class name for the element @param value [String] the value to search for @return [String]
Source
# File lib/appium_lib/ios/common/helper.rb, line 338 def string_visible_exact(element, value) exact = { target: value, substring: false, insensitive: false } { typeArray: [element], onlyVisible: true, name: exact, label: exact, value: exact } end
@private Create an object to exactly match the first element with target value @param element [String] the class name for the element @param value [String] the value to search for @return [String]
Source
# File lib/appium_lib/ios/common/helper.rb, line 249 def tag(class_name) ele_by_json(typeArray: [class_name], onlyVisible: true) end
Returns the first visible element matching class_name
@param class_name [String] the class_name to search for @return [Element]
Source
# File lib/appium_lib/ios/element/text.rb, line 33 def text(value) return ele_index static_text_class, value if value.is_a? Numeric ele_by_json_visible_contains static_text_class, value end
Find the first UIAStaticText|XCUIElementTypeStaticText that contains value or by index. @param value [String, Integer] the value to find. If int then the UIAStaticText|XCUIElementTypeStaticText at that index is returned. @return [UIA_STATIC_TEXT|XCUIELEMENT_TYPE_STATIC_TEXT]
Source
# File lib/appium_lib/ios/element/text.rb, line 64 def text_exact(value) ele_by_json_visible_exact static_text_class, value end
Find the first UIAStaticText|XCUIElementTypeStaticText that exactly matches value. @param value [String] the value to match exactly @return [UIA_STATIC_TEXT|XCUIElementTypeStaticText]
Source
# File lib/appium_lib/ios/element/textfield.rb, line 28 def text_field_class UIA_TEXT_FIELD end
@return [String] Class name for text field
Source
# File lib/appium_lib/ios/element/textfield.rb, line 42 def textfield(value) if value.is_a? Numeric index = value raise ArgumentError, "#{index} is not a valid index. Must be >= 1" if index <= 0 index -= 1 # eles_by_json and _textfields_with_predicate is 0 indexed. result = eles_by_json(_textfield_visible)[index] raise _no_such_element if result.nil? return result end ele_by_json _textfield_contains_string value end
Find the first TextField that contains value or by index. Note: Uses XPath @param value [String, Integer] the text to match exactly. If int then the TextField at that index is returned. @return [TextField]
Source
# File lib/appium_lib/ios/element/textfield.rb, line 85 def textfield_exact(value) ele_by_json _textfield_exact_string value end
Find the first TextField that exactly matches value. @param value [String] the value to match exactly @return [TextField]
Source
# File lib/appium_lib/ios/element/textfield.rb, line 61 def textfields(value = false) return eles_by_json _textfield_visible unless value eles_by_json _textfield_contains_string value end
Find all TextFields containing value. If value is omitted, all TextFields are returned. @param value [String] the value to search for @return [Array<TextField>]
Source
# File lib/appium_lib/ios/element/textfield.rb, line 92 def textfields_exact(value) eles_by_json _textfield_exact_string value end
Find all TextFields that exactly match value. @param value [String] the value to match exactly @return [Array<TextField>]
Source
# File lib/appium_lib/ios/element/text.rb, line 43 def texts(value = false) return tags static_text_class unless value eles_by_json_visible_contains static_text_class, value end
Find all UIAStaticTexts|XCUIElementTypeStaticTexts containing value. If value is omitted, all UIAStaticTexts|XCUIElementTypeStaticTexts are returned @param value [String] the value to search for @return [Array<UIA_STATIC_TEXT|XCUIELEMENT_TYPE_STATIC_TEXT>]
Source
# File lib/appium_lib/ios/element/text.rb, line 71 def texts_exact(value) eles_by_json_visible_exact static_text_class, value end
Find all UIAStaticTexts|XCUIElementTypeStaticTexts that exactly match value. @param value [String] the value to match exactly @return [Array<UIA_STATIC_TEXT|XCUIElementTypeStaticText>]
Private Instance Methods
Source
# File lib/appium_lib/ios/element/textfield.rb, line 111 def _textfield_contains_string(value) contains = { target: value, substring: true, insensitive: true } contains_obj = { name: contains, label: contains, value: contains } _textfield_visible.merge(contains_obj) end
Source
# File lib/appium_lib/ios/element/textfield.rb, line 104 def _textfield_exact_string(value) exact = { target: value, substring: false, insensitive: false } exact_obj = { name: exact, label: exact, value: exact } _textfield_visible.merge(exact_obj) end
Source
# File lib/appium_lib/ios/element/textfield.rb, line 99 def _textfield_visible { typeArray: [UIA_TEXT_FIELD, UIA_SECURE_TEXT_FIELD], onlyVisible: true } end
Source
# File lib/appium_lib/ios/element/generic.rb, line 47 def raise_error_if_no_element(element) error_message = 'An element could not be located on the page using the given search parameters.' raise(::Selenium::WebDriver::Error::NoSuchElementError, error_message) if element.nil? element end
Source
# File lib/appium_lib/ios/element/generic.rb, line 55 def select_visible_elements(elements) elements.select(&:displayed?) end
Return visible elements.