mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Provides timeout similar to standard library Timeout, but avoids threads
Capybara::WaitUntil - should return result of yield if it returns true value within timeout - should keep trying within timeout - should raise Capybara::TimeoutError if block fails to return true within timeout
This commit is contained in:
parent
5f9365da5c
commit
e41c757394
2 changed files with 51 additions and 0 deletions
23
lib/capybara/wait_until.rb
Normal file
23
lib/capybara/wait_until.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
module Capybara
|
||||
#Provides timeout similar to standard library Timeout, but avoids threads
|
||||
class WaitUntil
|
||||
|
||||
class << self
|
||||
|
||||
def timeout(seconds = 1, &block)
|
||||
start_time = Time.now
|
||||
|
||||
result = nil
|
||||
|
||||
until result
|
||||
return result if result = yield
|
||||
|
||||
if (Time.now - start_time) > seconds
|
||||
raise TimeoutError
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
28
spec/wait_until_spec.rb
Normal file
28
spec/wait_until_spec.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
require File.expand_path('spec_helper', File.dirname(__FILE__))
|
||||
|
||||
require 'capybara'
|
||||
require 'capybara/wait_until'
|
||||
|
||||
module Capybara
|
||||
|
||||
describe WaitUntil do
|
||||
|
||||
it "should return result of yield if it returns true value within timeout" do
|
||||
WaitUntil.timeout { "hello" }.should == "hello"
|
||||
end
|
||||
|
||||
it "should keep trying within timeout" do
|
||||
count = 0
|
||||
WaitUntil.timeout { count += 1; count == 5 ? count : nil }.should == 5
|
||||
end
|
||||
|
||||
it "should raise Capybara::TimeoutError if block fails to return true within timeout" do
|
||||
running do
|
||||
WaitUntil.timeout(0.1) { false }
|
||||
end.should raise_error(::Capybara::TimeoutError)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Add table
Reference in a new issue