1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Session#wait_until

This commit is contained in:
Lenny Marks 2009-12-22 18:14:04 -05:00
parent 4c29fd9581
commit 03e676b492
7 changed files with 64 additions and 10 deletions

View file

@ -11,14 +11,14 @@ module Capybara
class << self
attr_accessor :debug, :asset_root, :app_host
attr_writer :default_selector, :default_condition_timeout
attr_writer :default_selector, :default_wait_timeout
def default_selector
@default_selector ||= :xpath
end
def default_condition_timeout
@default_condition_timeout ||= 10
def default_wait_timeout
@default_wait_timeout ||= 10
end
def log(message)

View file

@ -167,10 +167,10 @@ module Capybara
end
nil
end
def wait_for_condition(script)
begin
Timeout.timeout(Capybara.default_condition_timeout) do
Timeout.timeout(Capybara.default_wait_timeout) do
result = false
until result
result = evaluate_script(script)
@ -178,10 +178,24 @@ module Capybara
return result
end
rescue Timeout::Error
return false
return false
end
end
def wait_until(timeout = Capybara.default_wait_timeout, &block)
return yield unless driver.wait?
returned = nil
Timeout.timeout(timeout) do
until returned = yield
sleep(0.1)
end
end
returned
end
def find_field(locator)
find(XPath.field(locator))
end

View file

@ -12,7 +12,7 @@ module AllSpec
end
it "should return an empty array when nothing was found" do
@session.all('//div').should be_empty
@session.all('//div[@id="nosuchthing"]').should be_empty
end
it "should accept an XPath instance" do

View file

@ -11,7 +11,7 @@ module FindSpec
end
it "should return nil when nothing was found" do
@session.find('//div').should be_nil
@session.find('//div[@id="nosuchthing"]').should be_nil
end
it "should accept an XPath instance and respect the order of paths" do

View file

@ -11,7 +11,7 @@ module WaitForSpec
end
it "should return nil when nothing was found" do
@session.wait_for('//div').should be_nil
@session.wait_for('//div[@id="nosuchthing"]').should be_nil
end
it "should accept an XPath instance and respect the order of paths" do

View file

@ -18,6 +18,30 @@ shared_examples_for "session with javascript support" do
end
end
describe '#wait_until' do
it "should wait for block to return true" do
@session.visit('/with_js')
@session.select('My Waiting Option', :from => 'waiter')
@session.evaluate_script('activeRequests == 1').should be_true
@session.wait_until do
@session.evaluate_script('activeRequests == 0')
end
@session.evaluate_script('activeRequests == 0').should be_true
end
it "should timeout if block doesn't return true within timeout" do
@session.visit('/with_html')
Proc.new do
@session.wait_until(1) do
@session.find('//div[@id="nosuchthing"]')
end
end.should raise_error(Timeout::Error)
end
end
describe '#wait_for_condition' do
it "should wait for condition to be true" do
@session.visit('/with_js')
@ -26,7 +50,7 @@ shared_examples_for "session with javascript support" do
@session.wait_for_condition('activeRequests == 0').should be_true
@session.evaluate_script('activeRequests == 0').should be_true
end
it "should timeout" do
@session.visit('/with_js')
@session.select('Timeout', :from => 'timeout')

View file

@ -11,5 +11,21 @@ shared_examples_for "session without javascript support" do
}.should raise_error(Capybara::NotSupportedByDriverError)
end
end
describe '#wait_until' do
it "should not wait for asynchronous load" do
@session.visit('/with_html')
before_time = Time.new
@session.wait_until(2) do
@session.find('//div[@id="nosuchthing"]')
end.should be_nil
(Time.now - before_time).should be < 2
end
end
end