class Unobtainium::Drivers::Appium

Driver implementation wrapping the appium_lib gem.

Constants

BROWSER_MATCHES

Browser matches for some platforms TODO: add many more matches

LABELS

Recognized labels for matching the driver

Public Class Methods

create(_, options) click to toggle source

Create and return a driver instance

# File lib/unobtainium/drivers/appium.rb, line 155
def create(_, options)
  # :nocov:

  # Determine compatibility option
  compat = options.fetch(:webdriver_compatibility, true)
  options.delete(:webdriver_compatibility)

  # Create & return proxy
  driver = ::Appium::Driver.new(options)
  result = DriverProxy.new(driver, compat)
  return result
  # :nocov:
end
ensure_preconditions(_, _) click to toggle source

Ensure that the driver's preconditions are fulfilled.

# File lib/unobtainium/drivers/appium.rb, line 107
def ensure_preconditions(_, _)
  require 'appium_lib'
rescue LoadError => err
  raise LoadError, "#{err.message}: you need to add "\
        "'appium_lib' to your Gemfile to use this driver!",
        err.backtrace
end
matches?(label) click to toggle source

Return true if the given label matches this driver implementation, false otherwise.

# File lib/unobtainium/drivers/appium.rb, line 101
def matches?(label)
  return !normalize_label(label).nil?
end
resolve_options(label, options) click to toggle source

Sanitize options, and expand the :browser key, if present.

# File lib/unobtainium/drivers/appium.rb, line 117
def resolve_options(label, options)
  # Normalize label and options
  normalized = normalize_label(label)
  options = ::Collapsium::UberHash.new(options || {})

  # Merge 'caps' and 'desired_capabilities', letting the former win
  options[:caps] =
    ::Collapsium::UberHash.new(options['desired_capabilities'])
                          .recursive_merge(options[:desired_capabilities])
                          .recursive_merge(options[:caps])
  options.delete(:desired_capabilities)
  options.delete('desired_capabilities')

  # The label specifies the platform, if no other platform is given.
  if not options['caps.platformName']
    options['caps.platformName'] = normalized.to_s
  end

  # Make the appium driver behave a little more like Selenium by using
  # the :url key if the normalized label is remote, and setting
  # appropriate options.
  set_url = options['appium_lib.server_url']
  if set_url and options['url'] and set_url != options['url']
    warn "You have the remote URL '#{set_url}' set in your options, "\
      "so we're not replacing it with '#{options['url']}'!"
  elsif not set_url
    options['appium_lib.server_url'] = options['url']
  end

  # If no app is given, but a browser is requested, we can supplement
  # some information
  options = supplement_browser(options)

  return normalized, options
end

Private Class Methods

supplement_browser(options) click to toggle source

If the driver options include a request for a browser, we can supplement some missing specs in the options.

# File lib/unobtainium/drivers/appium.rb, line 174
def supplement_browser(options)
  # Can't do anything without a browser request.
  if options['browser'].nil?
    return options
  end
  browser = options['browser'].downcase.to_sym

  # Platform
  platform = options['caps.platformName'].to_s.downcase.to_sym

  # If we have supplement data matching the platform and browser, great!
  data = (BROWSER_MATCHES[platform] || {})[browser]
  if data.nil?
    return options
  end

  # We do have to check that we're not overwriting any of the keys.
  data.each do |key, value|
    option_value = nil
    if options['caps'].key?(key)
      option_value = options['caps'][key]
    end

    if option_value.nil? or option_value == value
      next
    end
    raise ArgumentError, "You specified the browser option as, "\
      "'#{options['browser']}', but you also have the key "\
      "'#{key}' set in your requested capabilities. Use one or the "\
      "other."
  end

  # Merge, but also stringify symbol keys
  data.each do |key, value|
    options['caps'][key.to_s] = value
  end

  options
end