lib/appium_lib/android/helper.rb in appium_lib-1.0.0 vs lib/appium_lib/android/helper.rb in appium_lib-2.0.0

- old
+ new

@@ -37,10 +37,32 @@ if keys.include?(key) && !value.empty? attributes[key] = value end end + # scoped to: text resource-id content-desc + attributes_values = attributes.values + id_matches = $driver.lazy_load_strings.select do |key, value| + attributes_values.include? value + end + + string_ids = nil + + if id_matches && id_matches.length > 0 + space_suffix = ' ' * ' 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'] @@ -49,10 +71,11 @@ 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 end # class AndroidElements @@ -72,14 +95,15 @@ parser.document.result end # Intended for use with console. # Inspects and prints the current page. - # @param class_name [String] the class name to filter on. - # if false (default) then all classes will be inspected + # @option class [Symbol] the class name to filter on. case insensitive include match. + # if nil (default) then all classes will be inspected # @return [void] - def page class_name=false + def page opts={} + class_name = opts.is_a?(Hash) ? opts.fetch(:class, nil) : opts puts get_android_inspect class_name nil end # Lists package, activity, and adb shell am start -n value for current app. @@ -94,20 +118,32 @@ package: pkg, activity: act, am_start: pkg + '/' + act) end - # Find by id - # @param id [String] the id to search for - # @return [Element] - def id id - value = resolve_id id + # @private + def string_id_xpath id + value = resolve_id id # If the id doesn't resolve in strings.xml then use it as is # It's probably a resource id which won't be in strings.xml - value = id unless value + value = id unless value exact = string_visible_exact '*', value contains = string_visible_contains '*', value - xpath "#{exact} | #{contains}" + "#{exact} | #{contains}" + end + + # Find the first matching element by id + # @param id [String] the id to search for + # @return [Element] + def id id + xpath string_id_xpath id + end + + # Find all matching elements by id + # @param id [String] the id to search for + # @return [Element] + def ids id + xpaths string_id_xpath id end # Find the element of type class_name at matching index. # @param class_name [String] the class name to find # @param index [Integer] the index \ No newline at end of file