Module: Capybara::AsyncRunner::WaitHelper

Extended by:
WaitHelper
Included in:
WaitHelper
Defined in:
lib/capybara/async_runner/wait_helper.rb

Overview

This module allows to run a code periodically

and check for its returning value

Instance Method Summary collapse

Instance Method Details

#wait_until(timeout) { ... } ⇒ Object

Calls provided block every 100ms

and stops when it returns false

Examples:

current_time = Time.now
Capybara::AsyncRunner::WaitHelper.wait_until(3) do
  Time.now - current_time > 2
end

# 2 seconds later ...
# => true

current_time = Time.now
Capybara::AsyncRunner::WaitHelper.wait_until(3) do
  Time.now - current_time > 10
end

# 3 seconds later (after timeout)
# => false

Parameters:

  • timeout (Fixnum)

Yields:

  • block for execution



30
31
32
33
34
35
36
37
38
39
# File 'lib/capybara/async_runner/wait_helper.rb', line 30

def wait_until(timeout, &block)
  begin
    Timeout.timeout(timeout) do
      sleep(0.1) until value = block.call
      value
    end
  rescue TimeoutError
    false
  end
end