class Appium::Android::AndroidElements

@private nokogiri.org/Nokogiri/XML/SAX.html

Attributes

filter[R]
keys[R]
result[R]

Public Class Methods

new() click to toggle source
# File lib/appium_lib/android/common/helper.rb, line 30
def initialize # rubocop:disable Lint/MissingSuper
  reset
  @filter   = false
end

Public Instance Methods

filter=(value) click to toggle source

convert to string to support symbols

# File lib/appium_lib/android/common/helper.rb, line 23
def filter=(value)
  # nil and false disable the filter
  return @filter = false unless value # rubocop:disable Lint/ReturnInVoidContext

  @filter = value.to_s.downcase
end
reset() click to toggle source
# File lib/appium_lib/android/common/helper.rb, line 35
def reset
  @result   = ''
  @keys     = %w(text resource-id content-desc)
end
start_element(name, attrs = [], driver = $driver) click to toggle source

nokogiri.org/Nokogiri/XML/SAX/Document.html

# File lib/appium_lib/android/common/helper.rb, line 41
def start_element(name, attrs = [], driver = $driver)
  return if filter && !name.downcase.include?(filter)

  attributes = {}

  attrs.each do |key, value|
    attributes[key] = value if keys.include?(key) && !value.empty?
  end

  # scoped to: text resource-id content-desc
  attributes_values = attributes.values
  strings           = driver.lazy_load_strings
  id_matches = strings.empty? ? [] : strings.select { |_key, value| attributes_values.include? value }

  string_ids = nil

  if id_matches && !id_matches.empty?
    space_suffix = ' ' * 15 # 15 is '  strings.xml: '.length
    string_ids   = ''

    # add first
    string_ids += "#{id_matches.shift[0]}\n"

    # use padding for remaining values
    # [0] = key, [1] = value
    id_matches.each do |match|
      string_ids += "#{space_suffix}#{match[0]}\n"
    end
  end

  string = ''
  text   = attributes['text']
  desc   = attributes['content-desc']
  id     = attributes['resource-id']

  if !text.nil? && text == desc
    string += "  text, desc: #{text}\n"
  else
    string += "  text: #{text}\n" unless text.nil?
    string += "  desc: #{desc}\n" unless desc.nil?
  end
  string += "  id: #{id}\n" unless id.nil?
  string += "  strings.xml: #{string_ids}" unless string_ids.nil?

  @result += "\n#{name}\n#{string}" unless attributes.empty?
end