has_*? methods now autowait for content

This commit is contained in:
Jonas Nicklas 2010-01-01 19:36:45 +01:00
parent c95d351982
commit 89cd9803b6
3 changed files with 88 additions and 25 deletions

View File

@ -114,15 +114,8 @@ module Capybara
end
end
def has_content?(content)
has_xpath?(XPath.content(content).to_s)
end
def has_no_content?(content)
!has_content?(content)
end
def has_xpath?(path, options={})
wait_conditionally_until do
results = all(path, options)
if options[:count]
@ -131,9 +124,22 @@ module Capybara
results.size > 0
end
end
rescue Capybara::TimeoutError
return false
end
def has_no_xpath?(path, options={})
!has_xpath?(path, options)
wait_conditionally_until do
results = all(path, options)
if options[:count]
results.size != options[:count]
else
results.empty?
end
end
rescue Capybara::TimeoutError
return false
end
def has_css?(path, options={})
@ -141,7 +147,15 @@ module Capybara
end
def has_no_css?(path, options={})
!has_css?(path, options)
has_no_xpath?(XPath.from_css(path), options)
end
def has_content?(content)
has_xpath?(XPath.content(content))
end
def has_no_content?(content)
has_no_xpath?(XPath.content(content))
end
def save_and_open_page
@ -151,11 +165,7 @@ module Capybara
#return node identified by locator or raise ElementNotFound(using desc)
def locate(locator, fail_msg = nil)
if driver.wait?
node = wait_until { find(locator) }
else
node = find(locator)
end
node = wait_conditionally_until { find(locator) }
ensure
raise Capybara::ElementNotFound, fail_msg || "Unable to locate '#{locator}'" unless node
return node
@ -171,6 +181,10 @@ module Capybara
private
def wait_conditionally_until
if driver.wait? then wait_until { yield } else yield end
end
def all_unfiltered(locator)
XPath.wrap(locator).scope(current_scope).paths.map do |path|
driver.find(path)

View File

@ -14,6 +14,7 @@ $(function() {
$(link).after('<a href="#">Has been clicked</a>');
$(link).after('<input type="submit" value="New Here">');
$(link).after('<input type="text" id="new_field">');
$('#change').remove();
}, 500);
return false;
});

View File

@ -97,4 +97,52 @@ shared_examples_for "session with javascript support" do
end
end
describe '#has_xpath?' do
it "should wait for content to appear" do
@session.visit('/with_js')
@session.click_link('Click me')
@session.should have_xpath("//input[@type='submit' and @value='New Here']")
end
end
describe '#has_no_xpath?' do
it "should wait for content to disappear" do
@session.visit('/with_js')
@session.click_link('Click me')
@session.should have_no_xpath("//p[@id='change']")
end
end
describe '#has_css?' do
it "should wait for content to appear" do
@session.visit('/with_js')
@session.click_link('Click me')
@session.should have_css("input[type='submit'][value='New Here']")
end
end
describe '#has_no_xpath?' do
it "should wait for content to disappear" do
@session.visit('/with_js')
@session.click_link('Click me')
@session.should have_no_css("p#change")
end
end
describe '#has_content?' do
it "should wait for content to appear" do
@session.visit('/with_js')
@session.click_link('Click me')
@session.should have_content("Has been clicked")
end
end
describe '#has_no_content?' do
it "should wait for content to disappear" do
@session.visit('/with_js')
@session.click_link('Click me')
@session.should have_no_content("I changed it")
end
end
end