11class Driver
2- attr_accessor :udid , :bundle_id , :enable_simulator_keyboard , :session_duration , :session_path , :session_actions
2+ attr_accessor :udid , :bundle_id , :disable_simulator_keyboard , :event_count , :session_path , :session_actions , :ignore_crashes , :throttle
33
44 def initialize ( params )
55 self . udid = params [ :udid ]
6+ self . throttle = params [ :throttle ]
67 self . bundle_id = params [ :bundle_id ]
7- self . session_duration = params [ :duration ]
8+ self . event_count = params [ :event_count ]
89 self . session_path = params [ :session_path ]
9- self . enable_simulator_keyboard = params [ :enable_simulator_keyboard ]
10+ self . ignore_crashes = params [ :ignore_crashes ]
11+ self . disable_simulator_keyboard = params [ :disable_simulator_keyboard ]
1012 self . session_actions = params [ :session_actions ]
1113 @session = { params : params , actions : [ ] }
1214 ensure_driver_installed
@@ -23,10 +25,8 @@ def monkey_test_precondition
2325
2426 def monkey_test ( gestures )
2527 monkey_test_precondition
26- app_elements = describe_ui . shuffle
27- current_time = Time . now
28- counter = 0
29- while Time . now < current_time + session_duration
28+ event_count . times do |counter |
29+ app_elements = describe_ui . shuffle
3030 el1_coordinates = central_coordinates ( app_elements . first )
3131 el2_coordinates = central_coordinates ( app_elements . last )
3232 case gestures . sample
@@ -53,18 +53,14 @@ def monkey_test(gestures)
5353 else
5454 next
5555 end
56- detect_app_state_change
57- track_running_apps if counter % 5 == 0 # Track running apps after every 5th action to speed up the test
58- counter += 1
59- app_elements = describe_ui . shuffle
56+ checkup ( counter )
6057 end
6158 save_session
6259 end
6360
6461 def repeat_monkey_test
6562 monkey_test_precondition
66- counter = 0
67- session_actions . each do |action |
63+ session_actions . each_with_index do |action , counter |
6864 case action [ 'type' ]
6965 when 'tap'
7066 tap ( coordinates : { x : action [ 'x' ] , y : action [ 'y' ] } )
@@ -79,10 +75,16 @@ def repeat_monkey_test
7975 else
8076 next
8177 end
82- detect_app_state_change
83- track_running_apps if counter % 5 == 0
84- counter += 1
78+ checkup ( counter )
79+ end
80+ end
81+
82+ def checkup ( counter )
83+ detect_app_state_change
84+ if counter % 5 == 0 || throttle . to_i > 0 # Track running apps after every 5th action
85+ track_running_apps # (unless `throttle` was provided) to speed up the test
8586 end
87+ check_speed_limit
8688 end
8789
8890 def describe_ui
@@ -115,7 +117,7 @@ def shutdown_simulator
115117
116118 def configure_simulator_keyboard
117119 shutdown_simulator
118- keyboard_status = enable_simulator_keyboard ? 0 : 1
120+ keyboard_status = disable_simulator_keyboard ? 1 : 0
119121 `defaults write com.apple.iphonesimulator ConnectHardwareKeyboard #{ keyboard_status } `
120122 end
121123
@@ -165,10 +167,8 @@ def press(coordinates:, duration:)
165167 end
166168
167169 def swipe ( start_coordinates :, end_coordinates :, duration :)
168- Logger . info (
169- "Swipe (#{ duration } s):" ,
170- payload : "#{ JSON . pretty_generate ( start_coordinates ) } => #{ JSON . pretty_generate ( end_coordinates ) } "
171- )
170+ payload = "#{ JSON . pretty_generate ( start_coordinates ) } => #{ JSON . pretty_generate ( end_coordinates ) } "
171+ Logger . info ( "Swipe (#{ duration } s):" , payload : payload )
172172 unless session_actions
173173 @session [ :actions ] << {
174174 type : :swipe ,
@@ -188,8 +188,8 @@ def central_coordinates(element)
188188 x = ( frame [ 'x' ] + ( frame [ 'width' ] / 2 ) ) . abs . to_i
189189 y = ( frame [ 'y' ] + ( frame [ 'height' ] / 2 ) ) . abs . to_i
190190 {
191- x : x > screen_size [ :width ] . to_i ? rand ( 0 .. screen_size [ :width ] . to_i ) : x ,
192- y : y > screen_size [ :height ] . to_i ? rand ( 0 .. screen_size [ :height ] . to_i ) : y
191+ x : x > screen_size [ :width ] . to_i ? random_coordinates [ :x ] : x ,
192+ y : y > screen_size [ :height ] . to_i ? random_coordinates [ :y ] : y
193193 }
194194 end
195195
@@ -222,6 +222,8 @@ def press_duration
222222 end
223223
224224 def save_session
225+ return if session_path . nil?
226+
225227 File . write ( "#{ session_path } /xcmonkey-session.json" , JSON . pretty_generate ( @session ) )
226228 end
227229
@@ -236,6 +238,7 @@ def track_running_apps
236238 return if new_apps . empty?
237239
238240 launch_app ( target_bundle_id : bundle_id )
241+
239242 new_apps . each do |id |
240243 Logger . warn ( "Shutting down: #{ id } " )
241244 terminate_app ( id )
@@ -249,7 +252,7 @@ def detect_app_state_change
249252
250253 target_app_is_running = list_running_apps . any? { |app | app [ 'bundle_id' ] == bundle_id }
251254
252- if target_app_is_running
255+ if target_app_is_running || ignore_crashes
253256 launch_app ( target_bundle_id : bundle_id )
254257 else
255258 save_session
@@ -262,6 +265,10 @@ def detect_app_in_background
262265 current_app_label . nil? || current_app_label . strip . empty?
263266 end
264267
268+ def check_speed_limit
269+ sleep ( throttle / 1000.0 ) if throttle . to_i > 0
270+ end
271+
265272 private
266273
267274 def ensure_driver_installed
0 commit comments