Class: ICukeWorld

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeICukeWorld

Returns a new instance of ICukeWorld.



13
14
15
# File 'lib/icuke/cucumber.rb', line 13

def initialize
  @simulator = ICuke::Simulator.new
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/icuke/cucumber.rb', line 11

def response
  @response
end

#simulatorObject (readonly)

Returns the value of attribute simulator.



11
12
13
# File 'lib/icuke/cucumber.rb', line 11

def simulator
  @simulator
end

Instance Method Details

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



70
71
72
73
74
# File 'lib/icuke/cucumber.rb', line 70

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/icuke/cucumber.rb', line 82

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



98
99
100
101
102
103
# File 'lib/icuke/cucumber.rb', line 98

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



76
77
78
79
80
# File 'lib/icuke/cucumber.rb', line 76

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



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

def launch(application, options = {})
  process = ICuke::Simulator::Process.new(application, options)
  @simulator.launch(process)
end

#quitObject



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

def quit
  @simulator.quit
end

#recordObject



42
43
44
# File 'lib/icuke/cucumber.rb', line 42

def record
  @simulator.record
end

#resumeObject



30
31
32
# File 'lib/icuke/cucumber.rb', line 30

def resume
  @simulator.resume
end

#screenObject



34
35
36
# File 'lib/icuke/cucumber.rb', line 34

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

#scroll(direction) ⇒ Object



163
164
165
# File 'lib/icuke/cucumber.rb', line 163

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

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



153
154
155
156
157
158
159
160
161
# File 'lib/icuke/cucumber.rb', line 153

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



167
168
169
# File 'lib/icuke/cucumber.rb', line 167

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

#suspendObject



26
27
28
# File 'lib/icuke/cucumber.rb', line 26

def suspend
  @simulator.suspend
end

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



63
64
65
66
67
68
# File 'lib/icuke/cucumber.rb', line 63

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)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/icuke/cucumber.rb', line 46

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



105
106
107
108
109
110
111
112
113
114
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
# File 'lib/icuke/cucumber.rb', line 105

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