Class: ICuke::SimulatorDriver

Inherits:
Object
  • Object
show all
Includes:
ICuke::Simulate::Gestures
Defined in:
lib/icuke/simulator_driver.rb

Constant Summary collapse

DSL_METHODS =
[:launch, :quit, :suspend, :resume, :screen,
:response, :record, :tap, :swipe, :drag,
:drag_with_source, :drag_slider_to, 
:drag_slider_to_percentage, :type, :scroll_to,
:scroll, :set_application_defaults]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(simulator, configuration) ⇒ SimulatorDriver

Returns a new instance of SimulatorDriver.



17
18
19
20
# File 'lib/icuke/simulator_driver.rb', line 17

def initialize(simulator, configuration)
  @simulator = simulator
  @configuration = configuration
end

Class Method Details

.default_driver(configuration) ⇒ Object



22
23
24
# File 'lib/icuke/simulator_driver.rb', line 22

def self.default_driver(configuration)
  new(ICuke::Simulator.new, configuration)
end

Instance Method Details

#drag(source_x, source_y, dest_x, dest_y, options = {}) ⇒ Object



80
81
82
83
84
# File 'lib/icuke/simulator_driver.rb', line 80

def drag(source_x, source_y, dest_x, dest_y, options = {})
  @simulator.fire_event(Drag.new(source_x, source_y, dest_x, dest_y, 0.15, options))
  sleep(1)
  refresh
end

#drag_slider_to(label, direction, distance) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/icuke/simulator_driver.rb', line 92

def drag_slider_to(label, direction, distance)
  element = screen.first_slider_element(label)
  x, y = screen.find_slider_button(element)

  dest_x, dest_y = x, y
  modifier = direction_modifier(direction)

  if [:up, :down].include?(direction)
    dest_y += modifier * distance
  else
    dest_x += modifier * distance
  end

  drag(x, y, dest_x, dest_y)
end

#drag_slider_to_percentage(label, percentage) ⇒ Object



108
109
110
111
112
113
# File 'lib/icuke/simulator_driver.rb', line 108

def drag_slider_to_percentage(label, percentage)
  element = screen.first_slider_element(label)
  x, y = screen.find_slider_button(element)
  dest_x, dest_y = screen.find_slider_percentage_location(element, percentage)
  drag(x, y, dest_x, dest_y)
end

#drag_with_source(source, destination) ⇒ Object



86
87
88
89
90
# File 'lib/icuke/simulator_driver.rb', line 86

def drag_with_source(source, destination)
  sources = source.split(',').collect {|val| val.strip.to_i}
  destinations = destination.split(',').collect {|val| val.strip.to_i}
  drag(sources[0], sources[1], destinations[0], destinations[1])
end

#launch(application, options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/icuke/simulator_driver.rb', line 26

def launch(application, options = {})
  default_options = {:build_configuration => configuration[:build_configuration]}
  process = ICuke::Simulator::Process.new(application, default_options.merge(options))
  @simulator.launch(process)
end

#quitObject



32
33
34
# File 'lib/icuke/simulator_driver.rb', line 32

def quit
  @simulator.quit
end

#recordObject



52
53
54
# File 'lib/icuke/simulator_driver.rb', line 52

def record
  @simulator.record
end

#refreshObject



181
182
183
184
# File 'lib/icuke/simulator_driver.rb', line 181

def refresh
  @response = nil
  @screen = nil
end

#responseObject



48
49
50
# File 'lib/icuke/simulator_driver.rb', line 48

def response
  @response ||= @simulator.view
end

#resumeObject



40
41
42
# File 'lib/icuke/simulator_driver.rb', line 40

def resume
  @simulator.resume
end

#screenObject



44
45
46
# File 'lib/icuke/simulator_driver.rb', line 44

def screen
  @screen ||= Screen.new(response)
end

#scroll(direction) ⇒ Object



173
174
175
# File 'lib/icuke/simulator_driver.rb', line 173

def scroll(direction)
  swipe(swipe_direction(direction))
end

#scroll_to(text, options = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/icuke/simulator_driver.rb', line 163

def scroll_to(text, options = {})
  x, y, x2, y2 = screen.swipe_coordinates(swipe_direction(options[:direction]))
  previous_response = response.dup
  until screen.visible?(text) do
    @simulator.fire_event(Swipe.new(x, y, x2, y2, 0.15, options))
    refresh
    raise %Q{Content "#{text}" not found in: #{screen}} if response == previous_response
  end
end

#set_application_defaults(defaults) ⇒ Object



177
178
179
# File 'lib/icuke/simulator_driver.rb', line 177

def set_application_defaults(defaults)
  @simulator.set_defaults(defaults)
end

#suspendObject



36
37
38
# File 'lib/icuke/simulator_driver.rb', line 36

def suspend
  @simulator.suspend
end

#swipe(direction, options = {}) ⇒ Object



73
74
75
76
77
78
# File 'lib/icuke/simulator_driver.rb', line 73

def swipe(direction, options = {})
  x, y, x2, y2 = screen.swipe_coordinates(direction)
  @simulator.fire_event(Swipe.new(x, y, x2, y2, 0.015, options))
  sleep(1)
  refresh
end

#tap(label, options = {}) {|element| ... } ⇒ Object

Yields:

  • (element)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/icuke/simulator_driver.rb', line 56

def tap(label, options = {}, &block)
  options = {
    :pause => true
  }.merge(options)

  element = screen.first_tappable_element(label)
  x, y = screen.element_center(element)

  @simulator.fire_event(Tap.new(x, y, options))

  sleep(options[:pause] ? 2 : 0.2)

  refresh

  yield element if block_given?
end

#type(textfield, text, options = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/icuke/simulator_driver.rb', line 115

def type(textfield, text, options = {})
  tap(textfield, :hold_for => 0.75) do |field|
    if field['value']
      tap('Select All')
      tap('Delete')
    end
  end

  # Without this sleep fields which have auto-capitilisation/correction can
  # miss the first keystroke for some reason.
  sleep(0.5)

  text.split('').each do |c|
    begin
      tap(c == ' ' ? 'space' : c, :pause => false)
    rescue Exception => e
      try_keyboards =
        case c
        when /[a-zA-Z]/
          ['more, letters', 'shift']
        when /[0-9]/
          ['more, numbers']
        else
          ['more, numbers', 'more, symbols']
        end
      until try_keyboards.empty?
        begin
          tap(try_keyboards.shift, :pause => false)
          retry
        rescue
        end
      end
      raise e
    end
  end

  # From UIReturnKeyType
  # Should probably sort these in rough order of likelyhood?
  return_keys = ['return', 'go', 'google', 'join', 'next', 'route', 'search', 'send', 'yahoo', 'done', 'emergency call']
  return_keys.each do |key|
    begin
      tap(key)
      return
    rescue
    end
  end
end