Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 4 additions & 42 deletions lib/appium_lib/android/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Android
# @private
# http://nokogiri.org/Nokogiri/XML/SAX.html
class AndroidElements < Nokogiri::XML::SAX::Document
attr_reader :result, :keys, :instance, :filter
attr_reader :result, :keys, :filter

# convert to string to support symbols
def filter=(value)
Expand All @@ -15,22 +15,17 @@ def filter=(value)
def initialize
reset
@filter = false
@instance = Hash.new(-1)
end

def reset
@result = ''
@keys = %w(text resource-id content-desc)
@instance = Hash.new(-1)
end

# http://nokogiri.org/Nokogiri/XML/SAX/Document.html
def start_element(name, attrs = [])
return if filter && !name.downcase.include?(filter)

# instance numbers start at 0.
number = instance[name] += 1

attributes = {}

attrs.each do |key, value|
Expand Down Expand Up @@ -78,39 +73,10 @@ def start_element(name, attrs = [])
string += " id: #{id}\n" unless id.nil?
string += " strings.xml: #{string_ids}" unless string_ids.nil?

@result += "\n#{name} (#{number})\n#{string}" unless attributes.empty?
@result += "\n#{name}\n#{string}" unless attributes.empty?
end
end # class AndroidElements

# Fix uiautomator's xml dump.
# https://github.com/appium/appium/issues/2822
# https://code.google.com/p/android/issues/detail?id=74143
def _fix_android_native_source(source)
# <android.app.ActionBar$Tab
# <android.app.ActionBar $ Tab

# find each closing tag that contains a dollar sign.
source.scan(/<\/([^>]*\$[^>]*)>/).flatten.uniq.each do |problem_tag|
# "android.app.ActionBar$Tab"
before, after = problem_tag.split('$')
before.strip!
after.strip!

fixed = "#{before}.#{after}"

# now escape . in before/after because they're used in regex
before.gsub!('.', '\.')
after.gsub!('.', '\.')

# <android.app.ActionBar$Tab => <android.app.ActionBar.Tab
# </android.app.ActionBar$Tab> => </android.app.ActionBar.Tab>
source = source.gsub(/<#{before}\s*\$\s*#{after}/,
"<#{fixed}").gsub(/<\/#{before}\s*\$\s*#{after}>/, "</#{fixed}>")
end

source
end

# Prints xml of the current page
# @return [void]
def source
Expand Down Expand Up @@ -400,14 +366,10 @@ def complex_finds_exact(class_name, value)
find_elements :uiautomator, string_visible_exact(class_name, value)
end

# Returns XML string for the current page
# Fixes uiautomator's $ in node names.
# `android.app.ActionBar$Tab` becomes `android.app.ActionBar.Tab`
# Returns XML string for the current page via `page_source`
# @return [String]
def get_source
src = @driver.page_source
src = _fix_android_native_source src unless src && src.start_with?('<html>')
src
@driver.page_source
end
end # module Android
end # module Appium
3 changes: 1 addition & 2 deletions lib/appium_lib/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,10 @@ def initialize(opts = {})
# load common methods
extend Appium::Common
extend Appium::Device

if device_is_android?
# load Android specific methods
extend Appium::Android
else
# load iOS specific methods
extend Appium::Ios
extend Appium::Ios::XcuitestGesture if automation_name_is_xcuitest? # Override touch actions
end
Expand Down