Module: Awetestlib::Regression::Waits

Included in:
Runner
Defined in:
lib/awetestlib/regression/waits.rb

Instance Method Summary collapse

Instance Method Details

#hold_for_text(browser, howLong, text, desc = '', threshold = 20, interval = 0.25) ⇒ Object Also known as: wait_for_text

:tags:wait howLong is the number of seconds, text is a string to be found, threshold is the number of seconds after which a fail message is generated even though the text was detected within the howLong limit. Use this in place of wait_until_by_text when the wait time needs to be longer than the test automation default.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/awetestlib/regression/waits.rb', line 19

def hold_for_text(browser, howLong, text, desc = '', threshold = 20, interval = 0.25)
  countdown = howLong
  while ((not browser.contains_text(text)) and countdown > 0)
    sleep(interval)
    countdown = countdown - interval
  end
  if countdown < howLong
    waittime = howLong - countdown
    passed_tolog("#{__method__}  '#{text}' found after #{waittime} second(s) #{desc}")
    if waittime > threshold
      failed_tolog("#{__method__}  '#{text}' took #{waittime} second(s). (threshold: #{threshold} seconds) #{desc}")
    end
    true
  else
    failed_tolog("#{__method__}  '#{text}' not found after #{howLong} second(s) #{desc}")
    false
  end
rescue
  failed_tolog("Unable to #{__method__} '#{text}'. '#{$!}' #{desc}")
end

#sleep_for(seconds, dbg = true, desc = '') ⇒ Object



5
6
7
8
9
10
# File 'lib/awetestlib/regression/waits.rb', line 5

def sleep_for(seconds, dbg = true, desc = '')
  msg = "Sleeping for #{seconds} seconds."
  msg << " #{desc}" if desc.length > 0
  info_to_log(msg)
  sleep(seconds)
end

#wait_for(howLong, whatFor) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/awetestlib/regression/waits.rb', line 68

def wait_for(howLong, whatFor)
  countdown = howLong
  while ((not whatFor.exists?) and countdown > 0)
    sleep(1)
    puts whatFor.inspect+':'+countdown.to_s
    countdown = countdown - 1
  end
  if countdown
    puts 'found '+whatFor.inspect
    passed_tolog("wait_for (#{howLong} found "+whatFor.inspect)
  else
    puts 'Did not find '+whatFor.inspect
    failed_tolog("wait_for (#{howLong} did not find "+whatFor.inspect)
  end
  countdown
end

#wait_for_element_to_reappear(browser, how, what, desc = '', timeout = 20) ⇒ Object



57
58
59
60
61
# File 'lib/awetestlib/regression/waits.rb', line 57

def wait_for_element_to_reappear(browser, how, what, desc = '', timeout = 20)
  msg = "Element #{how}=#{what} exists. #{desc}"
  wait_while(browser, "While: #{msg}", timeout) { browser.element(how, what).exists? }
  wait_until(browser, "Until: #{msg}", timeout) { browser.element(how, what).exists? }
end

#wait_for_exists(howLong, whatFor) ⇒ Object

howLong is integer, whatFor is a browser object



64
65
66
# File 'lib/awetestlib/regression/waits.rb', line 64

def wait_for_exists(howLong, whatFor)
  wait_for(howLong, whatFor)
end

#wait_the_hard_way(browser, how, what, wait = 6, intvl = 0.25) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/awetestlib/regression/waits.rb', line 85

def wait_the_hard_way(browser, how, what, wait = 6, intvl = 0.25)
  count = (wait / intvl).to_i + 1
  tally = 0
  ok    = (1 / intvl).to_i + 1
  debug_to_log("#{__method__}: wait: #{wait} secs; intvl: #{intvl} secs; count; #{count}; thresh: #{ok}")
  (1..count).each do |x|
    begin
      if browser.element(how, what).exists?
        tally += 1
        debug_to_log("#{x}: #{(x - 1) * intvl}: #{what} exists.")
      else
        tally = 0
        debug_to_log("#{x}: #{(x - 1) * intvl}: #{what} does not exist.")
      end
    rescue
      tally = 0
      debug_to_log("#{x}: #{(x - 1) * intvl}: #{what} rescue: #{$!}")
    end
    if tally >= ok
      return true
    end
    sleep(intvl)
  end
end

#wait_until(browser, desc, timeout = 45, skip_pass = false, &block) ⇒ Object Also known as: wait_until_true



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/awetestlib/regression/waits.rb', line 192

def wait_until(browser, desc, timeout = 45, skip_pass = false, &block)
  #TODO: Would like to be able to see the block code in the log message instead of the identification
  msg   = "Wait until #{desc}"
  start = Time.now.to_f
  begin
    Watir::Wait.until(timeout) { block.call(nil) }
  rescue => e
    if e.class.to_s =~ /TimeOutException/ or e.message =~ /timed out/
      failed_to_log("#{msg} '#{$!}'")
      return false
    elsif not rescue_me(e, __method__, "#{block.to_s}", "#{browser.class}")
      raise e
    end
  end
  stop = Time.now.to_f
  #debug_to_log("#{__method__}: start:#{start} stop:#{stop} block: #{block.to_s}")
  #    sleep 1
  if validate(browser, @myName, __LINE__)
    passed_to_log("#{msg} (#{"%.5f" % (stop - start)} seconds)") unless skip_pass #  {#{block.to_s}}")
    true
  end
rescue
  failed_to_log("Unable to complete #{msg}  '#{$!}'")
end


299
300
301
# File 'lib/awetestlib/regression/waits.rb', line 299

def wait_until_by_link_text(browser, strg, desc = '')
  wait_until_exists(browser, :link, :text, strg, desc)
end

#wait_until_by_radio_value(browser, strg, desc = '') ⇒ Object



219
220
221
# File 'lib/awetestlib/regression/waits.rb', line 219

def wait_until_by_radio_value(browser, strg, desc = '')
  wait_until_exists(browser, :radio, :value, strg, desc)
end

#wait_until_enabled(browser, what, how, value, desc = '') ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/awetestlib/regression/waits.rb', line 303

def wait_until_enabled(browser, what, how, value, desc = '')
  # TODO: This can be simplified
  start = Time.now.to_f
  begin
    case what
      when :link
        Watir::Wait.until { browser.link(how, value).enabled? }
      when :button
        Watir::Wait.until { browser.button(how, value).enabled? }
      when :radio
        Watir::Wait.until { browser.radio(how, value).enabled? }
      when :checkbox
        Watir::Wait.until { browser.checkbox(how, value).enabled? }
      when :div
        Watir::Wait.until { browser.div(how, value).enabled? }
      when :select_list
        Watir::Wait.until { browser.select_list(how, value).enabled? }
      when :text_field
        Watir::Wait.until { browser.text_field(how, value).enabled? }
      when :table
        Watir::Wait.until { browser.table(how, value).enabled? }
      else
        raise "#{__method__}: Element #{what} not supported."
    end
  rescue => e
    if e.class.to_s =~ /TimeOutException/
      failed_to_log("Wait until (#{what} :#{how}=>#{value}) enabled. #{desc}: '#{$!}' #{desc}")
      return false
    elsif not rescue_me(e, __method__, "#{block.to_s}", "#{browser.class}")
      raise e
    end
  end
  stop = Time.now.to_f
  #debug_to_log("#{__method__}: start:#{start} stop:#{stop}")
  #    sleep 1
  if validate(browser, @myName, __LINE__)
    passed_to_log("Wait until (#{what} :#{how}=>#{value}) enabled. #{desc} (#{stop - start} seconds)")
    true
  end
rescue
  failed_to_log("Unable to complete wait until (#{what} :#{how}=>#{value}) enabled. #{desc}: '#{$!}'")
end

#wait_until_exists(browser, element, how, what, desc = '') ⇒ Object



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
152
153
154
155
156
157
158
159
160
# File 'lib/awetestlib/regression/waits.rb', line 110

def wait_until_exists(browser, element, how, what, desc = '')
  msg = "Wait until (#{element} :#{how}=>#{what}) exists."
  msg << " #{desc}" if desc.length > 0
  start = Time.now.to_f
  # TODO: try Watir::Wait.until { browser.element(how, what).exists? } instead of this (cumbersome) case statement
  # TODO: above fails on frame
  begin
    case element
      when :link
        Watir::Wait.until { browser.link(how, what).exists? }
      when :button
        Watir::Wait.until { browser.button(how, what).exists? }
      when :radio
        Watir::Wait.until { browser.radio(how, what).exists? }
      when :checkbox
        Watir::Wait.until { browser.checkbox(how, what).exists? }
      when :div
        Watir::Wait.until { browser.div(how, what).exists? }
      when :select_list
        Watir::Wait.until { browser.select_list(how, what).exists? }
      when :text_field
        Watir::Wait.until { browser.text_field(how, what).exists? }
      when :frame
        Watir::Wait.until { browser.frame(how, what).exists? }
      when :form
        Watir::Wait.until { browser.form(how, what).exists? }
      when :cell
        Watir::Wait.until { browser.cell(how, what).exists? }
      when :image
        Watir::Wait.until { browser.image(how, what).exists? }
      else
        Watir::Wait.until { browser.element(how, what).exists? }
    end
  rescue => e
    if e.class.to_s =~ /TimeOutException/
      failed_to_log("#{msg}: '#{$!}'")
      return false
    elsif not rescue_me(e, __method__, "#{block.to_s}", "#{browser.class}")
      raise e
    end
  end
  stop = Time.now.to_f
  #debug_to_log("#{__method__}: start:#{start} stop:#{stop}")
  #    sleep 1
  if validate(browser, @myName, __LINE__)
    passed_to_log("#{msg} (#{stop - start} seconds)")
    true
  end
rescue
  failed_to_log("Unable to complete #{msg}: '#{$!}'")
end

#wait_until_ready(browser, how, what, desc = '', timeout = 90, verbose = false) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/awetestlib/regression/waits.rb', line 223

def wait_until_ready(browser, how, what, desc = '', timeout = 90, verbose = false)
  msg = "#{__method__.to_s.titleize}: element: #{how}='#{what}'"
  msg << " #{desc}" if desc.length > 0
  proc_exists  = Proc.new { browser.element(how, what).exists? }
  proc_enabled = Proc.new { browser.element(how, what).enabled? }
  case how
    when :href
      proc_exists  = Proc.new { browser.link(how, what).exists? }
      proc_enabled = Proc.new { browser.link(how, what).enabled? }
  end
  if verbose
    if wait_until(browser, "#{msg} Element exists.", timeout) { proc_exists.call(nil) }
      if wait_until(browser, "#{msg} Element enabled.", timeout) { proc_enabled.call(nil) }
        passed_to_log(msg)
        true
      else
        failed_to_log(msg)
      end
    else
      failed_to_log(msg)
    end
  else
    start = Time.now.to_f
    if Watir::Wait.until(timeout) { proc_exists.call(nil) }
      if Watir::Wait.until(timeout) { proc_enabled.call(nil) }
        stop = Time.now.to_f
        #debug_to_log("#{__method__}: start:#{"%.5f" % start} stop:#{"%.5f" % stop}")
        passed_to_log("#{msg} (#{"%.5f" % (stop - start)} seconds)")
        true
      else
        failed_to_log(msg)
      end
    else
      failed_to_log(msg)
    end
  end
rescue
  failed_to_log("Unable to #{msg}. '#{$!}'")
end

#wait_until_ready_quiet(browser, how, what, desc = '', timeout = 45, quiet = true) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/awetestlib/regression/waits.rb', line 263

def wait_until_ready_quiet(browser, how, what, desc = '', timeout = 45, quiet = true)
  msg = "#{__method__.to_s.titleize}: element: #{how}='#{what}'"
  msg << " #{desc}" if desc.length > 0
  proc_exists  = Proc.new { browser.element(how, what).exists? }
  proc_enabled = Proc.new { browser.element(how, what).enabled? }
  case how
    when :href
      proc_exists  = Proc.new { browser.link(how, what).exists? }
      proc_enabled = Proc.new { browser.link(how, what).enabled? }
  end
  start = Time.now.to_f
  if Watir::Wait.until(timeout) { proc_exists.call(nil) }
    if Watir::Wait.until(timeout) { proc_enabled.call(nil) }
      stop = Time.now.to_f
      #debug_to_log("#{msg}: start:#{"%.5f" % start} stop:#{"%.5f" % stop}")
      passed_to_log("#{msg} (#{"%.5f" % (stop - start)} seconds)") unless quiet
      true
    else
      failed_to_log(msg)
    end
  else
    failed_to_log(msg)
  end
rescue
  failed_to_log("Unable to #{msg}. '#{$!}'")
end

#wait_until_text(browser, strg, desc = '', timeout = 60) ⇒ Object Also known as: wait_until_by_text



290
291
292
293
294
295
# File 'lib/awetestlib/regression/waits.rb', line 290

def wait_until_text(browser, strg, desc = '', timeout = 60)
  if not strg.class.to_s.match('String')
    raise "#{__method__} requires String for search target. #{strg.class} is not supported."
  end
  wait_until(browser, "'#{strg}' #{desc}", timeout) { browser.text.include? strg }
end

#wait_until_visible(browser, element, how, what, desc = '') ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/awetestlib/regression/waits.rb', line 346

def wait_until_visible(browser, element, how, what, desc = '')
  start = Time.now.to_f
  Watir::Wait.until(20) { browser.element(how, what).exists? }
  begin
    case element
      when :link
        Watir::Wait.until { browser.link(how, what).visible? }
      when :button
        Watir::Wait.until { browser.button(how, what).visible? }
      when :radio
        Watir::Wait.until { browser.radio(how, what).visible? }
      when :checkbox
        Watir::Wait.until { browser.checkbox(how, what).visible? }
      when :div
        Watir::Wait.until { browser.div(how, what).visible? }
      when :select_list
        Watir::Wait.until { browser.select_list(how, what).visible? }
      when :text_field
        Watir::Wait.until { browser.text_field(how, what).visible? }
      else
        Watir::Wait.until { browser.element(how, what).visible? }
    #          raise "#{__method__}: Element #{what} not supported."
    end
  rescue => e
    if e.class.to_s =~ /TimeOutException/
      failed_to_log("Wait until (#{what} :#{how}=>#{what}) visible. #{desc}: '#{$!}' #{desc}")
      return false
    elsif not rescue_me(e, __method__, '', "#{browser.class}")
      raise e
    end
  end
  stop = Time.now.to_f
  #debug_to_log("#{__method__}: start:#{start} stop:#{stop}")
  #    sleep 1
  if validate(browser, @myName, __LINE__)
    passed_to_log("Wait until (#{element} :#{how}=>#{what}) visible. #{desc} (#{stop - start} seconds)")
    true
  end
rescue
  failed_to_log("Unable to complete wait until (#{element} :#{how}=>#{what}) visible. #{desc}: '#{$!}'")
end

#wait_while(browser, desc, timeout = 45, &block) ⇒ Object Also known as: wait_while_true



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/awetestlib/regression/waits.rb', line 162

def wait_while(browser, desc, timeout = 45, &block)
  #TODO: Would like to be able to see the block code in the log message instead of the identification
  msg   = "Wait while #{desc}:"
  start = Time.now.to_f
  begin
    #Watir::Wait.until(timeout) { block.call(nil) }
    if block.call(nil)
      Watir::Wait.while(timeout) { block.call(nil) }
    end
  rescue => e
    if e.class.to_s =~ /TimeOutException/ or e.message =~ /timed out/
      failed_to_log("#{msg}: '#{$!}' ")
      return false
    elsif not rescue_me(e, __method__, "#{block.to_s}", "#{browser.class}")
      raise e
    end
  end
  stop = Time.now.to_f
  #debug_to_log("#{__method__}: start:#{start} stop:#{stop} block: #{block.to_s}")
  #    sleep 1
  if validate(browser, @myName, __LINE__)
    passed_to_log("#{msg} (#{"%.5f" % (stop - start)} seconds)") #  {#{block.to_s}}")
    true
  end
rescue
  failed_to_log("Unable to complete #{msg}. '#{$!}'")
end