1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/result.rb

58 lines
1.6 KiB
Ruby
Raw Normal View History

require 'forwardable'
module Capybara
##
# 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
#
class Result
include Enumerable
extend Forwardable
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
@query = query
end
def_delegators :@result, :each, :[], :at, :size, :count, :length,
:first, :last, :values_at, :empty?, :inspect, :sample, :index
def matches_count?
2013-03-17 10:48:04 -04:00
Capybara::Helpers.matches_count?(@result.size, @query.options)
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
end
def negative_failure_message
failure_message.sub(/(to find)/, 'not \1')
end
end
end