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

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

@ -113,27 +113,33 @@ module Capybara
yield
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={})
results = all(path, options)
wait_conditionally_until do
results = all(path, options)
if options[:count]
results.size == options[:count]
else
results.size > 0
if options[:count]
results.size == options[:count]
else
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

@ -17,7 +17,7 @@ shared_examples_for "session with javascript support" do
@session.locate("//a[contains(.,'Has been clicked')]")[:href].should == '#'
end
end
describe '#wait_until' do
before do
@default_timeout = Capybara.default_wait_time
@ -45,7 +45,7 @@ shared_examples_for "session with javascript support" do
end
end.should raise_error(::Capybara::TimeoutError)
end
it "should accept custom timeout in seconds" do
start = Time.now
Capybara.default_wait_time = 5
@ -80,7 +80,7 @@ shared_examples_for "session with javascript support" do
@session.click_link('Has been clicked')
end
end
describe '#click_button' do
it "should wait for asynchronous load" do
@session.visit('/with_js')
@ -88,7 +88,7 @@ shared_examples_for "session with javascript support" do
@session.click_button('New Here')
end
end
describe '#fill_in' do
it "should wait for asynchronous load" do
@session.visit('/with_js')
@ -96,5 +96,53 @@ shared_examples_for "session with javascript support" do
@session.fill_in('new_field', :with => 'Testing...')
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