Module: Calabash::Cucumber::WaitHelpers

Includes:
Core, TestsHelpers
Included in:
Operations
Defined in:
lib/calabash-cucumber/wait_helpers.rb

Defined Under Namespace

Classes: WaitError

Constant Summary collapse

CALABASH_CONDITIONS =
{:none_animating => "NONE_ANIMATING",
:no_network_indicator => "NO_NETWORK_INDICATOR"}
DEFAULT_OPTS =

‘post_timeout’ is the time to wait after a wait function returns true

{
  :timeout => 30,
  :retry_frequency => 0.3,
  :post_timeout => 0,
  :timeout_message => 'Timed out waiting...',
  :screenshot_on_error => true
}.freeze

Constants included from PlaybackHelpers

PlaybackHelpers::DATA_PATH

Instance Method Summary collapse

Methods included from TestsHelpers

#check_element_does_not_exist, #check_element_exists, #check_view_with_mark_exists, #classes, #each_cell, #each_cell_and_back, #element_does_not_exist, #element_exists, #navigation_path, #query_map, #view_with_mark_exists

Methods included from FailureHelpers

#fail, #screenshot, #screenshot_and_raise, #screenshot_embed

Methods included from Core

#_debug_level_response, #backdoor, #calabash_exit, #cell_swipe, #client_version, #console_attach, #double_tap, #extract_query_and_options, #flash, #flick, #launcher, #location_for_place, #locations_for_place, #macro, #move_wheel, #pan, #picker, #pinch, #prepare_query_options, #query, #query_action_with_options, #query_all, #scroll, #scroll_to_cell, #scroll_to_collection_view_item, #scroll_to_collection_view_item_with_mark, #scroll_to_row, #scroll_to_row_with_mark, #send_app_to_background, #server_debug_level, #server_version, #set_location, #set_server_debug_level, #shutdown_test_server, #start_test_server_in_background, #stop_test_server, #swipe, #touch, #touch_hold, #two_finger_tap

Methods included from PlaybackHelpers

#find_compatible_recording, #interpolate, #load_playback_data, #load_recording, #playback, #playback_file_directories, #record_begin, #record_end, #recording_name_for

Methods included from RotationHelpers

#rotate, #rotate_home_button_to, #rotation_candidates

Methods included from StatusBarHelpers

#device_orientation, #landscape?, #portrait?, #status_bar_orientation

Methods included from UIA

#escape_uia_string, #send_uia_command, #uia, #uia_call, #uia_call_method, #uia_call_windows, #uia_double_tap, #uia_double_tap_mark, #uia_double_tap_offset, #uia_element_does_not_exist?, #uia_element_exists?, #uia_enter, #uia_flick_offset, #uia_handle_command, #uia_names, #uia_pan, #uia_pan_offset, #uia_pinch, #uia_pinch_offset, #uia_query, #uia_query_el, #uia_query_windows, #uia_screenshot, #uia_scroll_to, #uia_send_app_to_background, #uia_serialize_argument, #uia_serialize_arguments, #uia_serialize_command, #uia_set_location, #uia_swipe, #uia_swipe_offset, #uia_tap, #uia_tap_mark, #uia_tap_offset, #uia_touch_hold, #uia_touch_hold_offset, #uia_two_finger_tap, #uia_two_finger_tap_offset, #uia_type_string

Methods included from Map

#assert_map_results, #map, #raw_map

Methods included from QueryHelpers

#escape_quotes, #point_from

Methods included from ConnectionHelpers

#connection, #http

Methods included from EnvironmentHelpers

#_deprecated, #debug_logging?, #default_device, #device_family_iphone?, #full_console_logging?, #ios5?, #ios6?, #ios7?, #ipad?, #iphone?, #iphone_4in?, #iphone_5?, #iphone_app_emulated_on_ipad?, #ipod?, #no_deprecation_warnings?, #simulator?, #uia_available?, #uia_not_available?, #xamarin_test_cloud?

Instance Method Details

#handle_error_with_options(ex, timeout_message, screenshot_on_error) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/calabash-cucumber/wait_helpers.rb', line 185

def handle_error_with_options(ex, timeout_message, screenshot_on_error)
  msg = (timeout_message || ex)
  if ex
    msg = "#{msg} (#{ex.class})"
  end
  if screenshot_on_error
    screenshot_and_raise msg
  else
    raise msg
  end
end

#screenshot_and_retry(msg, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/calabash-cucumber/wait_helpers.rb', line 63

def screenshot_and_retry(msg, &block)
  path  = screenshot
  res = yield
  # Validate after taking screenshot
  if res
    FileUtils.rm_f(path)
    return res
  else
    embed(path, 'image/png', msg)
    raise wait_error(msg)
  end
end

#touch_transition(touch_q, done_queries, check_options = {}, animation_options = {}) ⇒ Object



180
181
182
183
# File 'lib/calabash-cucumber/wait_helpers.rb', line 180

def touch_transition(touch_q, done_queries,check_options={},animation_options={})
  touch(touch_q)
  wait_for_transition(done_queries,check_options,animation_options)
end

#until_element_does_not_exist(uiquery, opts = {}) ⇒ Object

Performs a lambda action until the element (a query string) disappears. The default action is to do nothing.

Raises an error if no uiquery is specified. Same options as wait_for which are timeout, retry frequency, post_timeout, timeout_message, and screenshot on error.

Example usage: until_element_does_not_exist(“Button”, :action => lambda { swipe(“up”) })



227
228
229
230
231
232
233
234
# File 'lib/calabash-cucumber/wait_helpers.rb', line 227

def until_element_does_not_exist(uiquery, opts = {})
  condition = lambda { element_exists(uiquery) ? false : true }
  extra_opts = { :until => condition, :action => lambda { ; } }
  opts = DEFAULT_OPTS.merge(extra_opts).merge(opts)
  wait_poll(opts) do 
    opts[:action].call
  end
end

#until_element_exists(uiquery, opts = {}) ⇒ Object

Performs a lambda action until the element (a query string) appears. The default action is to do nothing.

Raises an error if no uiquery is specified. Same options as wait_for which are timeout, retry frequency, post_timeout, timeout_message, and screenshot on error.

Example usage: until_element_exists(“Button”, :action => lambda { swipe(“up”) })



210
211
212
213
214
215
216
# File 'lib/calabash-cucumber/wait_helpers.rb', line 210

def until_element_exists(uiquery, opts = {})
  extra_opts = { :until_exists => uiquery, :action => lambda { ; } }
  opts = DEFAULT_OPTS.merge(extra_opts).merge(opts)
  wait_poll(opts) do 
    opts[:action].call
  end
end

#wait_error(msg) ⇒ Object



197
198
199
# File 'lib/calabash-cucumber/wait_helpers.rb', line 197

def wait_error(msg)
  (msg.is_a?(String) ? WaitError.new(msg) : msg)
end

#wait_for(options_or_timeout = DEFAULT_OPTS, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/calabash-cucumber/wait_helpers.rb', line 26

def wait_for(options_or_timeout=DEFAULT_OPTS, &block)
  #note Hash is preferred, number acceptable for backwards compat
  default_timeout = 30
  timeout = options_or_timeout || default_timeout
  post_timeout=0
  retry_frequency=0.3
  timeout_message = nil
  screenshot_on_error = true

  if options_or_timeout.is_a?(Hash)
    timeout = options_or_timeout[:timeout] || default_timeout
    retry_frequency = options_or_timeout[:retry_frequency] || retry_frequency
    post_timeout = options_or_timeout[:post_timeout] || post_timeout
    timeout_message = options_or_timeout[:timeout_message]
    if options_or_timeout.key?(:screenshot_on_error)
      screenshot_on_error = options_or_timeout[:screenshot_on_error]
    end
  end

  begin
    Timeout::timeout(timeout, WaitError) do
      sleep(retry_frequency) until yield
    end
    sleep(post_timeout) if post_timeout > 0
  rescue WaitError => e
    msg = timeout_message || e
    if screenshot_on_error
     sleep(retry_frequency)
     return screenshot_and_retry(msg, &block)
    else
     raise wait_error(msg)
   end
  rescue Exception => e
    handle_error_with_options(e, nil, screenshot_on_error)
  end
end

#wait_for_condition(options = {}) ⇒ Object



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
152
153
154
155
156
157
158
159
160
161
# File 'lib/calabash-cucumber/wait_helpers.rb', line 114

def wait_for_condition(options = {})
  options[:timeout] = options[:timeout] || 30
  options[:query] = options[:query] || "view"
  if options.has_key?(:condition)
    opt_condition = options[:condition]
    if opt_condition.is_a?(Symbol)
      target_condition = CALABASH_CONDITIONS[opt_condition]
    elsif opt_condition.is_a?(String)
      target_condition = options[:condition]
    end
    options[:condition] = target_condition
  end
  options[:condition] = options[:condition] || CALABASH_CONDITIONS[:none_animating]
  options[:post_timeout] = options[:post_timeout] || 0
  options[:frequency] = options[:frequency] || 0.3
  retry_frequency = options[:retry_frequency] = options[:retry_frequency] || 0.3
  options[:count] = options[:count] || 2
  timeout_message = options[:timeout_message] = options[:timeout_message] || "Timeout waiting for condition (#{options[:condition]})"
  screenshot_on_error = options[:screenshot_on_error] = options[:screenshot_on_error] || true

  begin
    Timeout::timeout(options[:timeout],WaitError) do
      loop do
        res = http({:method => :post, :path => 'condition'},
                   options)
        res = JSON.parse(res)
        break if res['outcome'] == 'SUCCESS'
        sleep(options[:retry_frequency]) if options[:retry_frequency] > 0
      end
      sleep(options[:post_timeout]) if options[:post_timeout] > 0
    end
  rescue WaitError => e
    msg = timeout_message || e
    if screenshot_on_error
      sleep(retry_frequency)
      return screenshot_and_retry(msg) do
        res = http({:method => :post, :path => 'condition'},
                   options)
        res = JSON.parse(res)
        res['outcome'] == 'SUCCESS'
      end
    else
      raise wait_error(msg)
    end
  rescue Exception => e
    handle_error_with_options(e,nil, options[:screenshot_on_error])
  end
end

#wait_for_elements_do_not_exist(elements_arr, options = {}) ⇒ Object

options for wait_for apply



104
105
106
107
108
109
110
111
112
# File 'lib/calabash-cucumber/wait_helpers.rb', line 104

def wait_for_elements_do_not_exist(elements_arr, options={})
  if elements_arr.is_a?(String)
    elements_arr = [elements_arr]
  end
  options[:timeout_message] = options[:timeout_message] || "Timeout waiting for no elements matching: #{elements_arr.join(",")}"
  wait_for(options) do
    elements_arr.none? { |q| element_exists(q) }
  end
end

#wait_for_elements_exist(elements_arr, options = {}) ⇒ Object

options for wait_for apply



94
95
96
97
98
99
100
101
102
# File 'lib/calabash-cucumber/wait_helpers.rb', line 94

def wait_for_elements_exist(elements_arr, options={})
  if elements_arr.is_a?(String)
    elements_arr = [elements_arr]
  end
  options[:timeout_message] = options[:timeout_message] || "Timeout waiting for elements: #{elements_arr.join(",")}"
  wait_for(options) do
    elements_arr.all? { |q| element_exists(q) }
  end
end

#wait_for_no_network_indicator(options = {}) ⇒ Object



168
169
170
171
# File 'lib/calabash-cucumber/wait_helpers.rb', line 168

def wait_for_no_network_indicator(options = {})
  options[:condition] = CALABASH_CONDITIONS[:no_network_indicator]
  wait_for_condition(options)
end

#wait_for_none_animating(options = {}) ⇒ Object



163
164
165
166
# File 'lib/calabash-cucumber/wait_helpers.rb', line 163

def wait_for_none_animating(options = {})
  options[:condition] = CALABASH_CONDITIONS[:none_animating]
  wait_for_condition(options)
end

#wait_for_transition(done_queries, check_options = {}, animation_options = {}) ⇒ Object

may be called with a string (query) or an array of strings



174
175
176
177
178
# File 'lib/calabash-cucumber/wait_helpers.rb', line 174

def wait_for_transition(done_queries, check_options={},animation_options={})
  done_queries = [*done_queries]
  wait_for_elements_exist(done_queries,check_options)
  wait_for_none_animating(animation_options)
end

#wait_poll(opts, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/calabash-cucumber/wait_helpers.rb', line 76

def wait_poll(opts, &block)
  test = opts[:until]
  if test.nil?
    cond = opts[:until_exists]
    raise 'Must provide :until or :until_exists' unless cond
    test = lambda { element_exists(cond) }
  end
  wait_for(opts) do
    if test.call()
      true
    else
      yield
      false
    end
  end
end

#when_element_exists(uiquery, opts = {}) ⇒ Object

Performs a lambda action once the element exists. The default behavior is to touch the specified element.

Raises an error if no uiquery is specified. Same options as wait_for which are timeout, retry frequency, post_timeout, timeout_message, and screenshot on error.

Example usage: when_element_exists(“Button”, :timeout => 10)



244
245
246
247
248
249
# File 'lib/calabash-cucumber/wait_helpers.rb', line 244

def when_element_exists(uiquery, opts = {})
  action = { :action => lambda { touch uiquery } }
  opts = DEFAULT_OPTS.merge(action).merge(opts)
  wait_for_elements_exist([uiquery], opts)
  opts[:action].call
end