diff --git a/lib/capybara/query.rb b/lib/capybara/query.rb index 3a7fd0f3..e591c815 100644 --- a/lib/capybara/query.rb +++ b/lib/capybara/query.rb @@ -22,14 +22,11 @@ module Capybara end def failure_message - message = selector.failure_message.call(node, self) if selector.failure_message - message ||= options[:message] if find - message ||= "Unable to find #{description}" + "Unable to find #{description}" else - message ||= "expected #{description} to return something" + "expected #{description} to return something" end - message end def negative_failure_message @@ -37,9 +34,10 @@ module Capybara end def name; selector.name; end + def label; selector.label or selector.name; end def description - @description = "#{name} #{locator.inspect}" + @description = "#{label} #{locator.inspect}" @description << " with text #{options[:text].inspect}" if options[:text] @description end diff --git a/lib/capybara/selector.rb b/lib/capybara/selector.rb index b43c0859..d8efd88b 100644 --- a/lib/capybara/selector.rb +++ b/lib/capybara/selector.rb @@ -43,9 +43,9 @@ module Capybara @match end - def failure_message(&block) - @failure_message = block if block - @failure_message + def label(label=nil) + @label = label if label + @label end def call(locator) @@ -88,45 +88,45 @@ Capybara.add_selector(:fieldset) do end Capybara.add_selector(:link_or_button) do + label "link or button" xpath { |locator| XPath::HTML.link_or_button(locator) } - failure_message { |node, selector| "no link or button '#{selector.locator}' found" } end Capybara.add_selector(:link) do + label "link with title, id or text" xpath { |locator| XPath::HTML.link(locator) } - failure_message { |node, selector| "no link with title, id or text '#{selector.locator}' found" } filter(:href) do |node, href| node.first(:xpath, XPath.axis(:self)[XPath.attr(:href).equals(href)]) end end Capybara.add_selector(:button) do + label "button with value or id or text" xpath { |locator| XPath::HTML.button(locator) } - failure_message { |node, selector| "no button with value or id or text '#{selector.locator}' found" } end Capybara.add_selector(:fillable_field) do + label "text field, text area or password field with id, name, or label" xpath { |locator| XPath::HTML.fillable_field(locator) } - failure_message { |node, selector| "no text field, text area or password field with id, name, or label '#{selector.locator}' found" } end Capybara.add_selector(:radio_button) do + label "radio button with id, name, or label" xpath { |locator| XPath::HTML.radio_button(locator) } - failure_message { |node, selector| "no radio button with id, name, or label '#{selector.locator}' found" } filter(:checked) { |node, value| not(value ^ node.checked?) } filter(:unchecked) { |node, value| (value ^ node.checked?) } end Capybara.add_selector(:checkbox) do + label "checkbox with id, name, or label" xpath { |locator| XPath::HTML.checkbox(locator) } - failure_message { |node, selector| "no checkbox with id, name, or label '#{selector.locator}' found" } filter(:checked) { |node, value| not(value ^ node.checked?) } filter(:unchecked) { |node, value| (value ^ node.checked?) } end Capybara.add_selector(:select) do + label "select box with id, name, or label" xpath { |locator| XPath::HTML.select(locator) } - failure_message { |node, selector| "no select box with id, name, or label '#{selector.locator}' found" } filter(:options) do |node, options| actual = node.all(:xpath, './/option').map { |option| option.text } options.sort == actual.sort @@ -139,19 +139,13 @@ Capybara.add_selector(:select) do end Capybara.add_selector(:option) do + label "option with text" xpath { |locator| XPath::HTML.option(locator) } - failure_message do |node, selector| - "no option with text '#{selector.locator}'".tap do |message| - if node.is_a?(Capybara::Node::Element) and node.tag_name == 'select' - message << " in the select box" - end - end - end end Capybara.add_selector(:file_field) do xpath { |locator| XPath::HTML.file_field(locator) } - failure_message { |node, selector| "no file field with id, name, or label '#{selector.locator}' found" } + label "file field with id, name, or label" end Capybara.add_selector(:content) do diff --git a/lib/capybara/spec/session/attach_file_spec.rb b/lib/capybara/spec/session/attach_file_spec.rb index d1de9482..a26936e2 100644 --- a/lib/capybara/spec/session/attach_file_spec.rb +++ b/lib/capybara/spec/session/attach_file_spec.rb @@ -60,7 +60,7 @@ shared_examples_for "attach_file" do context "with a locator that doesn't exist" do it "should raise an error" do - msg = "no file field with id, name, or label 'does not exist' found" + msg = "Unable to find file field with id, name, or label \"does not exist\"" running do @session.attach_file('does not exist', @test_file_path) end.should raise_error(Capybara::ElementNotFound, msg) diff --git a/lib/capybara/spec/session/check_spec.rb b/lib/capybara/spec/session/check_spec.rb index 25eba138..5ed72a00 100644 --- a/lib/capybara/spec/session/check_spec.rb +++ b/lib/capybara/spec/session/check_spec.rb @@ -4,13 +4,13 @@ shared_examples_for "check" do before do @session.visit('/form') end - + describe "'checked' attribute" do it "should be true if checked" do @session.check("Terms of Use") @session.find(:xpath, "//input[@id='form_terms_of_use']")['checked'].should be_true end - + it "should be false if unchecked" do @session.find(:xpath, "//input[@id='form_terms_of_use']")['checked'].should be_false end @@ -58,7 +58,7 @@ shared_examples_for "check" do context "with a locator that doesn't exist" do it "should raise an error" do - msg = "no checkbox with id, name, or label 'does not exist' found" + msg = "Unable to find checkbox with id, name, or label \"does not exist\"" running do @session.check('does not exist') end.should raise_error(Capybara::ElementNotFound, msg) diff --git a/lib/capybara/spec/session/choose_spec.rb b/lib/capybara/spec/session/choose_spec.rb index d87ef41b..486d2a3e 100644 --- a/lib/capybara/spec/session/choose_spec.rb +++ b/lib/capybara/spec/session/choose_spec.rb @@ -19,7 +19,7 @@ shared_examples_for "choose" do context "with a locator that doesn't exist" do it "should raise an error" do - msg = "no radio button with id, name, or label 'does not exist' found" + msg = "Unable to find radio button with id, name, or label \"does not exist\"" running do @session.choose('does not exist') end.should raise_error(Capybara::ElementNotFound, msg) diff --git a/lib/capybara/spec/session/click_button_spec.rb b/lib/capybara/spec/session/click_button_spec.rb index d06c6b4f..33f2a35e 100644 --- a/lib/capybara/spec/session/click_button_spec.rb +++ b/lib/capybara/spec/session/click_button_spec.rb @@ -246,7 +246,7 @@ shared_examples_for "click_button" do end context "with a locator that doesn't exist" do it "should raise an error" do - msg = "no button with value or id or text 'does not exist' found" + msg = "Unable to find button with value or id or text \"does not exist\"" running do @session.click_button('does not exist') end.should raise_error(Capybara::ElementNotFound, msg) diff --git a/lib/capybara/spec/session/click_link_or_button_spec.rb b/lib/capybara/spec/session/click_link_or_button_spec.rb index 66b7e0a1..020b2654 100644 --- a/lib/capybara/spec/session/click_link_or_button_spec.rb +++ b/lib/capybara/spec/session/click_link_or_button_spec.rb @@ -27,7 +27,7 @@ shared_examples_for "click_link_or_button" do context "with a locator that doesn't exist" do it "should raise an error" do @session.visit('/with_html') - msg = "no link or button 'does not exist' found" + msg = "Unable to find link or button \"does not exist\"" running do @session.click_link_or_button('does not exist') end.should raise_error(Capybara::ElementNotFound, msg) diff --git a/lib/capybara/spec/session/click_link_spec.rb b/lib/capybara/spec/session/click_link_spec.rb index 910871ac..dffd2c2b 100644 --- a/lib/capybara/spec/session/click_link_spec.rb +++ b/lib/capybara/spec/session/click_link_spec.rb @@ -49,7 +49,7 @@ shared_examples_for "click_link" do context "with a locator that doesn't exist" do it "should raise an error" do - msg = "no link with title, id or text 'does not exist' found" + msg = "Unable to find link with title, id or text \"does not exist\"" running do @session.click_link('does not exist') end.should raise_error(Capybara::ElementNotFound, msg) diff --git a/lib/capybara/spec/session/fill_in_spec.rb b/lib/capybara/spec/session/fill_in_spec.rb index 0fc644ce..abeb3749 100644 --- a/lib/capybara/spec/session/fill_in_spec.rb +++ b/lib/capybara/spec/session/fill_in_spec.rb @@ -96,7 +96,7 @@ shared_examples_for "fill_in" do before { Capybara.ignore_hidden_elements = true } after { Capybara.ignore_hidden_elements = false } it "should not find a hidden field" do - msg = "no text field, text area or password field with id, name, or label 'Super Secret' found" + msg = "Unable to find text field, text area or password field with id, name, or label \"Super Secret\"" running do @session.fill_in('Super Secret', :with => '777') end.should raise_error(Capybara::ElementNotFound, msg) @@ -105,7 +105,7 @@ shared_examples_for "fill_in" do context "with a locator that doesn't exist" do it "should raise an error" do - msg = "no text field, text area or password field with id, name, or label 'does not exist' found" + msg = "Unable to find text field, text area or password field with id, name, or label \"does not exist\"" running do @session.fill_in('does not exist', :with => 'Blah blah') end.should raise_error(Capybara::ElementNotFound, msg) diff --git a/lib/capybara/spec/session/find_spec.rb b/lib/capybara/spec/session/find_spec.rb index 3b77fc99..7664829c 100644 --- a/lib/capybara/spec/session/find_spec.rb +++ b/lib/capybara/spec/session/find_spec.rb @@ -89,28 +89,6 @@ shared_examples_for "find" do end end - context "with custom selector with failure_message option" do - it "should raise an error with the failure message if the element is not found" do - Capybara.add_selector(:monkey) do - xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" } - failure_message { |node, selector| node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') } - end - running do - @session.find(:monkey, '14').text.should == 'Monkey Paul' - end.should raise_error(Capybara::ElementNotFound, "Monkey John, Monkey Paul") - end - - it "should pass the selector as the second argument" do - Capybara.add_selector(:monkey) do - xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" } - failure_message { |node, selector| selector.name.to_s + ': ' + selector.locator + ' - ' + node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') } - end - running do - @session.find(:monkey, '14').text.should == 'Monkey Paul' - end.should raise_error(Capybara::ElementNotFound, "monkey: 14 - Monkey John, Monkey Paul") - end - end - context "with custom selector with custom filter" do before do Capybara.add_selector(:monkey) do @@ -139,12 +117,6 @@ shared_examples_for "find" do after { Capybara.default_selector = :xpath } end - it "should raise ElementNotFound with specified fail message if nothing was found" do - running do - @session.find(:xpath, '//div[@id="nosuchthing"]', :message => 'arghh').should be_nil - end.should raise_error(Capybara::ElementNotFound, "arghh") - end - it "should raise ElementNotFound with a useful default message if nothing was found" do running do @session.find(:xpath, '//div[@id="nosuchthing"]').should be_nil diff --git a/lib/capybara/spec/session/select_spec.rb b/lib/capybara/spec/session/select_spec.rb index 4fb3d2a0..a9d3c4ba 100644 --- a/lib/capybara/spec/session/select_spec.rb +++ b/lib/capybara/spec/session/select_spec.rb @@ -57,7 +57,7 @@ shared_examples_for "select" do context "with a locator that doesn't exist" do it "should raise an error" do - msg = "no select box with id, name, or label 'does not exist' found" + msg = "Unable to find select box with id, name, or label \"does not exist\"" running do @session.select('foo', :from => 'does not exist') end.should raise_error(Capybara::ElementNotFound, msg) @@ -66,7 +66,7 @@ shared_examples_for "select" do context "with an option that doesn't exist" do it "should raise an error" do - msg = "no option with text 'Does not Exist' in the select box" + msg = "Unable to find option with text \"Does not Exist\"" running do @session.select('Does not Exist', :from => 'form_locale') end.should raise_error(Capybara::ElementNotFound, msg) diff --git a/lib/capybara/spec/session/unselect_spec.rb b/lib/capybara/spec/session/unselect_spec.rb index f38375e7..b9e84664 100644 --- a/lib/capybara/spec/session/unselect_spec.rb +++ b/lib/capybara/spec/session/unselect_spec.rb @@ -48,7 +48,7 @@ shared_examples_for "unselect" do context "with a locator that doesn't exist" do it "should raise an error" do - msg = "no select box with id, name, or label 'does not exist' found" + msg = "Unable to find select box with id, name, or label \"does not exist\"" running do @session.unselect('foo', :from => 'does not exist') end.should raise_error(Capybara::ElementNotFound, msg) @@ -57,7 +57,7 @@ shared_examples_for "unselect" do context "with an option that doesn't exist" do it "should raise an error" do - msg = "no option with text 'Does not Exist' in the select box" + msg = "Unable to find option with text \"Does not Exist\"" running do @session.unselect('Does not Exist', :from => 'form_underwear') end.should raise_error(Capybara::ElementNotFound, msg) diff --git a/spec/rspec/matchers_spec.rb b/spec/rspec/matchers_spec.rb index ccc0acea..ce87f242 100644 --- a/spec/rspec/matchers_spec.rb +++ b/spec/rspec/matchers_spec.rb @@ -168,16 +168,6 @@ describe Capybara::RSpecMatchers do "

Text

".should have_selector('//h2') end.to raise_error(%r(expected xpath "//h2" 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}]" } - failure_message { |node, selector| node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') } - end - expect do - '

Monkey John

'.should have_selector(:monkey, 14) - end.to raise_error("Monkey John") - end end context "with should_not" do @@ -214,16 +204,6 @@ describe Capybara::RSpecMatchers 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}]" } - failure_message { |node, selector| node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') } - end - expect do - page.should have_selector(:monkey, 14) - end.to raise_error("Monkey John, Monkey Paul") - end end context "with should_not" do