Skip to content

Commit b5bb400

Browse files
Validate resourceId before searching
1 parent c41ee41 commit b5bb400

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

lib/appium_lib/android/helper.rb

+34-4
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,36 @@ def tags class_name
248248
end
249249

250250
# @private
251+
# Detects if the string represents a resourceId
252+
# resourceId is only supported on API >= 18 devices
253+
#
254+
# @param string [String] the string check for a resourceId
255+
# value will be auto unquoted
256+
# @param on_match [String] the string to return on resourceId match
257+
#
258+
# @return [String] empty string on failure, on_match on successful match
259+
def _resourceId string, on_match
260+
return '' unless string
261+
262+
# unquote the string
263+
# "com.example.Test:id/enter" -> com.example.Test:id/enter
264+
unquote = string.match(/"(.+)"/)
265+
string = unquote[1] if unquote
266+
267+
# java_package : type / name
268+
#
269+
# com.example.Test:id/enter
270+
#
271+
# ^[a-zA-Z_] - Java package must start with letter or underscore
272+
# [a-zA-Z0-9\._]* - Java package may contain letters, numbers, periods and underscores
273+
# : - : ends the package and starts the type
274+
# [^\/]+ - type is made up of at least one non-/ characters
275+
# \\/ - / ends the type and starts the name
276+
# [\S]+$ - the name contains at least one non-space character and then the line is ended
277+
resource_id = /^[a-zA-Z_][a-zA-Z0-9\._]*:[^\/]+\/[\S]+$/
278+
string.match(resource_id) ? on_match : ''
279+
end
280+
251281
# Returns a string that matches the first element that contains value
252282
#
253283
# example: complex_find_contains 'UIATextField', 'sign in'
@@ -259,14 +289,14 @@ def string_visible_contains class_name, value
259289
value = %Q("#{value}")
260290

261291
if class_name == '*'
262-
return "new UiSelector().resourceId(#{value});" +
292+
return _resourceId(value, "new UiSelector().resourceId(#{value});") +
263293
"new UiSelector().descriptionContains(#{value});" +
264294
"new UiSelector().textContains(#{value});"
265295
end
266296

267297
class_name = %Q("#{class_name}")
268298

269-
"new UiSelector().className(#{class_name}).resourceId(#{value});" +
299+
_resourceId(value, "new UiSelector().className(#{class_name}).resourceId(#{value});") +
270300
"new UiSelector().className(#{class_name}).descriptionContains(#{value});" +
271301
"new UiSelector().className(#{class_name}).textContains(#{value});"
272302
end
@@ -296,14 +326,14 @@ def string_visible_exact class_name, value
296326
value = %Q("#{value}")
297327

298328
if class_name == '*'
299-
return "new UiSelector().resourceId(#{value});" +
329+
return _resourceId(value, "new UiSelector().resourceId(#{value});") +
300330
"new UiSelector().description(#{value});" +
301331
"new UiSelector().text(#{value});"
302332
end
303333

304334
class_name = %Q("#{class_name}")
305335

306-
"new UiSelector().className(#{class_name}).resourceId(#{value});" +
336+
_resourceId(value, "new UiSelector().className(#{class_name}).resourceId(#{value});") +
307337
"new UiSelector().className(#{class_name}).description(#{value});" +
308338
"new UiSelector().className(#{class_name}).text(#{value});"
309339
end

0 commit comments

Comments
 (0)