Class: Selenium::WebDriver::Wait

Inherits:
Object
  • Object
show all
Defined in:
build/rb/lib/selenium/webdriver/common/wait.rb

Constant Summary

DEFAULT_TIMEOUT =
5
DEFAULT_INTERVAL =
0.2

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Wait

Create a new Wait instance

Parameters:

  • opts (Hash) (defaults to: {})

    Options for this instance

Options Hash (opts):

  • :timeout (Numeric) — default: 5

    Seconds to wait before timing out.

  • :interval (Numeric) — default: 0.2

    Seconds to sleep between polls.

  • :message (String)

    Exception mesage if timed out.

  • :ignore (Array, Exception)

    Exceptions to ignore while polling (default: Error::NoSuchElementError)



34
35
36
37
38
39
# File 'build/rb/lib/selenium/webdriver/common/wait.rb', line 34

def initialize(opts = {})
  @timeout  = opts.fetch(:timeout, DEFAULT_TIMEOUT)
  @interval = opts.fetch(:interval, DEFAULT_INTERVAL)
  @message  = opts[:message]
  @ignored  = Array(opts[:ignore] || Error::NoSuchElementError)
end

Instance Method Details

#untilObject

Wait until the given block returns a true value.

Returns:

  • (Object)

    the result of the block

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'build/rb/lib/selenium/webdriver/common/wait.rb', line 48

def until
  end_time = Time.now + @timeout
  last_error = nil

  until Time.now > end_time
    begin
      result = yield
      return result if result
    rescue *@ignored => last_error
      # swallowed
    end

    sleep @interval
  end

  msg = if @message
          @message.dup
        else
          "timed out after #{@timeout} seconds"
        end

  msg << " (#{last_error.message})" if last_error

  raise Error::TimeOutError, msg
end