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
# 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_idObject

Returns the value of attribute bundle_id.



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

def bundle_id
  @bundle_id
end

#durationObject

Returns the value of attribute duration.



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

def duration
  @duration
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



58
59
60
61
# File 'lib/xcmonkey/driver.rb', line 58

def boot_simulator
  `idb boot #{udid}`
  ensure_simulator_was_booted
end

#central_coordinates(element) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/xcmonkey/driver.rb', line 107

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



43
44
45
46
47
# File 'lib/xcmonkey/driver.rb', line 43

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



39
40
41
# File 'lib/xcmonkey/driver.rb', line 39

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

#ensure_app_installedObject



76
77
78
# File 'lib/xcmonkey/driver.rb', line 76

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



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

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_bootedObject



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

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_appObject



49
50
51
52
# File 'lib/xcmonkey/driver.rb', line 49

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

#list_appsObject



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

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

#list_booted_simulatorsObject



72
73
74
# File 'lib/xcmonkey/driver.rb', line 72

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

#list_targetsObject



67
68
69
70
# File 'lib/xcmonkey/driver.rb', line 67

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
31
32
# 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)
    else
      next
    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



34
35
36
37
# File 'lib/xcmonkey/driver.rb', line 34

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

#shutdown_simulatorObject



63
64
65
# File 'lib/xcmonkey/driver.rb', line 63

def shutdown_simulator
  `idb shutdown #{udid}`
end

#swipe(start_coordinates:, end_coordinates:) ⇒ Object



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

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



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

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

#terminate_appObject



54
55
56
# File 'lib/xcmonkey/driver.rb', line 54

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