Class: Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/xcmonkey/driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Driver

Returns a new instance of Driver.



4
5
6
7
8
9
10
11
12
13
# File 'lib/xcmonkey/driver.rb', line 4

def initialize(params)
  self.udid = params[:udid]
  self.bundle_id = params[:bundle_id]
  self.session_duration = params[:duration]
  self.session_path = params[:session_path]
  self.enable_simulator_keyboard = params[:enable_simulator_keyboard]
  self.session_actions = params[:session_actions]
  @session = { params: params, actions: [] }
  ensure_driver_installed
end

Instance Attribute Details

#bundle_idObject

Returns the value of attribute bundle_id.



2
3
4
# File 'lib/xcmonkey/driver.rb', line 2

def bundle_id
  @bundle_id
end

#enable_simulator_keyboardObject

Returns the value of attribute enable_simulator_keyboard.



2
3
4
# File 'lib/xcmonkey/driver.rb', line 2

def enable_simulator_keyboard
  @enable_simulator_keyboard
end

#session_actionsObject

Returns the value of attribute session_actions.



2
3
4
# File 'lib/xcmonkey/driver.rb', line 2

def session_actions
  @session_actions
end

#session_durationObject

Returns the value of attribute session_duration.



2
3
4
# File 'lib/xcmonkey/driver.rb', line 2

def session_duration
  @session_duration
end

#session_pathObject

Returns the value of attribute session_path.



2
3
4
# File 'lib/xcmonkey/driver.rb', line 2

def session_path
  @session_path
end

#udidObject

Returns the value of attribute udid.



2
3
4
# File 'lib/xcmonkey/driver.rb', line 2

def udid
  @udid
end

Instance Method Details

#boot_simulatorObject



106
107
108
109
# File 'lib/xcmonkey/driver.rb', line 106

def boot_simulator
  `idb boot #{udid}`
  Logger.error("Failed to boot #{udid}") if device_info['state'] != 'Booted'
end

#central_coordinates(element) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/xcmonkey/driver.rb', line 180

def central_coordinates(element)
  frame = element['frame']
  x = (frame['x'] + (frame['width'] / 2)).abs.to_i
  y = (frame['y'] + (frame['height'] / 2)).abs.to_i
  {
    x: x > screen_size[:width].to_i ? rand(0..screen_size[:width].to_i) : x,
    y: y > screen_size[:height].to_i ? rand(0..screen_size[:height].to_i) : y
  }
end

#configure_simulator_keyboardObject



115
116
117
118
119
# File 'lib/xcmonkey/driver.rb', line 115

def configure_simulator_keyboard
  shutdown_simulator
  keyboard_status = enable_simulator_keyboard ? 0 : 1
  `defaults write com.apple.iphonesimulator ConnectHardwareKeyboard #{keyboard_status}`
end

#describe_point(x, y) ⇒ Object



91
92
93
94
95
# File 'lib/xcmonkey/driver.rb', line 91

def describe_point(x, y)
  point_info = JSON.parse(`idb ui describe-point --udid #{udid} #{x} #{y}`)
  Logger.info("x:#{x} y:#{y} point info:", payload: JSON.pretty_generate(point_info))
  point_info
end

#describe_uiObject



87
88
89
# File 'lib/xcmonkey/driver.rb', line 87

def describe_ui
  JSON.parse(`idb ui describe-all --udid #{udid}`)
end

#device_infoObject



197
198
199
200
# File 'lib/xcmonkey/driver.rb', line 197

def device_info
  @device_info ||= JSON.parse(`idb describe --udid #{udid} --json`)
  @device_info
end

#ensure_app_installedObject



130
131
132
# File 'lib/xcmonkey/driver.rb', line 130

def ensure_app_installed
  Logger.error("App #{bundle_id} is not installed on device #{udid}") unless list_apps.include?(bundle_id)
end

#ensure_device_existsObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/xcmonkey/driver.rb', line 134

def ensure_device_exists
  device = list_targets.detect { |target| target.include?(udid) }
  Logger.error("Can't find device #{udid}") if device.nil?

  Logger.info('Device info:', payload: device)
  if device.include?('simulator')
    configure_simulator_keyboard
    boot_simulator
  end
end

#launch_appObject



97
98
99
100
# File 'lib/xcmonkey/driver.rb', line 97

def launch_app
  `idb launch --udid #{udid} #{bundle_id}`
  wait_until_app_launched
end

#list_appsObject



145
146
147
# File 'lib/xcmonkey/driver.rb', line 145

def list_apps
  `idb list-apps --udid #{udid}`
end

#list_booted_simulatorsObject



126
127
128
# File 'lib/xcmonkey/driver.rb', line 126

def list_booted_simulators
  `idb list-targets`.split("\n").grep(/Booted/)
end

#list_targetsObject



121
122
123
124
# File 'lib/xcmonkey/driver.rb', line 121

def list_targets
  @list_targets ||= `idb list-targets`.split("\n")
  @list_targets
end

#monkey_test(gestures) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/xcmonkey/driver.rb', line 23

def monkey_test(gestures)
  monkey_test_precondition
  app_elements = describe_ui.shuffle
  current_time = Time.now
  while Time.now < current_time + session_duration
    el1_coordinates = central_coordinates(app_elements.first)
    el2_coordinates = central_coordinates(app_elements.last)
    case gestures.sample
    when :precise_tap
      tap(coordinates: el1_coordinates)
    when :blind_tap
      tap(coordinates: random_coordinates)
    when :precise_press
      press(coordinates: el1_coordinates, duration: press_duration)
    when :blind_press
      press(coordinates: random_coordinates, duration: press_duration)
    when :precise_swipe
      swipe(
        start_coordinates: el1_coordinates,
        end_coordinates: el2_coordinates,
        duration: swipe_duration
      )
    when :blind_swipe
      swipe(
        start_coordinates: random_coordinates,
        end_coordinates: random_coordinates,
        duration: swipe_duration
      )
    else
      next
    end
    app_elements = describe_ui.shuffle
    next unless app_elements.include?(@home_tracker)

    save_session
    Logger.error('App lost')
  end
  save_session
end

#monkey_test_preconditionObject



15
16
17
18
19
20
21
# File 'lib/xcmonkey/driver.rb', line 15

def monkey_test_precondition
  ensure_device_exists
  ensure_app_installed
  terminate_app
  open_home_screen(with_tracker: true)
  launch_app
end

#open_home_screen(with_tracker: false) ⇒ Object



82
83
84
85
# File 'lib/xcmonkey/driver.rb', line 82

def open_home_screen(with_tracker: false)
  `idb ui button --udid #{udid} HOME`
  detect_home_unique_element if with_tracker
end

#press(coordinates:, duration:) ⇒ Object



155
156
157
158
159
# File 'lib/xcmonkey/driver.rb', line 155

def press(coordinates:, duration:)
  Logger.info("Press (#{duration}s):", payload: JSON.pretty_generate(coordinates))
  @session[:actions] << { type: :press, x: coordinates[:x], y: coordinates[:y], duration: duration } unless session_actions
  `idb ui tap --udid #{udid} --duration #{duration} #{coordinates[:x]} #{coordinates[:y]}`
end

#press_durationObject



214
215
216
# File 'lib/xcmonkey/driver.rb', line 214

def press_duration
  rand(0.5..1.5).ceil(1)
end

#random_coordinatesObject



190
191
192
193
194
195
# File 'lib/xcmonkey/driver.rb', line 190

def random_coordinates
  {
    x: rand(0..screen_size[:width].to_i),
    y: rand(0..screen_size[:height].to_i)
  }
end

#repeat_monkey_testObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xcmonkey/driver.rb', line 63

def repeat_monkey_test
  monkey_test_precondition
  session_actions.each do |action|
    case action['type']
    when 'tap'
      tap(coordinates: { x: action['x'], y: action['y'] })
    when 'press'
      press(coordinates: { x: action['x'], y: action['y'] }, duration: action['duration'])
    when 'swipe'
      swipe(
        start_coordinates: { x: action['x'], y: action['y'] },
        end_coordinates: { x: action['endX'], y: action['endY'] },
        duration: action['duration']
      )
    end
    Logger.error('App lost') if describe_ui.shuffle.include?(@home_tracker)
  end
end

#save_sessionObject



218
219
220
# File 'lib/xcmonkey/driver.rb', line 218

def save_session
  File.write("#{session_path}/xcmonkey-session.json", JSON.pretty_generate(@session))
end

#screen_sizeObject



202
203
204
205
206
207
208
# File 'lib/xcmonkey/driver.rb', line 202

def screen_size
  screen_dimensions = device_info['screen_dimensions']
  {
    width: screen_dimensions['width_points'],
    height: screen_dimensions['height_points']
  }
end

#shutdown_simulatorObject



111
112
113
# File 'lib/xcmonkey/driver.rb', line 111

def shutdown_simulator
  `idb shutdown #{udid}`
end

#swipe(start_coordinates:, end_coordinates:, duration:) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/xcmonkey/driver.rb', line 161

def swipe(start_coordinates:, end_coordinates:, duration:)
  Logger.info(
    "Swipe (#{duration}s):",
    payload: "#{JSON.pretty_generate(start_coordinates)} => #{JSON.pretty_generate(end_coordinates)}"
  )
  unless session_actions
    @session[:actions] << {
      type: :swipe,
      x: start_coordinates[:x],
      y: start_coordinates[:y],
      endX: end_coordinates[:x],
      endY: end_coordinates[:y],
      duration: duration
    }
  end
  coordinates = "#{start_coordinates[:x]} #{start_coordinates[:y]} #{end_coordinates[:x]} #{end_coordinates[:y]}"
  `idb ui swipe --udid #{udid} --duration #{duration} #{coordinates}`
end

#swipe_durationObject



210
211
212
# File 'lib/xcmonkey/driver.rb', line 210

def swipe_duration
  rand(0.1..0.7).ceil(1)
end

#tap(coordinates:) ⇒ Object



149
150
151
152
153
# File 'lib/xcmonkey/driver.rb', line 149

def tap(coordinates:)
  Logger.info('Tap:', payload: JSON.pretty_generate(coordinates))
  @session[:actions] << { type: :tap, x: coordinates[:x], y: coordinates[:y] } unless session_actions
  `idb ui tap --udid #{udid} #{coordinates[:x]} #{coordinates[:y]}`
end

#terminate_appObject



102
103
104
# File 'lib/xcmonkey/driver.rb', line 102

def terminate_app
  `idb terminate --udid #{udid} #{bundle_id} 2>/dev/null`
end