Module: Honeydew::DeviceCommands

Included in:
DeviceActions
Defined in:
lib/honeydew/device_commands.rb

Instance Method Summary collapse

Instance Method Details

#adb(command) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/honeydew/device_commands.rb', line 93

def adb(command)
  adb_command = "adb -s #{serial} #{command}"
  info "executing '#{adb_command}'"
  `#{adb_command} 2>/dev/null`.tap do
    if $?.exitstatus != 0
      message = "ADB command '#{command}' failed"
      error message
      raise message
    end
  end
end

#clear_app_data(package_name) ⇒ Object



81
82
83
# File 'lib/honeydew/device_commands.rb', line 81

def clear_app_data(package_name)
  adb "shell pm clear #{package_name}"
end

#clear_directory(directory) ⇒ Object



15
16
17
18
# File 'lib/honeydew/device_commands.rb', line 15

def clear_directory(directory)
  all_files_in_directory_path = [directory.chomp('/'), '/*'].join
  adb "shell rm -r #{all_files_in_directory_path}"
end

#device_endpoint(action) ⇒ Object



20
21
22
# File 'lib/honeydew/device_commands.rb', line 20

def device_endpoint action
  URI.join("http://localhost:#{port}", action)
end

#dump_window_hierarchy(local_path) ⇒ Object



24
25
26
27
28
# File 'lib/honeydew/device_commands.rb', line 24

def dump_window_hierarchy(local_path)
  path_in_device = perform_action('dump_window_hierarchy')
  info "dumping window hierarchy to #{local_path}..."
  adb "pull #{path_in_device} #{local_path}"
end

#forwarding_portObject



69
70
71
# File 'lib/honeydew/device_commands.rb', line 69

def forwarding_port
  adb "forward tcp:#{port} tcp:7120"
end

#honeydew_server_fileObject



54
55
56
# File 'lib/honeydew/device_commands.rb', line 54

def honeydew_server_file
  File.absolute_path(File.join(File.dirname(__FILE__), "../../server/target/#{honeydew_server_package}"))
end

#honeydew_server_packageObject



50
51
52
# File 'lib/honeydew/device_commands.rb', line 50

def honeydew_server_package
  "honeydew-server-#{Honeydew::VERSION}.jar"
end

#install_app(apk_location, opts = []) ⇒ Object



77
78
79
# File 'lib/honeydew/device_commands.rb', line 77

def install_app(apk_location, opts=[])
  adb "install #{opts.join(" ")} #{apk_location}"
end

#is_app_installed?(package_name) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/honeydew/device_commands.rb', line 7

def is_app_installed?(package_name)
  has_app_installed?(package_name)
end

#is_app_installed_in_data?(package_name) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/honeydew/device_commands.rb', line 11

def is_app_installed_in_data?(package_name)
  has_app_installed_in_data?(package_name)
end

#launch_settingsObject



89
90
91
# File 'lib/honeydew/device_commands.rb', line 89

def launch_settings
  adb 'shell am start -n com.android.settings/com.android.settings.Settings'
end

#rebootObject



85
86
87
# File 'lib/honeydew/device_commands.rb', line 85

def reboot
  adb 'reboot'
end

#start_automation_serverObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/honeydew/device_commands.rb', line 58

def start_automation_server
  info "starting honeydew-server on the device"
  adb "push #{honeydew_server_file} /data/local/tmp"
  Thread.new do
    adb "shell uiautomator runtest #{honeydew_server_package} -c com.amplify.honeydew_server.TestRunner"
  end
  at_exit do
    terminate_honeydew_server
  end
end

#start_honeydew_serverObject



37
38
39
40
41
# File 'lib/honeydew/device_commands.rb', line 37

def start_honeydew_server
  forwarding_port
  terminate_honeydew_server
  start_automation_server
end

#take_screenshot(local_path) ⇒ Object



30
31
32
33
34
35
# File 'lib/honeydew/device_commands.rb', line 30

def take_screenshot(local_path)
  path_in_device = '/data/local/tmp/honeydew.png'
  info "saving screenshot to #{local_path}..."
  adb "shell /system/bin/screencap -p #{path_in_device}"
  adb "pull #{path_in_device} #{local_path}"
end

#terminate_honeydew_serverObject



43
44
45
46
47
48
# File 'lib/honeydew/device_commands.rb', line 43

def terminate_honeydew_server
  info "terminating honeydew-server"
  Net::HTTP.post_form device_endpoint('/terminate'), {}
rescue Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError
  # Swallow
end

#uninstall_app(package_name) ⇒ Object



73
74
75
# File 'lib/honeydew/device_commands.rb', line 73

def uninstall_app(package_name)
  adb "uninstall #{package_name}"
end