module Appium::Android::Espresso::Element

Public Instance Methods

button(value) click to toggle source

Find the first button that contains value or by index. @param value [String, Integer] the value to exactly match. If int then the button at that index is returned. @return [Button]

# File lib/appium_lib/android/espresso/element/button.rb, line 23
def button(value)
  # Don't use ele_index because that only works on one element type.
  # Android needs to combine button and image button to match iOS.
  if value.is_a? Numeric
    index = value
    raise "#{index} is not a valid index. Must be >= 1" if index <= 0

    # zero index
    _button_visible_selectors_xpath(index: index - 1)
  end

  i = find_elements :xpath, _button_contains_string_xpath(BUTTON, value)
  e = find_elements :xpath, _button_contains_string_xpath(IMAGE_BUTTON, value)

  raise_no_such_element_if_empty(i + e)

  (i + e)[0]
end
button_exact(value) click to toggle source

Find the first button that exactly matches value. @param value [String] the value to match exactly @return [Button]

# File lib/appium_lib/android/espresso/element/button.rb, line 77
def button_exact(value)
  i = find_elements :xpath, _button_exact_string_xpath(BUTTON, value)
  e = find_elements :xpath, _button_exact_string_xpath(IMAGE_BUTTON, value)

  raise_no_such_element_if_empty(i + e)

  (i + e)[0]
end
buttons(value = false) click to toggle source

Find all buttons containing value. If value is omitted, all buttons are returned. @param value [String] the value to search for @return [Array<Button>]

# File lib/appium_lib/android/espresso/element/button.rb, line 46
def buttons(value = false)
  return _button_visible_selectors_xpath unless value

  i = find_elements :xpath, _button_contains_string_xpath(BUTTON, value)
  e = find_elements :xpath, _button_contains_string_xpath(IMAGE_BUTTON, value)
  i + e
end
buttons_exact(value) click to toggle source

Find all buttons that exactly match value. @param value [String] the value to match exactly @return [Array<Button>]

# File lib/appium_lib/android/espresso/element/button.rb, line 89
def buttons_exact(value)
  i = find_elements :xpath, _button_exact_string_xpath(BUTTON, value)
  e = find_elements :xpath, _button_exact_string_xpath(IMAGE_BUTTON, value)
  i + e
end
first_button() click to toggle source

Find the first button. @return [Button]

# File lib/appium_lib/android/espresso/element/button.rb, line 56
def first_button
  _button_visible_selectors_xpath(button_index: 0, image_button_index: 0)
end
last_button() click to toggle source

Find the last button. @return [Button]

# File lib/appium_lib/android/espresso/element/button.rb, line 62
def last_button
  # uiautomator index doesn't support last
  # and it's 0 indexed
  button_index = tags(::Appium::Android::BUTTON).length
  button_index -= 1 if button_index.positive?
  image_button_index = tags(::Appium::Android::IMAGE_BUTTON).length
  image_button_index -= 1 if image_button_index.positive?

  _button_visible_selectors_xpath(button_index: button_index,
                                  image_button_index: image_button_index)
end
scroll_to(text) click to toggle source

Scroll to the first element containing target text or description. Scroll happens upto 30 times in centre of device width. @param text [String] the text or resourceId to search for in the text value and content description @return [Element] the element scrolled to

# File lib/appium_lib/android/espresso/element/generic.rb, line 23
def scroll_to(text)
  err = nil
  w_s = window_rect

  (1..30).each do |_count|
    action
      .move_to_location(w_s.width / 2, (w_s.height * 2) / 5) # pointer based magic number
      .pointer_down(:left)
      .move_to_location(0, w_s.height / 5)
      .release
      .perform
    sleep 1 # we must wait finish scrolling

    return text(text)
  rescue StandardError => e
    err = e
  end

  raise err
end
scroll_to_exact(text) click to toggle source

Scroll to the first element with the exact target text or description. Scroll happens upto 30 times in centre of device width. @param text [String] the text or resourceId to search for in the text value and content description @return [Element] the element scrolled to

# File lib/appium_lib/android/espresso/element/generic.rb, line 48
def scroll_to_exact(text)
  err = nil
  w_s = window_rect

  (1..30).each do |_count|
    action
      .move_to_location(w_s.width / 2, (w_s.height * 2) / 5) # pointer based magic number
      .pointer_down(:left)
      .move_to_location(0, w_s.height / 5)
      .release
      .perform
    sleep 1 # we must wait finish scrolling

    return text_exact(text)
  rescue StandardError => e
    err = e
  end

  raise err
end

Private Instance Methods

_button_contains_string_xpath(class_name, value) click to toggle source
# File lib/appium_lib/android/espresso/element/button.rb, line 130
def _button_contains_string_xpath(class_name, value)
  r_id = resource_id(value, " or @resource-id='#{value}'")
  "//#{class_name}[contains(translate(@text,'#{value.upcase}', '#{value}'), '#{value}') " \
    "or contains(translate(@content-desc,'#{value.upcase}', '#{value}'), '#{value}')#{r_id}]"
end
_button_exact_string_xpath(class_name, value) click to toggle source
# File lib/appium_lib/android/espresso/element/button.rb, line 125
def _button_exact_string_xpath(class_name, value)
  r_id = resource_id(value, " or @resource-id='#{value}'")
  "//#{class_name}[@text='#{value}' or @content-desc='#{value}'#{r_id}]"
end
_button_visible_selectors_xpath(opts = {}) click to toggle source
# File lib/appium_lib/android/espresso/element/button.rb, line 104
def _button_visible_selectors_xpath(opts = {})
  button_index       = opts.fetch :button_index, false
  image_button_index = opts.fetch :image_button_index, false

  index = opts.fetch :index, false

  b = find_elements :xpath, "//#{BUTTON}"
  i = find_elements :xpath, "//#{IMAGE_BUTTON}"

  if index
    raise_no_such_element_if_empty(b + i)
    (b + i)[index]
  elsif button_index && image_button_index
    raise_no_such_element_if_empty(b + i)
    b_index = button_index + image_button_index
    (b + i)[b_index]
  else
    b + i
  end
end
raise_no_such_element_if_empty(elements) click to toggle source

@private

# File lib/appium_lib/android/espresso/element/button.rb, line 98
def raise_no_such_element_if_empty(elements)
  raise _no_such_element if elements.empty?

  elements.first
end