From d18787de65b825a337d2cb3924477e838d7d4dbd Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Sun, 13 Feb 2011 17:04:13 +0100 Subject: [PATCH] Added have_button matchers --- lib/capybara/rspec/matchers.rb | 8 ++++++++ spec/rspec/matchers_spec.rb | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/capybara/rspec/matchers.rb b/lib/capybara/rspec/matchers.rb index 34adb752..565634f9 100644 --- a/lib/capybara/rspec/matchers.rb +++ b/lib/capybara/rspec/matchers.rb @@ -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 diff --git a/spec/rspec/matchers_spec.rb b/spec/rspec/matchers_spec.rb index add98a08..837b4cfc 100644 --- a/spec/rspec/matchers_spec.rb +++ b/spec/rspec/matchers_spec.rb @@ -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 '

Monkey John

'.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) { '' } + + 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"