Added have_button matchers

This commit is contained in:
Jonas Nicklas 2011-02-13 17:04:13 +01:00
parent 2c5e1c3e6e
commit d18787de65
2 changed files with 26 additions and 3 deletions

View File

@ -110,5 +110,13 @@ module Capybara
%(expected there to be content #{matcher.locator.inspect} in #{page.text.inspect})
end
end
def have_button(button, options={})
HaveMatcher.new(:button, button, options) do |page, matcher|
buttons = page.all(:xpath, './/button | .//input[(type="submit") or (type="image") or (type="button")]')
labels = buttons.map { |button| %("#{button.text}") }.join(', ')
%(expected there to be a button #{matcher.locator.inspect}, other buttons: #{labels})
end
end
end
end

View File

@ -140,7 +140,7 @@ describe Capybara::RSpecMatchers do
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| node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') }
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)
@ -186,7 +186,7 @@ describe Capybara::RSpecMatchers do
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| node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') }
failure_message { |node, selector| node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') }
end
expect do
page.should have_selector(:monkey, 14)
@ -265,8 +265,23 @@ describe Capybara::RSpecMatchers do
end
end
end
describe "have_link matcher"
describe "have_button matcher"
describe "have_button matcher" do
let(:html) { '<button>A button</button><button>Another button</button>' }
it "passes if there is such a button" do
html.should have_button('A button')
end
it "fails if there is no such button" do
expect do
html.should have_button('No such Button')
end.to raise_error(/expected there to be a button "No such Button", other buttons: "A button", "Another button"/)
end
end
describe "have_no_button matcher"
describe "have_field matcher"
describe "have_checked_field matcher"