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?()
  • values_at()
  • sample()

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(elements, query) ⇒ Result

Returns a new instance of Result.



28
29
30
31
32
33
34
35
# File 'lib/capybara/result.rb', line 28

def initialize(elements, query)
  @elements = elements
  @result_cache = []
  @filter_errors = []
  @results_enum = lazy_select_elements { |node| query.matches_filters?(node, @filter_errors) }
  @query = query
  @allow_reload = false
end

Instance Method Details

#[](*args) ⇒ Object Also known as: at



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/capybara/result.rb', line 53

def [](*args)
  idx, length = args
  max_idx = case idx
  when Integer
    if idx.negative?
      nil
    else
      length.nil? ? idx : idx + length - 1
    end
  when Range
    # idx.max is broken with beginless ranges
    # idx.end && idx.max # endless range will have end == nil
    max = idx.end
    max = nil if max&.negative?
    max -= 1 if max && idx.exclude_end?
    max
  end

  if max_idx.nil?
    full_results[*args]
  else
    load_up_to(max_idx + 1)
    @result_cache[*args]
  end
end

#allow_reload!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



140
141
142
143
# File 'lib/capybara/result.rb', line 140

def allow_reload!
  @allow_reload = true
  self
end

#compare_countObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/capybara/result.rb', line 84

def compare_count
  return 0 unless @query

  count, min, max, between = @query.options.values_at(:count, :minimum, :maximum, :between)

  # Only check filters for as many elements as necessary to determine result
  if count && (count = Integer(count))
    return load_up_to(count + 1) <=> count
  end

  return -1 if min && (min = Integer(min)) && (load_up_to(min) < min)

  return 1 if max && (max = Integer(max)) && (load_up_to(max + 1) > max)

  if between
    min, max = (between.begin && between.min) || 1, between.end
    max -= 1 if max && between.exclude_end?

    size = load_up_to(max ? max + 1 : min)
    return size <=> min unless between.include?(size)
  end

  0
end

#each(&block) ⇒ Object



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

def each(&block)
  return enum_for(:each) unless block

  @result_cache.each(&block)
  loop do
    next_result = @results_enum.next
    add_to_cache(next_result)
    yield next_result
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/capybara/result.rb', line 80

def empty?
  !any?
end

#failure_messageObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/capybara/result.rb', line 113

def failure_message
  message = @query.failure_message
  if count.zero?
    message << ' but there were no matches'
  else
    message << ", found #{count} #{Capybara::Helpers.declension('match', 'matches', count)}: " \
            << full_results.map { |r| r.text.inspect }.join(', ')
  end
  unless rest.empty?
    elements = rest.map { |el| el.text rescue '<<ERROR>>' }.map(&:inspect).join(', ') # rubocop:disable Style/RescueModifier
    message << '. Also found ' << elements << ', which matched the selector but not all filters. '
    message << @filter_errors.join('. ') if (rest.size == 1) && count.zero?
  end
  message
end

#matches_count?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/capybara/result.rb', line 109

def matches_count?
  compare_count.zero?
end

#negative_failure_messageObject



129
130
131
# File 'lib/capybara/result.rb', line 129

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

#unfiltered_sizeObject



133
134
135
# File 'lib/capybara/result.rb', line 133

def unfiltered_size
  @elements.length
end