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

  • Element

Instance Method Summary collapse

Constructor Details

#initialize(elements, query) ⇒ Result

Returns a new instance of Result.



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

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



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

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)


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

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

#negative_failure_messageObject



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

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