Class: Selenium::WebDriver::Wait

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

Constant Summary collapse

DEFAULT_TIMEOUT =
5
DEFAULT_INTERVAL =
0.2

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Wait

Create a new Wait 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.


16
17
18
19
20
# File 'lib/selenium/webdriver/common/wait.rb', line 16

def initialize(opts = {})
  @timeout  = opts.fetch(:timeout, DEFAULT_TIMEOUT)
  @interval = opts.fetch(:interval, DEFAULT_INTERVAL)
  @message  = opts[:message]
end

Instance Method Details

#until(&blk) ⇒ Object

Wait until the given block returns a true value.


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/selenium/webdriver/common/wait.rb', line 30

def until(&blk)
  end_time = Time.now + @timeout
  last_error = nil

  until Time.now > end_time
    begin
      result = yield
      return result if result
    rescue Error::NoSuchElementError => last_error
      # swallowed
    end

    sleep @interval
  end


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

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

  raise Error::TimeOutError, msg
end