Skip to content

Commit 72ff02a

Browse files
Restore strings.xml support on Android
1 parent 0dc4965 commit 72ff02a

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

lib/appium_lib/android/helper.rb

+42-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ def start_element name, attrs = []
3939
end
4040
end
4141

42+
# scoped to: text resource-id content-desc
43+
attributes_values = attributes.values
44+
id_matches = $driver.lazy_load_strings.select do |key, value|
45+
attributes_values.include? value
46+
end
47+
48+
string_ids = nil
49+
50+
if id_matches && id_matches.length > 0
51+
space_suffix = ' ' * ' strings.xml: '.length
52+
string_ids = ''
53+
54+
# add first
55+
string_ids += "#{id_matches.shift[0]}\n"
56+
57+
# use padding for remaining values
58+
# [0] = key, [1] = value
59+
id_matches.each do |match|
60+
string_ids += "#{space_suffix}#{match[0]}\n"
61+
end
62+
end
63+
4264
string = ''
4365
text = attributes['text']
4466
desc = attributes['content-desc']
@@ -51,6 +73,7 @@ def start_element name, attrs = []
5173
string += " desc: #{desc}\n" unless desc.nil?
5274
end
5375
string += " id: #{id}\n" unless id.nil?
76+
string += " strings.xml: #{string_ids}" unless string_ids.nil?
5477

5578
@result += "\n#{name}\n#{string}" unless attributes.empty?
5679
end
@@ -97,17 +120,29 @@ def current_app
97120
am_start: pkg + '/' + act)
98121
end
99122

100-
# Find by id
101-
# @param id [String] the id to search for
102-
# @return [Element]
103-
def id id
104-
value = resolve_id id
123+
# @private
124+
def string_id_xpath id
125+
value = resolve_id id
105126
# If the id doesn't resolve in strings.xml then use it as is
106127
# It's probably a resource id which won't be in strings.xml
107-
value = id unless value
128+
value = id unless value
108129
exact = string_visible_exact '*', value
109130
contains = string_visible_contains '*', value
110-
xpath "#{exact} | #{contains}"
131+
"#{exact} | #{contains}"
132+
end
133+
134+
# Find the first matching element by id
135+
# @param id [String] the id to search for
136+
# @return [Element]
137+
def id id
138+
xpath string_id_xpath id
139+
end
140+
141+
# Find all matching elements by id
142+
# @param id [String] the id to search for
143+
# @return [Element]
144+
def ids id
145+
xpaths string_id_xpath id
111146
end
112147

113148
# Find the element of type class_name at matching index.

0 commit comments

Comments
 (0)