Class Watir::Waiter

  1. commonwatir/lib/watir/waiter.rb
Parent: Object

Methods

public class

  1. new
  2. wait_until

public instance

  1. wait_until

Attributes

polling_interval [RW] How long to wait between each iteration through the wait_until loop. In seconds.
timeout [RW] Timeout for wait_until.
timer [RW] This is an interface to a TimeKeeper which proxies calls to “sleep” and “Time.now“. Useful for unit testing Waiter.

Public class methods

new (timeout=@@default_timeout, polling_interval=@@default_polling_interval)
[show source]
# File commonwatir/lib/watir/waiter.rb, line 38
    def initialize(timeout=@@default_timeout,
        polling_interval=@@default_polling_interval)
      @timeout = timeout
      @polling_interval = polling_interval
      @timer = TimeKeeper.new
    end
wait_until (timeout=@@default_timeout, polling_interval=@@default_polling_interval) {|| ...}

Execute the provided block until either (1) it returns true, or (2) the timeout (in seconds) has been reached. If the timeout is reached, a TimeOutException will be raised. The block will always execute at least once.

Waiter.wait_until(5) {puts 'hello'}

This code will print out “hello” for five seconds, and then raise a Watir::TimeOutException.

Example Usage:

Waiter.wait_until(20,4) do
  browser.text_field(:id, 'customer_id').exists?
end

This code will check every 4 seconds if a specific text_field exists. It will raise Watir::TimeOutException if text_field is not found after 20 seconds of waiting

[show source]
# File commonwatir/lib/watir/waiter.rb, line 87
    def self.wait_until(timeout=@@default_timeout,
        polling_interval=@@default_polling_interval)
      waiter = new(timeout, polling_interval)
      waiter.wait_until { yield }
    end

Public instance methods

wait_until (

Execute the provided block until either (1) it returns true, or (2) the timeout (in seconds) has been reached. If the timeout is reached, a TimeOutException will be raised. The block will always execute at least once.

waiter = Waiter.new(5)
waiter.wait_until {puts 'hello'}

This code will print out “hello” for five seconds, and then raise a Watir::TimeOutException.

[show source]
# File commonwatir/lib/watir/waiter.rb, line 55
    def wait_until # block
      start_time = now
      until yield do
        if (duration = now - start_time) > @timeout
          raise Watir::Exception::TimeOutException.new(duration, @timeout),
            "Timed out after #{duration} seconds."
        end
        sleep @polling_interval
      end
    end