Added ambiguous error

This commit is contained in:
Jonas Nicklas 2012-07-09 13:21:44 +02:00
parent 453cec449d
commit b8af344659
3 changed files with 13 additions and 6 deletions

View File

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

View File

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

View File

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