diff --git a/lib/capybara/rspec_matchers.rb b/lib/capybara/rspec_matchers.rb index 0a9b07d8..541aae37 100644 --- a/lib/capybara/rspec_matchers.rb +++ b/lib/capybara/rspec_matchers.rb @@ -19,12 +19,18 @@ module Capybara if normalized[:selector].failure_message normalized[:selector].failure_message.call(@actual) else - "expected #{normalized[:selector].name} #{normalized[:locator].inspect} to return something" + "expected #{selector_name} to return something" end end def failure_message_for_should_not - "expected #{normalized[:selector].name} #{normalized[:locator].inspect} not to return anything" + "expected #{selector_name} not to return anything" + end + + def selector_name + name = "#{normalized[:selector].name} #{normalized[:locator].inspect}" + name << " with text #{normalized[:options][:text].inspect}" if normalized[:options][:text] + name end def wrap(actual) diff --git a/lib/capybara/selector.rb b/lib/capybara/selector.rb index a5db8dfc..7e541d9c 100644 --- a/lib/capybara/selector.rb +++ b/lib/capybara/selector.rb @@ -15,15 +15,16 @@ module Capybara all.delete(name.to_sym) end - def normalize(name_or_locator, locator=nil) + def normalize(*args) result = {} + result[:options] = if args.last.is_a?(Hash) then args.pop else {} end - if locator - result[:selector] = all[name_or_locator] - result[:locator] = locator + if args[1] + result[:selector] = all[args[0]] + result[:locator] = args[1] else - result[:selector] = all.values.find { |s| s.match?(name_or_locator) } - result[:locator] = name_or_locator + result[:selector] = all.values.find { |s| s.match?(args[0]) } + result[:locator] = args[0] end result[:selector] ||= all[Capybara.default_selector] diff --git a/spec/rspec_matchers_spec.rb b/spec/rspec_matchers_spec.rb index 9de49d5d..bc9c7c43 100644 --- a/spec/rspec_matchers_spec.rb +++ b/spec/rspec_matchers_spec.rb @@ -168,7 +168,7 @@ describe Capybara::RSpecMatchers do context "with should" do it "passes if has_css? returns true" do - page.should have_selector('//h1') + page.should have_selector('//h1', :text => 'test') end it "fails if has_css? returns false" do @@ -177,6 +177,12 @@ describe Capybara::RSpecMatchers do end.to raise_error(%r(expected xpath "//h1\[@id='doesnotexist'\]" to return something)) end + it "includes text in error message" do + expect do + page.should have_selector("//h1", :text => 'wrong text') + end.to raise_error(%r(expected xpath "//h1" with text "wrong text" to return something)) + end + it "fails with the selector's failure_message if set" do Capybara.add_selector(:monkey) do xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" } @@ -195,8 +201,8 @@ describe Capybara::RSpecMatchers do it "fails if has_no_css? returns false" do expect do - page.should_not have_selector(:css, 'h1') - end.to raise_error(%r(expected css "h1" not to return anything)) + page.should_not have_selector(:css, 'h1', :text => 'test') + end.to raise_error(%r(expected css "h1" with text "test" not to return anything)) end end end