Class: Driver
- Inherits:
-
Object
- Object
- Driver
- Defined in:
- lib/xcmonkey/driver.rb
Instance Attribute Summary collapse
-
#bundle_id ⇒ Object
Returns the value of attribute bundle_id.
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#enable_simulator_keyboard ⇒ Object
Returns the value of attribute enable_simulator_keyboard.
-
#udid ⇒ Object
Returns the value of attribute udid.
Instance Method Summary collapse
- #boot_simulator ⇒ Object
- #central_coordinates(element) ⇒ Object
- #configure_simulator_keyboard ⇒ Object
- #describe_point(x, y) ⇒ Object
- #describe_ui ⇒ Object
- #device_info ⇒ Object
- #ensure_app_installed ⇒ Object
- #ensure_device_exists ⇒ Object
-
#initialize(params) ⇒ Driver
constructor
A new instance of Driver.
- #launch_app ⇒ Object
- #list_apps ⇒ Object
- #list_booted_simulators ⇒ Object
- #list_targets ⇒ Object
- #monkey_test(gestures) ⇒ Object
- #open_home_screen(with_tracker: false) ⇒ Object
- #press(coordinates:, duration:) ⇒ Object
- #press_duration ⇒ Object
- #random_coordinates ⇒ Object
- #screen_size ⇒ Object
- #shutdown_simulator ⇒ Object
- #swipe(start_coordinates:, end_coordinates:, duration:) ⇒ Object
- #swipe_duration ⇒ Object
- #tap(coordinates:) ⇒ Object
- #terminate_app ⇒ Object
Constructor Details
#initialize(params) ⇒ Driver
Returns a new instance of Driver.
4 5 6 7 8 9 10 |
# File 'lib/xcmonkey/driver.rb', line 4 def initialize(params) self.udid = params[:udid] self.bundle_id = params[:bundle_id] self.duration = params[:duration] self.enable_simulator_keyboard = params[:enable_simulator_keyboard] ensure_driver_installed end |
Instance Attribute Details
#bundle_id ⇒ Object
Returns the value of attribute bundle_id.
2 3 4 |
# File 'lib/xcmonkey/driver.rb', line 2 def bundle_id @bundle_id end |
#duration ⇒ Object
Returns the value of attribute duration.
2 3 4 |
# File 'lib/xcmonkey/driver.rb', line 2 def duration @duration end |
#enable_simulator_keyboard ⇒ Object
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 |
#udid ⇒ Object
Returns the value of attribute udid.
2 3 4 |
# File 'lib/xcmonkey/driver.rb', line 2 def udid @udid end |
Instance Method Details
#boot_simulator ⇒ Object
71 72 73 74 |
# File 'lib/xcmonkey/driver.rb', line 71 def boot_simulator `idb boot #{udid}` Logger.error("Failed to boot #{udid}") if device_info['state'] != 'Booted' end |
#central_coordinates(element) ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/xcmonkey/driver.rb', line 133 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_keyboard ⇒ Object
80 81 82 83 84 |
# File 'lib/xcmonkey/driver.rb', line 80 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
56 57 58 59 60 |
# File 'lib/xcmonkey/driver.rb', line 56 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_ui ⇒ Object
52 53 54 |
# File 'lib/xcmonkey/driver.rb', line 52 def describe_ui JSON.parse(`idb ui describe-all --udid #{udid}`) end |
#device_info ⇒ Object
150 151 152 153 |
# File 'lib/xcmonkey/driver.rb', line 150 def device_info @device_info ||= JSON.parse(`idb describe --udid #{udid} --json`) @device_info end |
#ensure_app_installed ⇒ Object
95 96 97 |
# File 'lib/xcmonkey/driver.rb', line 95 def ensure_app_installed Logger.error("App #{bundle_id} is not installed on device #{udid}") unless list_apps.include?(bundle_id) end |
#ensure_device_exists ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/xcmonkey/driver.rb', line 99 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_app ⇒ Object
62 63 64 65 |
# File 'lib/xcmonkey/driver.rb', line 62 def launch_app `idb launch --udid #{udid} #{bundle_id}` wait_until_app_launched end |
#list_apps ⇒ Object
110 111 112 |
# File 'lib/xcmonkey/driver.rb', line 110 def list_apps `idb list-apps --udid #{udid}` end |
#list_booted_simulators ⇒ Object
91 92 93 |
# File 'lib/xcmonkey/driver.rb', line 91 def list_booted_simulators `idb list-targets`.split("\n").grep(/Booted/) end |
#list_targets ⇒ Object
86 87 88 89 |
# File 'lib/xcmonkey/driver.rb', line 86 def list_targets @list_targets ||= `idb list-targets`.split("\n") @list_targets end |
#monkey_test(gestures) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/xcmonkey/driver.rb', line 12 def monkey_test(gestures) app_elements = describe_ui.shuffle current_time = Time.now while Time.now < current_time + 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 Logger.error('App lost') if app_elements.include?(@home_tracker) end end |
#open_home_screen(with_tracker: false) ⇒ Object
47 48 49 50 |
# File 'lib/xcmonkey/driver.rb', line 47 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
119 120 121 122 |
# File 'lib/xcmonkey/driver.rb', line 119 def press(coordinates:, duration:) Logger.info("Press (#{duration}s):", payload: JSON.pretty_generate(coordinates)) `idb ui tap --udid #{udid} --duration #{duration} #{coordinates[:x]} #{coordinates[:y]}` end |
#press_duration ⇒ Object
167 168 169 |
# File 'lib/xcmonkey/driver.rb', line 167 def press_duration rand(0.5..1.5).ceil(1) end |
#random_coordinates ⇒ Object
143 144 145 146 147 148 |
# File 'lib/xcmonkey/driver.rb', line 143 def random_coordinates { x: rand(0..screen_size[:width].to_i), y: rand(0..screen_size[:height].to_i) } end |
#screen_size ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/xcmonkey/driver.rb', line 155 def screen_size screen_dimensions = device_info['screen_dimensions'] { width: screen_dimensions['width_points'], height: screen_dimensions['height_points'] } end |
#shutdown_simulator ⇒ Object
76 77 78 |
# File 'lib/xcmonkey/driver.rb', line 76 def shutdown_simulator `idb shutdown #{udid}` end |
#swipe(start_coordinates:, end_coordinates:, duration:) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/xcmonkey/driver.rb', line 124 def swipe(start_coordinates:, end_coordinates:, duration:) Logger.info( "Swipe (#{duration}s):", payload: "#{JSON.pretty_generate(start_coordinates)} => #{JSON.pretty_generate(end_coordinates)}" ) coordinates = "#{start_coordinates[:x]} #{start_coordinates[:y]} #{end_coordinates[:x]} #{end_coordinates[:y]}" `idb ui swipe --udid #{udid} --duration #{duration} #{coordinates}` end |
#swipe_duration ⇒ Object
163 164 165 |
# File 'lib/xcmonkey/driver.rb', line 163 def swipe_duration rand(0.1..0.7).ceil(1) end |
#tap(coordinates:) ⇒ Object
114 115 116 117 |
# File 'lib/xcmonkey/driver.rb', line 114 def tap(coordinates:) Logger.info('Tap:', payload: JSON.pretty_generate(coordinates)) `idb ui tap --udid #{udid} #{coordinates[:x]} #{coordinates[:y]}` end |
#terminate_app ⇒ Object
67 68 69 |
# File 'lib/xcmonkey/driver.rb', line 67 def terminate_app `idb terminate --udid #{udid} #{bundle_id} 2>/dev/null` end |