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.
-
#udid ⇒ Object
Returns the value of attribute udid.
Instance Method Summary collapse
- #boot_simulator ⇒ Object
- #central_coordinates(element) ⇒ Object
- #describe_point(x, y) ⇒ Object
- #describe_ui ⇒ Object
- #ensure_app_installed ⇒ Object
- #ensure_device_exists ⇒ Object
- #ensure_simulator_was_booted ⇒ 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(return_tracker: false) ⇒ Object
- #shutdown_simulator ⇒ Object
- #swipe(start_coordinates:, end_coordinates:) ⇒ Object
- #tap(coordinates:) ⇒ Object
- #terminate_app ⇒ Object
Constructor Details
#initialize(params) ⇒ Driver
Returns a new instance of Driver.
4 5 6 7 8 9 |
# File 'lib/xcmonkey/driver.rb', line 4 def initialize(params) self.udid = params[:udid] self.bundle_id = params[:bundle_id] self.duration = params[:duration] 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 |
#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
55 56 57 58 |
# File 'lib/xcmonkey/driver.rb', line 55 def boot_simulator `idb boot #{udid}` ensure_simulator_was_booted end |
#central_coordinates(element) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/xcmonkey/driver.rb', line 104 def central_coordinates(element) frame = element['frame'] { x: (frame['x'] + (frame['width'] / 2)).to_i, y: (frame['y'] + (frame['height'] / 2)).to_i } end |
#describe_point(x, y) ⇒ Object
41 42 43 44 45 |
# File 'lib/xcmonkey/driver.rb', line 41 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
37 38 39 |
# File 'lib/xcmonkey/driver.rb', line 37 def describe_ui JSON.parse(`idb ui describe-all --udid #{udid}`) end |
#ensure_app_installed ⇒ Object
73 74 75 |
# File 'lib/xcmonkey/driver.rb', line 73 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
77 78 79 80 81 82 |
# File 'lib/xcmonkey/driver.rb', line 77 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) boot_simulator if device.include?('simulator') end |
#ensure_simulator_was_booted ⇒ Object
84 85 86 87 |
# File 'lib/xcmonkey/driver.rb', line 84 def ensure_simulator_was_booted sim = list_booted_simulators.detect { |target| target.include?(udid) } Logger.error("Failed to boot #{udid}") if sim.nil? end |
#launch_app ⇒ Object
47 48 49 |
# File 'lib/xcmonkey/driver.rb', line 47 def launch_app `idb launch --udid #{udid} #{bundle_id}` end |
#list_apps ⇒ Object
89 90 91 |
# File 'lib/xcmonkey/driver.rb', line 89 def list_apps `idb list-apps --udid #{udid}` end |
#list_booted_simulators ⇒ Object
69 70 71 |
# File 'lib/xcmonkey/driver.rb', line 69 def list_booted_simulators `idb list-targets`.split("\n").grep(/Booted/) end |
#list_targets ⇒ Object
64 65 66 67 |
# File 'lib/xcmonkey/driver.rb', line 64 def list_targets @list_targets ||= `idb list-targets`.split("\n") @list_targets end |
#monkey_test(gestures) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/xcmonkey/driver.rb', line 11 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 x = (el1_coordinates[:x] - el2_coordinates[:x]).abs y = (el1_coordinates[:y] - el2_coordinates[:y]).abs tap(coordinates: { x: x, y: y }) when :swipe swipe(start_coordinates: el1_coordinates, end_coordinates: el2_coordinates) end app_elements = describe_ui.shuffle Logger.error('App lost') if app_elements.include?(@home_tracker) end end |
#open_home_screen(return_tracker: false) ⇒ Object
32 33 34 35 |
# File 'lib/xcmonkey/driver.rb', line 32 def open_home_screen(return_tracker: false) `idb ui button --udid #{udid} HOME` detect_home_unique_element if return_tracker end |
#shutdown_simulator ⇒ Object
60 61 62 |
# File 'lib/xcmonkey/driver.rb', line 60 def shutdown_simulator `idb shutdown #{udid}` end |
#swipe(start_coordinates:, end_coordinates:) ⇒ Object
98 99 100 101 102 |
# File 'lib/xcmonkey/driver.rb', line 98 def swipe(start_coordinates:, end_coordinates:) Logger.info('Swipe:', 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 0.5 #{coordinates}` end |
#tap(coordinates:) ⇒ Object
93 94 95 96 |
# File 'lib/xcmonkey/driver.rb', line 93 def tap(coordinates:) Logger.info('Tap:', payload: JSON.pretty_generate(coordinates)) `idb ui tap --udid #{udid} #{coordinates[:x]} #{coordinates[:y]}` end |
#terminate_app ⇒ Object
51 52 53 |
# File 'lib/xcmonkey/driver.rb', line 51 def terminate_app `idb terminate --udid #{udid} #{bundle_id} 2>/dev/null` end |