Class: MonkeyMaster::ADB

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_master/adb.rb

Overview

Provide helpers to work with Android ADB

Class Method Summary collapse

Class Method Details

.adb?Boolean

Check if adb is accessible as an executable

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/monkey_master/adb.rb', line 76

def self.adb?
  adb = find_executable 'adb'
  adb.nil? ? false : true
end

.detect_devicesObject

Use ADB to detect connected Android devices.



25
26
27
28
# File 'lib/monkey_master/adb.rb', line 25

def self.detect_devices
  device_list = `adb devices | grep -v "List" | grep "device" | awk '{print $1}'`
  device_list.split("\n")
end

.end_logging(devices) ⇒ Object

End logging on multiple devices.

device

Devices for which logging should be stopped



66
67
68
69
70
71
72
73
# File 'lib/monkey_master/adb.rb', line 66

def self.end_logging(devices)
  devices.each do |device|
    puts "[ADB/LOGS] KILLING the logcat process on device #{device}."
    `adb -s #{device} shell ps | grep -m1 logcat | awk '{print $2}' | xargs adb -s #{device} shell kill`
    puts "[ADB/LOGS] KILLING the logcat process for the device #{device} on the machine."
    `ps ax | grep -m1 "adb -s #{device} logcat" | awk '{print $1}' | xargs kill`
  end
end

.kill_monkeys(devices) ⇒ Object

Kill ADB monkeys.

device

Devices for which the adb monkey should be killed



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/monkey_master/adb.rb', line 33

def self.kill_monkeys(devices)
  unless devices
    puts '[ADB] No devices specified yet.'
    return
  end

  devices.each do |device|
    puts "[ADB] KILLING the monkey on device #{device}."
    `adb -s #{device} shell ps | awk '/com\.android\.commands\.monkey/ { system("adb -s #{device} shell kill " $2) }'`
  end
end

.monkey_run(app_id, device, args) ⇒ Object

Run the adb monkey.

app_id

ID of the android app for which the monkey should be run

device

Device on which the adb monkey should be run

args

Arguments passed to the adb monkey



12
13
14
15
# File 'lib/monkey_master/adb.rb', line 12

def self.monkey_run(app_id, device, args)
  `adb -s #{device} shell monkey -p #{app_id} #{args}`
  $?.exitstatus
end

.monkey_stop(app_id, device) ⇒ Object

Force stop a monkey for an app.

app_id

ID of the android app for which the monkey should be stopped



20
21
22
# File 'lib/monkey_master/adb.rb', line 20

def self.monkey_stop(app_id, device)
  `adb -s #{device} shell am force-stop #{app_id}`
end

.start_logging(app_id, device, log) ⇒ Object

Start logging on a certain device with logcat

app_id

App for which logs should be retrieved

device

Device for which logging should be started

log

File that should be used for logging



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/monkey_master/adb.rb', line 50

def self.start_logging(app_id, device, log)
  begin
    timeout(5) do
      puts "[ADB/LOGS] Logging device #{device} to #{log}."
      `adb -s #{device} logcat -c #{log} &`
      `adb -s #{device} logcat #{app_id}:W > #{log} &`
    end
  rescue Timeout::Error
    end_logging
    raise ArgumentError, 'It doesn’t seem like there are ready, connected devices.'
  end
end