Selectors can no longer have custom failure messages

This commit is contained in:
Jonas Nicklas and Nicklas Ramhöj 2012-06-08 15:53:25 +02:00
parent 6cb1b124d5
commit ad833a1e13
13 changed files with 30 additions and 86 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -168,16 +168,6 @@ describe Capybara::RSpecMatchers do
"<h1>Text</h1>".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
'<h1 id="monkey_paul">Monkey John</h1>'.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