Class: MonkeyMaster::MonkeyCommander

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

Overview

A class for conveniently employing Android adb monkeys.

Author

Lukas Nagl ([email protected])

Copyright

Copyright © 2013 Innovaptor OG

License

MIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id) ⇒ MonkeyCommander

Initialize the monkey master.

app_id

The id of the app that should be tested by the monkeys, e.g. com.innovaptor.MonkeyTestApp



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/monkey_master/monkey_commander.rb', line 30

def initialize(app_id)
  @app_id = app_id
  @iterations = 1 # Default to a single iteration
  @base_dir = Dir.pwd
  time = Time.new
  @log_dir = 'monkey_logs' + time.strftime('%Y%m%d_%H%M%S')
  @logger = Logger.new(STDOUT)
  @logger.formatter = proc do |severity, datetime, _progname, msg|
    "#{severity}|#{datetime}: #{msg}\n"
  end
end

Instance Attribute Details

#app_id=(value) ⇒ Object (writeonly)

The id of the app that should be tested by monkeys. E.g.: com.innovaptor.MonkeyTestApp



19
20
21
# File 'lib/monkey_master/monkey_commander.rb', line 19

def app_id=(value)
  @app_id = value
end

#device_list=(value) ⇒ Object (writeonly)

List of devices that should be used by the MonkeyCommander.



23
24
25
# File 'lib/monkey_master/monkey_commander.rb', line 23

def device_list=(value)
  @device_list = value
end

#iterations=(value) ⇒ Object (writeonly)

The number of monkey iterations that should be run on each device.



21
22
23
# File 'lib/monkey_master/monkey_commander.rb', line 21

def iterations=(value)
  @iterations = value
end

#log_dirObject (readonly)

Directory of the monkey logs.



15
16
17
# File 'lib/monkey_master/monkey_commander.rb', line 15

def log_dir
  @log_dir
end

#loggerObject (readonly)

Logger used for the monkey output.



17
18
19
# File 'lib/monkey_master/monkey_commander.rb', line 17

def logger
  @logger
end

Instance Method Details

#command_monkeys(adb_args = '-v 80000 --throttle 200 --ignore-timeouts --pct-majornav 20 --pct-appswitch 0 --kill-process-after-error') ⇒ Object

Start running monkeys on all specified devices.

adb_args

Arguments passed to the adb monkeys



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/monkey_master/monkey_commander.rb', line 58

def command_monkeys(adb_args='-v 80000 --throttle 200 --ignore-timeouts --pct-majornav 20 --pct-appswitch 0 --kill-process-after-error')
  @logger.info("[SETUP] Will run adb monkeys with the following arguments: #{adb_args}")
  if !@device_list || @device_list.empty?
    fail(ArgumentError, 'No devices found or specified. Check if development mode is on.')
  end

  fail(ArgumentError, 'No app id specified.') unless @app_id

  prepare

  masters = []
  begin
    @device_list.each do |device|
      master = Thread.new do
        # Monkey around in parallel

        device_log = log_for_device(@app_id, device)

        @logger.info("[MASTER #{device}] Starting to command monkeys.")
        @iterations.to_i.times do |i|
          @logger.info("\t[MASTER #{device}] Monkey #{i} is doing its thing…")

          # Start the monkey
          if ADB.monkey_run(@app_id, device, adb_args) != 0
            @logger.info("\t\t[MASTER #{device}] Monkey encountered an error!")
          end

          # Archive and clean the log
          archive_and_clean_log(device_log, "monkeylog_#{device}_#{i}.txt")

          @logger.info("\t\t[MASTER #{device}] Monkey #{i} is killing the app now in preparation for the next monkey.")
          ADB.monkey_stop(@app_id, device)
        end
        @logger.info("[MASTER #{device}] All monkeys are done.")
      end
      masters.push(master)
    end

    masters.each(&:join) # wait for all masters to finish
  rescue SystemExit, Interrupt
    # Clean and graceful shutdown, if possible
    @logger.info('[MASTER] Received interrupt. Stopping all masters.')
    masters.each(&:terminate)
  end

  ADB.kill_monkeys(@device_list)
  ADB.end_logging(@device_list)
end

#detect_devices(devices) ⇒ Object

Either create a list of devices from the parameter, or detect connected devices using adb.

devices

nil, for automatic device detection; or a list of device IDs separated by ‘,’



46
47
48
# File 'lib/monkey_master/monkey_commander.rb', line 46

def detect_devices(devices)
  @device_list = devices ? devices.split(',') : ADB.detect_devices
end

#kill_monkeysObject

Kill the monkey on all detected devices.



51
52
53
# File 'lib/monkey_master/monkey_commander.rb', line 51

def kill_monkeys
  ADB.kill_monkeys(@device_list)
end