Module: Calabash::Android::WaitHelpers

Included in:
Operations
Defined in:
lib/calabash-android/wait_helpers.rb

Defined Under Namespace

Classes: WaitError

Constant Summary collapse

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
}

Instance Method Summary collapse

Instance Method Details

#handle_error_with_options(ex, timeout_message, screenshot_on_error) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/calabash-android/wait_helpers.rb', line 123

def handle_error_with_options(ex, timeout_message, screenshot_on_error)
  error_class = (ex && ex.class) || RuntimeError
  error = error_class.new(timeout_message || ex.message)

  if screenshot_on_error
    screenshot_and_raise error
  else
    raise error
  end
end

#screenshot_and_retry(msg, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/calabash-android/wait_helpers.rb', line 56

def screenshot_and_retry(msg, &block)
  path  = screenshot
  res = yield
  # Validate after taking screenshot
  if res
    FileUtils.rm_f(path)
    return res
  else
    begin
      embed(path, 'image/png', msg)
    rescue NoMethodError
      attach(path, 'image/png')
    end
    
    raise wait_error(msg)
  end
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”) })



164
165
166
167
168
169
170
171
# File 'lib/calabash-android/wait_helpers.rb', line 164

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”) })



147
148
149
150
151
152
153
# File 'lib/calabash-android/wait_helpers.rb', line 147

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



134
135
136
# File 'lib/calabash-android/wait_helpers.rb', line 134

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

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



19
20
21
22
23
24
25
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
# File 'lib/calabash-android/wait_helpers.rb', line 19

def wait_for(options_or_timeout=DEFAULT_OPTS, &block)
  #note Hash is preferred, number acceptable for backwards compat
  default_timeout = DEFAULT_OPTS[:timeout]
  timeout = options_or_timeout || default_timeout
  post_timeout = DEFAULT_OPTS[:post_timeout]
  retry_frequency = DEFAULT_OPTS[:retry_frequency]
  timeout_message = DEFAULT_OPTS[:timeout_message]
  screenshot_on_error = DEFAULT_OPTS[:screenshot_on_error]

  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 => e
    handle_error_with_options(e, nil, screenshot_on_error)
  end
end

#wait_for_activity(activity_name, options = {}) ⇒ Object



201
202
203
204
205
# File 'lib/calabash-android/wait_helpers.rb', line 201

def wait_for_activity(activity_name, options={})
  wait_for(options) do
    perform_action('get_activity_name')['message'] == activity_name
  end
end

#wait_for_element_does_not_exist(uiquery, options = {}) ⇒ Object

options for wait_for apply



108
109
110
# File 'lib/calabash-android/wait_helpers.rb', line 108

def wait_for_element_does_not_exist(uiquery, options={})
  wait_for_elements_do_not_exist([uiquery], options)
end

#wait_for_element_exists(uiquery, options = {}) ⇒ Object

options for wait_for apply



92
93
94
# File 'lib/calabash-android/wait_helpers.rb', line 92

def wait_for_element_exists(uiquery, options={})
  wait_for_elements_exist([uiquery], options)
end

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

options for wait_for apply



113
114
115
116
117
118
119
120
121
# File 'lib/calabash-android/wait_helpers.rb', line 113

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



97
98
99
100
101
102
103
104
105
# File 'lib/calabash-android/wait_helpers.rb', line 97

def wait_for_elements_exist(elements_arr, options={})
  if elements_arr.is_a?(String) || elements_arr.is_a?(Symbol)
    elements_arr = [elements_arr.to_s]
  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_text(text, options = {}) ⇒ Object



193
194
195
# File 'lib/calabash-android/wait_helpers.rb', line 193

def wait_for_text(text, options={})
  wait_for_element_exists("* {text CONTAINS[c] '#{text}'}", options)
end

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



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

def wait_for_text_to_disappear(text, options={})
  wait_for_element_does_not_exist("* {text CONTAINS[c] '#{text}'}", options)
end

#wait_poll(opts, &block) ⇒ Object



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

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)



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/calabash-android/wait_helpers.rb', line 181

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

  if opts[:action].parameters.length == 0
    opts[:action].call
  else
    opts[:action].call(uiquery)
  end
end