lib/appium_lib/android/helper.rb in appium_lib-9.4.0 vs lib/appium_lib/android/helper.rb in appium_lib-9.4.1
- old
+ new
@@ -277,17 +277,17 @@
resource_id = /^[a-zA-Z_][a-zA-Z0-9\._]*:[^\/]+\/[\S]+$/
string.match(resource_id) ? on_match : ''
end
# Returns a string that matches the first element that contains value
+ # For automationName is uiautomator2
+ # example: string_visible_contains_xpath 'UIATextField', 'sign in'
#
- # example: complex_find_contains 'UIATextField', 'sign in'
- #
# @param class_name [String] the class name for the element
# @param value [String] the value to search for
# @return [String]
- def string_visible_contains(class_name, value)
+ def string_visible_contains_xpath(class_name, value)
r_id = resource_id(value, " or @resource-id='#{value}'")
if class_name == '*'
return "//*[contains(translate(@text,'#{value.upcase}', '#{value}'), '#{value}')" \
" or contains(translate(@content-desc,'#{value.upcase}', '#{value}'), '#{value}')" + r_id + ']'
@@ -295,54 +295,112 @@
"//#{class_name}[contains(translate(@text,'#{value.upcase}', '#{value}'), '#{value}')" \
" or contains(translate(@content-desc,'#{value.upcase}', '#{value}'), '#{value}')" + r_id + ']'
end
+ # Returns a string that matches the first element that contains value
+ # For automationName is Appium
+ # example: string_visible_contains 'UIATextField', 'sign in'
+ #
+ # @param class_name [String] the class name for the element
+ # @param value [String] the value to search for
+ # @return [String]
+ def string_visible_contains(class_name, value)
+ value = %("#{value}")
+ if class_name == '*'
+ return (resource_id(value, "new UiSelector().resourceId(#{value});") +
+ "new UiSelector().descriptionContains(#{value});" \
+ "new UiSelector().textContains(#{value});")
+ end
+
+ class_name = %("#{class_name}")
+ resource_id(value, "new UiSelector().className(#{class_name}).resourceId(#{value});") +
+ "new UiSelector().className(#{class_name}).descriptionContains(#{value});" \
+ "new UiSelector().className(#{class_name}).textContains(#{value});"
+ end
+
# Find the first element that contains value
# @param element [String] the class name for the element
# @param value [String] the value to search for
# @return [Element]
def complex_find_contains(element, value)
- find_element :xpath, string_visible_contains(element, value)
+ if automation_name_is_uiautomator2?
+ find_element :xpath, string_visible_contains_xpath(element, value)
+ else
+ find_element :uiautomator, string_visible_contains(element, value)
+ end
end
# Find all elements containing value
# @param element [String] the class name for the element
# @param value [String] the value to search for
# @return [Array<Element>]
def complex_finds_contains(element, value)
- find_elements :xpath, string_visible_contains(element, value)
+ if automation_name_is_uiautomator2?
+ find_elements :xpath, string_visible_contains_xpath(element, value)
+ else
+ find_elements :uiautomator, string_visible_contains(element, value)
+ end
end
# @private
# Create an string to exactly match the first element with target value
+ # For automationName is uiautomator2
# @param class_name [String] the class name for the element
# @param value [String] the value to search for
# @return [String]
- def string_visible_exact(class_name, value)
+ def string_visible_exact_xpath(class_name, value)
r_id = resource_id(value, " or @resource-id='#{value}'")
if class_name == '*'
return "//*[@text='#{value}' or @content-desc='#{value}'" + r_id + ']'
end
"//#{class_name}[@text='#{value}' or @content-desc='#{value}'" + r_id + ']'
end
+ # @private
+ # Create an string to exactly match the first element with target value
+ # @param class_name [String] the class name for the element
+ # @param value [String] the value to search for
+ # @return [String]
+ def string_visible_exact(class_name, value)
+ value = %("#{value}")
+
+ if class_name == '*'
+ return (resource_id(value, "new UiSelector().resourceId(#{value});") +
+ "new UiSelector().description(#{value});" \
+ "new UiSelector().text(#{value});")
+ end
+
+ class_name = %("#{class_name}")
+ resource_id(value, "new UiSelector().className(#{class_name}).resourceId(#{value});") +
+ "new UiSelector().className(#{class_name}).description(#{value});" \
+ "new UiSelector().className(#{class_name}).text(#{value});"
+ end
+
# Find the first element exactly matching value
# @param class_name [String] the class name for the element
# @param value [String] the value to search for
# @return [Element]
def complex_find_exact(class_name, value)
- find_element :xpath, string_visible_exact(class_name, value)
+ if automation_name_is_uiautomator2?
+ find_element :xpath, string_visible_exact_xpath(class_name, value)
+ else
+ find_element :uiautomator, string_visible_exact(class_name, value)
+ end
end
# Find all elements exactly matching value
# @param class_name [String] the class name for the element
# @param value [String] the value to search for
# @return [Element]
def complex_finds_exact(class_name, value)
- find_elements :xpath, string_visible_exact(class_name, value)
+ if automation_name_is_uiautomator2?
+ find_elements :xpath, string_visible_exact_xpath(class_name, value)
+ else
+ find_elements :uiautomator, string_visible_exact(class_name, value)
+ end
end
# Returns XML string for the current page
# Fixes uiautomator's $ in node names.
# `android.app.ActionBar$Tab` becomes `android.app.ActionBar.Tab`