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/wait.rb, line 19
def timer
  @timer ||= Timer.new
end
until(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, object: nil) { |object| ... } 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 [Integer] timeout How long to wait in seconds @param [String] message Message to raise if timeout is exceeded @param [Object, NilClass] object Object to evaluate block against @raise [TimeoutError] if timeout is exceeded

# File lib/watir/wait.rb, line 35
def until(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, object: nil)
  if depr_message || depr_timeout
    Watir.logger.deprecate 'Using arguments for Wait#until', 'keywords', ids: %i[until timeout_arguments]
    timeout = depr_timeout
    message = depr_message
  end
  timeout ||= Watir.default_timeout
  run_with_timer(timeout, interval) do
    result = yield(object)
    return result if result
  end
  raise TimeoutError, message_for(timeout, object, message)
end
while(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, object: nil) { |object| ... } 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 [Integer] timeout How long to wait in seconds @param [String] message Message to raise if timeout is exceeded @param [Object, NilClass] object Object to evaluate block against @raise [TimeoutError] if timeout is exceeded

# File lib/watir/wait.rb, line 61
def while(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, object: nil)
  if depr_message || depr_timeout
    Watir.logger.deprecate 'Using arguments for Wait#while', 'keywords', ids: %i[while timeout_arguments]
    timeout = depr_timeout
    message = depr_message
  end
  timeout ||= Watir.default_timeout
  run_with_timer(timeout, interval) { return unless yield(object) }
  raise TimeoutError, message_for(timeout, object, message)
end

Private Class Methods

message_for(timeout, object, message) click to toggle source
# File lib/watir/wait.rb, line 74
def message_for(timeout, object, message)
  message = message.call(object) if message.is_a?(Proc)
  err = "timed out after #{timeout} seconds"
  err << ", #{message}" if message

  err
end
run_with_timer(timeout, interval) { || ... } click to toggle source
# File lib/watir/wait.rb, line 82
def run_with_timer(timeout, interval)
  if timeout.zero?
    yield
  else
    timer.wait(timeout) do
      yield
      sleep interval || INTERVAL
    end
  end
end