module Appium::Common

def back

Returns:
  • (void) -
def back
  @driver.navigate.back
end

def button text, number=0

Returns:
  • (Button) - the button found with text and matching number

Parameters:
  • number (Integer) -- the occurrence of the button matching text. Defaults to the first button.
  • text (String, Integer) -- the text to exactly match. If int then the button at that index is returned.
def button text, number=0
  # return button at index.
  return ele_index :button, text if text.is_a? Numeric
  number >= 1 ? button_num( text, number ) :
                find_ele_by_text_include( :button, text )
end

def button_exact text

Returns:
  • (Button) -

Parameters:
  • text (String) -- the text to match exactly
def button_exact text
  find_ele_by_text :button, text
end

def button_num text, number=1

Returns:
  • (Button) - the button that matches text and number

Parameters:
  • number (Integer) -- the button occurance to return. 1 = first button
  • text (String) -- the text to match
def button_num text, number=1
  raise 'Number must be >= 1' if number <= 0
  number = number - 1 # zero indexed
  result = nil
  elements = buttons text
  elements.size > number ? result = elements[number]
                         : result = elements.first
  result
end

def buttons text=nil

Returns:
  • (Array, Array) - either an array of button texts or an array of button elements if text is provided.

Parameters:
  • text (String) -- the text to exactly match
def buttons text=nil
  text == nil ? find_eles_attr( :button, :text ) :
                find_eles_by_text_include( :button, text )
end

def buttons_exact text

Returns:
  • (Array

Parameters:
  • text (String) -- the text to match exactly
def buttons_exact text
  find_eles_by_text :button, text
end

def e_buttons

Returns:
  • (Array
def e_buttons
  find_eles :button
end

def e_s_texts

Returns:
  • (Array) -
def e_s_texts
  find_eles :text
end

def ele_index tag_name, index

Returns:
  • (Element) - the found element of type tag_name

Parameters:
  • index (Integer) -- the index
  • tag_name (String) -- the tag name to find
def ele_index tag_name, index
  # XPath index starts at 1. ruby_lib index starts at 0
  find_element :xpath, "//#{tag_name}[#{index + 1}]"
end

def find_ele_by_attr_include tag, attr, value

Returns:
  • (Element) - the element of type tag who's attribute includes value

Parameters:
  • value (String) -- the value of the attribute that the element must include
  • attr (String) -- the attribute to compare
  • tag (String) -- the tag name to match
def find_ele_by_attr_include tag, attr, value
  @driver.find_element :xpath, %Q(#{tag}[contains(@#{attr}, '#{value}')])
end

def find_ele_by_text tag, text

Returns:
  • (Element) - the element of type tag exactly matching text

Parameters:
  • text (String) -- the text to exactly match
  • tag (String) -- the tag name to match
def find_ele_by_text tag, text
  @driver.find_element :xpath, %Q(#{tag}[@text='#{text}'])
end

def find_ele_by_text_include tag, text

Returns:
  • (Element) - the element of type tag that includes text

Parameters:
  • text (String) -- the text the element must include
  • tag (String) -- the tag name to match
def find_ele_by_text_include tag, text
  find_ele_by_attr_include tag, :text, text
end

def find_eles tag_name

Returns:
  • (Array) - the found elements of type tag_name

Parameters:
  • tag_name (String) -- the tag name to find
def find_eles tag_name
  @driver.find_elements :tag_name, tag_name
end

def find_eles_by_attr_include tag, attr, value

Returns:
  • (Array) - the elements of type tag who's attribute includes value

Parameters:
  • value (String) -- the value of the attribute that the element must include
  • attr (String) -- the attribute to compare
  • tag (String) -- the tag name to match
def find_eles_by_attr_include tag, attr, value
  @driver.find_elements :xpath, %Q(#{tag}[contains(@#{attr}, '#{value}')])
end

def find_eles_by_text tag, text

Returns:
  • (Array) - the elements of type tag exactly matching text

Parameters:
  • text (String) -- the text to exactly match
  • tag (String) -- the tag name to match
def find_eles_by_text tag, text
  @driver.find_elements :xpath, %Q(#{tag}[@text='#{text}'])
end

def find_eles_by_text_include tag, text

Returns:
  • (Array) - the elements of type tag that includes text

Parameters:
  • text (String) -- the text the element must include
  • tag (String) -- the tag name to match
def find_eles_by_text_include tag, text
  find_eles_by_attr_include tag, :text, text
end

def find_name name

Returns:
  • (Element) -

Parameters:
  • name (String) -- the name to exactly match
def find_name name
  find_element :name, name
end

def find_names name

Returns:
  • (Array) -

Parameters:
  • name (String) -- the name to exactly match
def find_names name
  find_elements :name, name
end

def first_button

Returns:
  • (Button) -
def first_button
  first_ele :button
end

def first_ele tag_name

Returns:
  • (Element) -

Parameters:
  • tag_name (String) -- the tag to match
def first_ele tag_name
  # XPath index starts at 1
  find_element :xpath, "//#{tag_name}[1]"
end

def first_s_text

Returns:
  • (Text) -
def first_s_text
  first_ele :text
end

def get_source

Returns:
  • (JSON) -
def get_source
  # must set max nesting. default limit of 20 is too low for selendroid
  JSON.parse @driver.page_source, max_nesting: 9999
end

def id id

Returns:
  • (Element) -

Parameters:
  • id (String) -- the id to search for
def id id
  find_element :id, id
end

def ignore &block

Return block.call and ignore any exceptions.
def ignore &block
  begin; block.call; rescue; end
end

def last_button

Returns:
  • (Button) -
def last_button
  last_ele :button
end

def last_ele tag_name

Returns:
  • (Element) -

Parameters:
  • tag_name (String) -- the tag to match
def last_ele tag_name
  xpath "//#{tag_name}[last()]"
end

def last_s_text

Returns:
  • (Text) -
def last_s_text
  last_ele :text
end

def lazy_load_strings

def lazy_load_strings
  @strings_xml ||= mobile(:getStrings)
end

def px_to_window_rel opts={}

def px_to_window_rel opts={}
  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

def resolve_id id

Returns:
  • (String) -

Parameters:
  • id (String) -- the id to resolve
def resolve_id id
  lazy_load_strings
  @strings_xml[id]
end

def s_text text

Returns:
  • (Text) -

Parameters:
  • text (String, Integer) -- the text to find. If int then the text at that index is returned.
def s_text text
  return ele_index :text, text if text.is_a? Numeric
  find_ele_by_text_include :text, text
end

def s_text_exact text

Returns:
  • (Text) -

Parameters:
  • text (String) -- the text that the tag must match
def s_text_exact text
  find_ele_by_text :text, text
end

def s_texts

Returns:
  • (Array) -
def s_texts
  find_eles_attr :text, :text
end

def session_id

For Sauce Labs reporting. Returns the current session id.
def session_id
  @driver.session_id
end

def source

Returns:
  • (void) -
def source
  ap get_source
end

def tag tag_name

Returns:
  • (Element) -

Parameters:
  • tag_name (String) -- the tag_name to search for
def tag tag_name
  find_element :tag_name, tag_name
end

def wait max_wait=30, interval=0.5, &block

Returns:
  • (Object) - the result of block.call

Parameters:
  • block (Block) -- the block to call
  • interval (Float) -- the time in seconds to wait after calling the block
  • max_wait (Integer) -- the maximum time in seconds to wait for.
def wait max_wait=30, interval=0.5, &block
  # Rescue Timeout::Error: execution expired
  result = nil
  timeout max_wait do
    until (result = begin; block.call || true; rescue; end)
      sleep interval
    end
  end
  result
end

def wait_true max_wait=30, interval=0.5, &block

Returns:
  • (Object) - the result of block.call

Parameters:
  • block (Block) -- the block to call
  • interval (Float) -- the time in seconds to wait after calling the block
  • max_wait (Integer) -- the maximum time in seconds to wait for
def wait_true max_wait=30, interval=0.5, &block
  # Rescue Timeout::Error: execution expired
  result = nil
  timeout max_wait do
    until (result = begin; block.call; rescue; end)
      sleep interval
    end
  end
  result
end

def window_size

Get the window's size
def window_size
  return nil if @driver.nil?
  @driver.manage.window.size
end

def xml_keys target

Returns:
  • (Array) -

Parameters:
  • target (String) -- the target to search for in strings.xml values
def xml_keys target
  lazy_load_strings
  @strings_xml.select { |key, value| key.downcase.include? target.downcase }
end

def xml_values target

Returns:
  • (Array) -

Parameters:
  • target (String) -- the target to search for in strings.xml keys
def xml_values target
  lazy_load_strings
  @strings_xml.select { |key, value| value.downcase.include? target.downcase }
end

def xpath xpath_str

Returns:
  • (Element) -

Parameters:
  • xpath_str (String) -- the XPath string
def xpath xpath_str
  find_element :xpath, xpath_str
end

def xpaths xpath_str

Returns:
  • (Array) -

Parameters:
  • xpath_str (String) -- the XPath string
def xpaths xpath_str
  find_elements :xpath, xpath_str
end