Sha256: 23d142866e5034a48f4bb6a1382529aa2dc514c2bd642f7cddc214fe2dd74673

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

require 'forwardable'

module Capybara

  ##
  # A {Capybara::Result} represents a collection of {Capybara::Node::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::Node::Element
  #
  class Result
    include Enumerable
    extend Forwardable

    def initialize(elements, query)
      @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?
      Capybara::Helpers.matches_count?(@result.size, @query.options)
    end

    def failure_message
      message = Capybara::Helpers.failure_message(@query.description, @query.options)
      if count > 0
        message << ", found #{count} #{Capybara::Helpers.declension("match", "matches", count)}: " << @result.map(&:text).map(&:inspect).join(", ")
      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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capybara-2.6.2 lib/capybara/result.rb
capybara-2.6.1 lib/capybara/result.rb
capybara-2.6.0 lib/capybara/result.rb
capybara-2.5.0 lib/capybara/result.rb