Class: Capybara::Result

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/capybara/result.rb

Overview

A Result represents a collection of 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 Also:

Instance Method Summary collapse

Constructor Details

#initialize(elements, query) ⇒ Result

Returns a new instance of Result.



26
27
28
29
30
31
# File 'lib/capybara/result.rb', line 26

def initialize(elements, query)
  @elements = elements
  @result = elements.select { |node| query.matches_filters?(node) }
  @rest = @elements - @result
  @query = query
end

Instance Method Details

#failure_messageObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/capybara/result.rb', line 40

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

#matches_count?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/capybara/result.rb', line 36

def matches_count?
  Capybara::Helpers.matches_count?(@result.size, @query.options)
end

#negative_failure_messageObject



54
55
56
# File 'lib/capybara/result.rb', line 54

def negative_failure_message
  failure_message.sub(/(to find)/, 'not \1')
end