From 89cd9803b620f5c85299e2ba0be03932bf7ad1c6 Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Fri, 1 Jan 2010 19:36:45 +0100 Subject: [PATCH] has_*? methods now autowait for content --- lib/capybara/session.rb | 54 +++++++++++------- spec/public/test.js | 1 + spec/session_with_javascript_support_spec.rb | 58 ++++++++++++++++++-- 3 files changed, 88 insertions(+), 25 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 46ed9f35..9fabb5c4 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -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) diff --git a/spec/public/test.js b/spec/public/test.js index 1a497273..df456c55 100644 --- a/spec/public/test.js +++ b/spec/public/test.js @@ -14,6 +14,7 @@ $(function() { $(link).after('Has been clicked'); $(link).after(''); $(link).after(''); + $('#change').remove(); }, 500); return false; }); diff --git a/spec/session_with_javascript_support_spec.rb b/spec/session_with_javascript_support_spec.rb index d46ecaac..c0d2095a 100644 --- a/spec/session_with_javascript_support_spec.rb +++ b/spec/session_with_javascript_support_spec.rb @@ -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