2012-08-09 20:38:07 -04:00
|
|
|
require 'forwardable'
|
|
|
|
|
2012-06-08 10:47:01 -04:00
|
|
|
module Capybara
|
2013-01-28 18:58:15 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# A {Capybara::Result} represents a collection of {Capybara::Element} on the page. It is possible to interact with this
|
|
|
|
# collection similar to an Array because it implements Enumerable and offers the following Array methods through delegation:
|
|
|
|
#
|
|
|
|
# * []
|
|
|
|
# * each()
|
|
|
|
# * at()
|
|
|
|
# * size()
|
|
|
|
# * count()
|
|
|
|
# * length()
|
|
|
|
# * first()
|
|
|
|
# * last()
|
|
|
|
# * empty?()
|
|
|
|
#
|
|
|
|
# @see Capybara::Element
|
|
|
|
#
|
2012-06-08 10:47:01 -04:00
|
|
|
class Result
|
|
|
|
include Enumerable
|
2012-08-09 20:38:07 -04:00
|
|
|
extend Forwardable
|
2012-06-08 10:47:01 -04:00
|
|
|
|
|
|
|
def initialize(elements, query)
|
2012-07-09 08:47:11 -04:00
|
|
|
@elements = elements
|
|
|
|
@result = elements.select { |node| query.matches_filters?(node) }
|
|
|
|
@rest = @elements - @result
|
2012-06-08 10:47:01 -04:00
|
|
|
@query = query
|
|
|
|
end
|
|
|
|
|
2013-10-29 16:43:11 -04:00
|
|
|
def_delegators :@result, :each, :[], :at, :size, :count, :length,
|
|
|
|
:first, :last, :values_at, :empty?, :inspect, :sample, :index
|
2012-06-08 10:47:01 -04:00
|
|
|
|
|
|
|
def matches_count?
|
2013-03-17 10:48:04 -04:00
|
|
|
Capybara::Helpers.matches_count?(@result.size, @query.options)
|
2012-06-08 10:47:01 -04:00
|
|
|
end
|
|
|
|
|
2012-07-09 07:21:44 -04:00
|
|
|
def failure_message
|
2013-03-17 10:48:04 -04:00
|
|
|
message = Capybara::Helpers.failure_message(@query.description, @query.options)
|
2012-07-09 08:47:11 -04:00
|
|
|
if count > 0
|
2013-03-17 10:48:04 -04:00
|
|
|
message << ", found #{count} #{Capybara::Helpers.declension("match", "matches", count)}: " << @result.map(&:text).map(&:inspect).join(", ")
|
2012-07-09 08:47:11 -04:00
|
|
|
else
|
|
|
|
message << " but there were no matches"
|
|
|
|
end
|
|
|
|
unless @rest.empty?
|
|
|
|
elements = @rest.map(&:text).map(&:inspect).join(", ")
|
|
|
|
message << ". Also found " << elements << ", which matched the selector but not all filters."
|
|
|
|
end
|
|
|
|
message
|
2012-06-08 10:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def negative_failure_message
|
2014-02-16 12:13:58 -05:00
|
|
|
failure_message.sub(/(to find)/, 'not \1')
|
2012-06-08 10:47:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|