Created
April 28, 2016 18:43
-
-
Save CSRafaelAlves/3c06d8f193cdca6ff2816301ef102127 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SystemHelper | |
require 'Open3' | |
require 'Nokogiri' | |
def uiautomator_dump | |
puts "Chamando uiautomator_dump" | |
stdout, stderr, status = exec_adb('shell uiautomator dump') | |
unless /dumped to: (?<file>\S*)/ =~ stdout | |
fail "uiautomator dump failed? Returned #{stdout} :: #{stderr}" | |
end | |
stdout, stderr, status = exec_adb("shell cat #{file}") | |
[stdout, stderr, status] | |
end | |
def exec_adb(cmd) | |
puts "Chamando exec_adb" | |
adb_cmd = "#{default_device.adb_command} #{cmd}" | |
stdout, stderr, status = Open3.capture3(adb_cmd) | |
unless status.success? | |
fail "Adb failed: #{adb_cmd} Returned #{stdout} :: #{stderr}" | |
end | |
[stdout, stderr, status] | |
end | |
def keyboard_enter_keyevent(keyevent) | |
puts "Chamando keyboard_enter_keyevent" | |
exec_adb("shell input keyevent #{keyevent}") | |
end | |
def xpath_for_full_path_texts(params) | |
puts "Chamando xpath_for_full_path_texts" | |
texts = params.keys.grep(/^notification.full./) | |
clauses = texts.collect { |k| "./node[@text='#{params[k]}']" } | |
"//node[#{clauses.join('][')}]" | |
end | |
def extract_integer_bounds(set) | |
puts "Chamando extract_integer_bounds" | |
return nil if set.empty? | |
match = (set.attr('bounds').to_s.match(/\[(\d+),(\d+)\]\[(\d+),(\d+)\]/)) | |
match.captures.collect(&:to_i) | |
end | |
def bounds_from_xpath(xpath) | |
puts "Chamando bounds_from_xpath" | |
stdout, _stderr, _status = uiautomator_dump | |
set = Nokogiri::XML(stdout).xpath(xpath) | |
if (bounds = extract_integer_bounds(set)) | |
return yield bounds | |
else | |
return nil | |
end | |
end | |
### INTERACTING WITH THE PHONE ### | |
def tap_notification(xpath) | |
puts "Chamando tap_notification" | |
found_bounds = bounds_from_xpath(xpath) do |x1, y1, x2, y2| | |
ym = (y1 + y2) >> 1 | |
exec_adb("shell input tap #{(x1 + x2) >> 1} #{ym}") | |
end | |
dismissed = !found_bounds.nil? | |
keyboard_enter_keyevent('KEYCODE_BACK') unless dismissed | |
return dismissed | |
end | |
### IMPLEMENTING THE STEP ### | |
def handle_notification(params) | |
puts "Chamando handle_notification" | |
xpath = xpath_for_full_path_texts(params) | |
timeout = params['timeout'].to_i | |
start = Time.new | |
while start + timeout / 1000 > Time.new | |
if params['action.click'] | |
break if tap_notification(xpath) | |
else | |
break if dismiss_notification(xpath) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment