Class: Watir::Waiter

Inherits:
Object
  • Object
show all
Defined in:
lib/watir/waiter.rb

Constant Summary collapse

@@default_polling_interval =
0.5
@@default_timeout =
60.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout = @@default_timeout, polling_interval = @@default_polling_interval) ⇒ Waiter

Returns a new instance of Waiter.



43
44
45
46
47
48
49
# File 'lib/watir/waiter.rb', line 43

def initialize(timeout=@@default_timeout,
               polling_interval=@@default_polling_interval)
  Kernel.warn "Using Watir::Waiter is DEPRECATED and will be removed in some newer version of Watir! Use Watir::Wait and Watir::ElementExtensions methods instead!"
  @timeout = timeout
  @polling_interval = polling_interval
  @timer = TimeKeeper.new
end

Instance Attribute Details

#polling_intervalObject

How long to wait between each iteration through the wait_until loop. In seconds.



35
36
37
# File 'lib/watir/waiter.rb', line 35

def polling_interval
  @polling_interval
end

#timeoutObject

Timeout for wait_until.



38
39
40
# File 'lib/watir/waiter.rb', line 38

def timeout
  @timeout
end

#timerObject

This is an interface to a TimeKeeper which proxies calls to “sleep” and “Time.now”. Useful for unit testing Waiter.



31
32
33
# File 'lib/watir/waiter.rb', line 31

def timer
  @timer
end

Class Method Details

.wait_until(timeout = @@default_timeout, polling_interval = @@default_polling_interval) ⇒ Object

IDEA: wait_until: remove defaults from Waiter.wait_until



83
84
85
86
87
# File 'lib/watir/waiter.rb', line 83

def self.wait_until(timeout=@@default_timeout,
                    polling_interval=@@default_polling_interval)
  waiter = new(timeout, polling_interval)
  waiter.wait_until { yield }
end

Instance Method Details

#wait_untilObject

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 ‘hello’

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



61
62
63
64
65
66
67
68
69
70
# File 'lib/watir/waiter.rb', line 61

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