Class: Appium::Android::AndroidElements

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/appium_lib/android/common/helper.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAndroidElements

Returns a new instance of AndroidElements.



16
17
18
19
# File 'lib/appium_lib/android/common/helper.rb', line 16

def initialize
  reset
  @filter   = false
end

Instance Attribute Details

#filterObject

Returns the value of attribute filter.



6
7
8
# File 'lib/appium_lib/android/common/helper.rb', line 6

def filter
  @filter
end

#keysObject (readonly)

Returns the value of attribute keys.



6
7
8
# File 'lib/appium_lib/android/common/helper.rb', line 6

def keys
  @keys
end

#resultObject (readonly)

Returns the value of attribute result.



6
7
8
# File 'lib/appium_lib/android/common/helper.rb', line 6

def result
  @result
end

Instance Method Details

#resetObject



21
22
23
24
# File 'lib/appium_lib/android/common/helper.rb', line 21

def reset
  @result   = ''
  @keys     = %w(text resource-id content-desc)
end

#start_element(name, attrs = [], driver = $driver) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/appium_lib/android/common/helper.rb', line 27

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