From b8af344659e5fff3066a149a535fe6b71e7d3cef Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Mon, 9 Jul 2012 13:21:44 +0200 Subject: [PATCH] Added ambiguous error --- lib/capybara.rb | 1 + lib/capybara/result.rb | 16 +++++++++++----- lib/capybara/spec/session/find_spec.rb | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/capybara.rb b/lib/capybara.rb index e21e9826..c8dd1ebf 100644 --- a/lib/capybara.rb +++ b/lib/capybara.rb @@ -7,6 +7,7 @@ module Capybara class DriverNotFoundError < CapybaraError; end class FrozenInTime < CapybaraError; end class ElementNotFound < CapybaraError; end + class Ambiguous < ElementNotFound; end class ExpectationNotMet < ElementNotFound; end class FileNotFound < CapybaraError; end class UnselectNotAllowed < CapybaraError; end diff --git a/lib/capybara/result.rb b/lib/capybara/result.rb index d0de4136..738b87a3 100644 --- a/lib/capybara/result.rb +++ b/lib/capybara/result.rb @@ -21,14 +21,20 @@ module Capybara end def find! - raise Capybara::ElementNotFound, failure_message(true) if @filtered_elements.count != 1 + raise find_error if @filtered_elements.count != 1 @filtered_elements.first end - def failure_message(find=false) - if find - "Unable to find #{@query.description}" - elsif @query.options[:count] + def find_error + if @filtered_elements.count == 0 + Capybara::ElementNotFound.new("Unable to find #{@query.description}") + elsif @filtered_elements.count > 1 + Capybara::Ambiguous.new("Ambiguous match, found #{@filtered_elements.count} elements matching #{@query.description}") + end + end + + def failure_message + if @query.options[:count] "expected #{@query.description} to be returned #{@query.options[:count]} times" else "expected #{@query.description} to return something" diff --git a/lib/capybara/spec/session/find_spec.rb b/lib/capybara/spec/session/find_spec.rb index 72cfa673..f04c6c8b 100644 --- a/lib/capybara/spec/session/find_spec.rb +++ b/lib/capybara/spec/session/find_spec.rb @@ -19,7 +19,7 @@ shared_examples_for "find" do end it "should raise an error if there are multiple matches" do - expect { @session.find('//a') }.to raise_error(Capybara::ElementNotFound) + expect { @session.find('//a') }.to raise_error(Capybara::Ambiguous) end describe 'the returned node' do