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

Allow :wait as an option to find, closes #974

This commit is contained in:
CJ Kihlbom and Jonas Nicklas 2013-02-25 20:34:58 +01:00
parent b64f567386
commit 0c53168899
3 changed files with 39 additions and 6 deletions

View file

@ -19,14 +19,15 @@ module Capybara
# page.find('li', :text => 'Quox').click_link('Delete')
#
# @param (see Capybara::Node::Finders#all)
# @option options [Boolean] match The matching strategy to use.
# @option options [Boolean] match The matching strategy to use.
# @option options [false, Numeric] wait How long to wait for the element to appear.
#
# @return [Capybara::Element] The found element
# @raise [Capybara::ElementNotFound] If the element can't be found before time expires
# @return [Capybara::Element] The found element
# @raise [Capybara::ElementNotFound] If the element can't be found before time expires
#
def find(*args)
synchronize do
query = Capybara::Query.new(*args)
query = Capybara::Query.new(*args)
synchronize(query.wait) do
if query.match == :smart or query.match == :prefer_exact
result = resolve_query(query, true)
result = resolve_query(query, false) if result.size == 0 and not query.exact?

View file

@ -2,7 +2,7 @@ module Capybara
class Query
attr_accessor :selector, :locator, :options, :expression, :find, :negative
VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum, :exact, :match]
VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum, :exact, :match, :wait]
VALID_MATCH = [:first, :smart, :prefer_exact, :one]
def initialize(*args)
@ -83,6 +83,14 @@ module Capybara
end
end
def wait
if options.has_key?(:wait)
@options[:wait] or 0
else
Capybara.default_wait_time
end
end
def exact?
if options.has_key?(:exact)
@options[:exact]

View file

@ -27,6 +27,30 @@ Capybara::SpecHelper.spec '#find' do
@session.find(:css, "a#has-been-clicked").text.should include('Has been clicked')
end
context "with :wait option", :requires => [:js] do
it "should not wait for asynchronous load when `false` given" do
@session.visit('/with_js')
@session.click_link('Click me')
expect do
@session.find(:css, "a#has-been-clicked", :wait => false)
end.to raise_error(Capybara::ElementNotFound)
end
it "should not find element if it appears after given wait duration" do
@session.visit('/with_js')
@session.click_link('Click me')
expect do
@session.find(:css, "a#has-been-clicked", :wait => 0.2)
end.to raise_error(Capybara::ElementNotFound)
end
it "should find element if it appears before given wait duration" do
@session.visit('/with_js')
@session.click_link('Click me')
@session.find(:css, "a#has-been-clicked", :wait => 0.9).text.should include('Has been clicked')
end
end
context "with frozen time", :requires => [:js] do
it "raises an error suggesting that Capybara is stuck in time" do
@session.visit('/with_js')