module Appium::Common
Public Instance Methods
@private
# File lib/appium_lib/common/helper.rb, line 268 def _no_such_element error_message = 'An element could not be located on the page using the given search parameters.' raise Selenium::WebDriver::Error::NoSuchElementError, error_message end
@private
# File lib/appium_lib/common/helper.rb, line 274 def _print_source(source) opts = Nokogiri::XML::ParseOptions::NOBLANKS | Nokogiri::XML::ParseOptions::NONET doc = if source.start_with? '<html' Nokogiri::HTML(source) { |cfg| cfg.options = opts } else Nokogiri::XML(source) { |cfg| cfg.options = opts } end puts doc.to_xml indent: 2 end
Navigate back. @return [void]
# File lib/appium_lib/common/helper.rb, line 38 def back @driver.navigate.back end
Get a list of available log types
@return [[String]] A list of available log types.
@example
@driver.get_available_log_types #=> [:syslog, :crashlog, :performance]
# File lib/appium_lib/common/log.rb, line 37 def get_available_log_types @driver.logs.available_types end
@param [String|Hash] type You can get particular type’s logs. @return [[Selenium::WebDriver::LogEntry]] A list of logs data.
@example
@driver.get_log("syslog") #=> [[Selenium::WebDriver::LogEntry]] @driver.get_log(:syslog) #=> [[Selenium::WebDriver::LogEntry]]
# File lib/appium_lib/common/log.rb, line 25 def get_log(type) @driver.logs.get type end
Returns a string of class counts of visible elements. @return [String]
@example
get_page_class #=> "24x XCUIElementTypeStaticText\n12x XCUIElementTypeCell\n8x XCUIElementTypeOther\n # 2x XCUIElementTypeWindow\n1x XCUIElementTypeStatusBar\n1x XCUIElementTypeTable\n1 # x XCUIElementTypeNavigationBar\n1x XCUIElementTypeApplication"
# File lib/appium_lib/common/helper.rb, line 116 def get_page_class parser = @count_elements_parser ||= Nokogiri::XML::SAX::Parser.new(CountElements.new(@core.device)) parser.document.reset parser.parse get_source parser.document.formatted_result end
Returns XML string for the current page Same as driver.page_source @return [String]
# File lib/appium_lib/common/helper.rb, line 155 def get_source @driver.page_source end
Return yield and ignore any exceptions.
# File lib/appium_lib/common/helper.rb, line 30 def ignore yield rescue Exception # rubocop:disable Lint/RescueException # Ignored end
@private
# File lib/appium_lib/common/helper.rb, line 175 def lazy_load_strings # app strings only works on local apps. # on disk apps (ex: com.android.settings) will error @lazy_load_strings ||= ignore { app_strings } || {} end
Count all classes on screen and print to stdout. Useful for appium_console. @return [nil]
@example
page_class # 24x XCUIElementTypeStaticText # 12x XCUIElementTypeCell # 8x XCUIElementTypeOther # 2x XCUIElementTypeWindow # 1x XCUIElementTypeStatusBar # 1x XCUIElementTypeTable # 1x XCUIElementTypeNavigationBar # 1x XCUIElementTypeApplication
# File lib/appium_lib/common/helper.rb, line 141 def page_class puts get_page_class nil end
Converts pixel values to window relative values
@example
px_to_window_rel x: 50, y: 150 #=> #<OpenStruct x="50.0 / 375.0", y="150.0 / 667.0">
# File lib/appium_lib/common/helper.rb, line 165 def px_to_window_rel(opts = {}, driver = $driver) w = driver.window_size x = opts.fetch :x, 0 y = opts.fetch :y, 0 OpenStruct.new(x: "#{x.to_f} / #{w.width.to_f}", y: "#{y.to_f} / #{w.height.to_f}") end
Resolve id in strings.xml and return the value. @param id [String] the id to resolve @return [String]
# File lib/appium_lib/common/helper.rb, line 200 def resolve_id(id) lazy_load_strings @lazy_load_strings[id] end
For Sauce Labs reporting. Returns the current session id. @return [String]
@example
@driver.session_id #=> "some-session-ids"
# File lib/appium_lib/common/helper.rb, line 49 def session_id @driver.session_id end
Prints xml of the current page @return [void]
# File lib/appium_lib/common/helper.rb, line 148 def source _print_source get_source end
Check every interval seconds to see if yield doesn’t raise an exception. Give up after timeout seconds.
Wait
code from the selenium Ruby gem github.com/SeleniumHQ/selenium/blob/cf501dda3f0ed12233de51ce8170c0e8090f0c20/rb/lib/selenium/webdriver/common/wait.rb
If only a number is provided then it’s treated as the timeout value.
@param [Hash|Numeric] opts Options. If the value is Numeric, the value is set as ‘{ timeout: value }` @option opts [Numeric] :timeout Seconds to wait before timing out. Set default by `appium_wait_timeout` (30). @option opts [Numeric] :interval Seconds to sleep between polls. Set default by `appium_wait_interval` (0.5). @option opts [String] :message Exception message if timed out. @option opts [Array, Exception] :ignore Exceptions to ignore while polling (default: Exception)
@example
wait(timeout: 20, interval: 0.2, message: 'custom message') { button_exact('Back') }.click wait(20) { button_exact('Back') }.click
# File lib/appium_lib/common/wait.rb, line 73 def wait(opts = {}) opts = { timeout: opts } if opts.is_a? Numeric if opts.is_a? Hash opts.empty? ? @core.wait { yield } : @core.wait(**opts) { yield } else ::Appium::Logger.warn('Arguments should be Hash like {timeout: 100}') end end
Check every interval seconds to see if yield returns a truthy value. Note this isn’t a strict boolean true, any truthy value is accepted. false and nil are considered failures. Give up after timeout seconds.
Wait
code from the selenium Ruby gem github.com/SeleniumHQ/selenium/blob/cf501dda3f0ed12233de51ce8170c0e8090f0c20/rb/lib/selenium/webdriver/common/wait.rb
If only a number is provided then it’s treated as the timeout value.
@param [Hash|Numeric] opts Options. If the value is Numeric, the value is set as ‘{ timeout: value }` @option opts [Numeric] :timeout Seconds to wait before timing out. Set default by `appium_wait_timeout` (30). @option opts [Numeric] :interval Seconds to sleep between polls. Set default by `appium_wait_interval` (0.5). @option opts [String] :message Exception message if timed out. @option opts [Array, Exception] :ignore Exceptions to ignore while polling (default: Exception)
@example
wait_true(timeout: 20, interval: 0.2, message: 'custom message') { button_exact('Back') }.click wait_true(20) { button_exact('Back') }.click
# File lib/appium_lib/common/wait.rb, line 44 def wait_true(opts = {}) opts = { timeout: opts } if opts.is_a? Numeric if opts.is_a? Hash opts.empty? ? @core.wait_true { yield } : @core.wait_true(**opts) { yield } else ::Appium::Logger.warn('Arguments should be Hash like {timeout: 100}') end end
Search strings.xml’s values for target. @param target [String] the target to search for in strings.xml values @return [Array]
# File lib/appium_lib/common/helper.rb, line 184 def xml_keys(target) lazy_load_strings @lazy_load_strings.select { |key, _value| key.downcase.include? target.downcase } end
Search strings.xml’s keys for target. @param target [String] the target to search for in strings.xml keys @return [Array]
# File lib/appium_lib/common/helper.rb, line 192 def xml_values(target) lazy_load_strings @lazy_load_strings.select { |_key, value| value.downcase.include? target.downcase } end
Returns the first element that matches the provided xpath.
@param xpath_str [String] the XPath string @return [Element]
# File lib/appium_lib/common/helper.rb, line 57 def xpath(xpath_str) @driver.find_element :xpath, xpath_str end
Returns all elements that match the provided xpath.
@param xpath_str [String] the XPath string @return [Array<Element>]
# File lib/appium_lib/common/helper.rb, line 65 def xpaths(xpath_str) @driver.find_elements :xpath, xpath_str end