module Watir::Wait

Constants

INTERVAL

Attributes

timer[W]

@!attribute timer

Access Watir timer implementation in use.
@see Timer
@return [#wait]

Public Class Methods

timer() click to toggle source
# File lib/watir-webdriver/wait.rb, line 23
def timer
  @timer ||= Timer.new
end
until(timeout = nil, message = nil) { |self| ... } click to toggle source

Waits until the block evaluates to true or times out.

@example

Watir::Wait.until { browser.text_field(name: "new_user_first_name").visible? }

@param [Fixnum] timeout How long to wait in seconds @param [String] message Message to raise if timeout is exceeded @raise [TimeoutError] if timeout is exceeded

# File lib/watir-webdriver/wait.rb, line 38
def until(timeout = nil, message = nil)
  timeout ||= Watir.default_timeout
  run_with_timer(timeout) do
    result = yield(self)
    return result if result
  end
  raise TimeoutError, message_for(timeout, message)
end
while(timeout = nil, message = nil) { |self| ... } click to toggle source

Wait while the block evaluates to true or times out.

@example

Watir::Wait.while { browser.text_field(name: "abrakadbra").present? }

@param [Fixnum] timeout How long to wait in seconds @param [String] message Message to raise if timeout is exceeded @raise [TimeoutError] if timeout is exceeded

# File lib/watir-webdriver/wait.rb, line 58
def while(timeout = nil, message = nil)
  timeout ||= Watir.default_timeout
  run_with_timer(timeout) { return unless yield(self) }
  raise TimeoutError, message_for(timeout, message)
end

Private Class Methods

message_for(timeout, message) click to toggle source
# File lib/watir-webdriver/wait.rb, line 66
def message_for(timeout, message)
  err = "timed out after #{timeout} seconds"
  err << ", #{message}" if message

  err
end
run_with_timer(timeout, &block) click to toggle source
# File lib/watir-webdriver/wait.rb, line 73
def run_with_timer(timeout, &block)
  if timeout == 0
    block.call
  else
    timer.wait(timeout) do
      block.call
      sleep INTERVAL
    end
  end
end