module Appium::Android::Uiautomator2::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/uiautomator2/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 ArgumentError, "#{index} is not a valid index. Must be >= 1" if index <= 0

    result = find_elements :uiautomator, _button_visible_selectors(index: index)
    raise _no_such_element if result.empty?

    return result[value - 1]
  end

  elements = find_elements :uiautomator, _button_contains_string(value)
  raise_no_such_element_if_empty(elements)
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/uiautomator2/element/button.rb, line 76
def button_exact(value)
  elements = find_elements :uiautomator, _button_exact_string(value)
  raise_no_such_element_if_empty(elements)
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/uiautomator2/element/button.rb, line 44
def buttons(value = false)
  return find_elements :uiautomator, _button_visible_selectors unless value

  find_elements :uiautomator, _button_contains_string(value)
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/uiautomator2/element/button.rb, line 84
def buttons_exact(value)
  find_elements :uiautomator, _button_exact_string(value)
end
first_button() click to toggle source

Find the first button. @return [Button]

# File lib/appium_lib/android/uiautomator2/element/button.rb, line 52
def first_button
  elements = find_elements :uiautomator, _button_visible_selectors(button_index: 0, image_button_index: 0)
  raise_no_such_element_if_empty(elements)
end
last_button() click to toggle source

Find the last button. @return [Button]

# File lib/appium_lib/android/uiautomator2/element/button.rb, line 59
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::ImageButton).length
  image_button_index -= 1 if image_button_index.positive?

  elements = find_elements :uiautomator,
                           _button_visible_selectors(button_index: button_index,
                                                     image_button_index: image_button_index)
  raise_no_such_element_if_empty(elements)
end

Private Instance Methods

_button_contains_string(value) click to toggle source

@private

# File lib/appium_lib/android/uiautomator2/element/button.rb, line 119
def _button_contains_string(value)
  button       = string_visible_contains ::Appium::Android::Button, value
  image_button = string_visible_contains ::Appium::Android::ImageButton, value
  button + image_button
end
_button_exact_string(value) click to toggle source

@private

# File lib/appium_lib/android/uiautomator2/element/button.rb, line 112
def _button_exact_string(value)
  button       = string_visible_exact ::Appium::Android::Button, value
  image_button = string_visible_exact ::Appium::Android::ImageButton, value
  button + image_button
end
_button_visible_selectors(opts = {}) click to toggle source

@private

# File lib/appium_lib/android/uiautomator2/element/button.rb, line 98
def _button_visible_selectors(opts = {})
  button_index       = opts.fetch :button_index, false
  image_button_index = opts.fetch :image_button_index, false

  if button_index && image_button_index
    "new UiSelector().className(#{::Appium::Android::Button}).instance(#{button_index});" \
      "new UiSelector().className(#{::Appium::Android::ImageButton}).instance(#{image_button_index});"
  else
    "new UiSelector().className(#{::Appium::Android::Button});" \
      "new UiSelector().className(#{::Appium::Android::ImageButton});"
  end
end
raise_no_such_element_if_empty(elements) click to toggle source

@private

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

  elements.first
end