Skip to content

Commit

Permalink
Selectors can no longer have custom failure messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Nicklas and Nicklas Ramhöj committed Jun 8, 2012
1 parent 6cb1b12 commit ad833a1
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 86 deletions.
10 changes: 4 additions & 6 deletions lib/capybara/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,22 @@ def initialize(node, *args)
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
"expected #{description} not to return anything"
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
Expand Down
30 changes: 12 additions & 18 deletions lib/capybara/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def match(&block)
@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)
Expand Down Expand Up @@ -88,45 +88,45 @@ def filter(name, &block)
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
Expand All @@ -139,19 +139,13 @@ def filter(name, &block)
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
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/attach_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

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)
Expand Down
6 changes: 3 additions & 3 deletions lib/capybara/spec/session/check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
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
Expand Down Expand Up @@ -58,7 +58,7 @@

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)
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/choose_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

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)
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/click_button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
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)
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/click_link_or_button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
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)
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/click_link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

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)
Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/spec/session/fill_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
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)
Expand All @@ -105,7 +105,7 @@

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)
Expand Down
28 changes: 0 additions & 28 deletions lib/capybara/spec/session/find_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,6 @@
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
Expand Down Expand Up @@ -139,12 +117,6 @@
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
Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/spec/session/select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

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)
Expand All @@ -66,7 +66,7 @@

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)
Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/spec/session/unselect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

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)
Expand All @@ -57,7 +57,7 @@

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)
Expand Down
20 changes: 0 additions & 20 deletions spec/rspec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,6 @@
"<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
Expand Down Expand Up @@ -214,16 +204,6 @@
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
Expand Down

0 comments on commit ad833a1

Please sign in to comment.