Class: Capybara::Result
- Inherits:
-
Object
- Object
- Capybara::Result
- 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?()
Instance Method Summary collapse
- #[](*args) ⇒ Object (also: #at)
- #compare_count ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #failure_message ⇒ Object
-
#initialize(elements, query) ⇒ Result
constructor
A new instance of Result.
- #matches_count? ⇒ Boolean
- #negative_failure_message ⇒ Object
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_cache = [] @results_enum = lazy_select_elements { |node| query.matches_filters?(node) } @query = query end |
Instance Method Details
#[](*args) ⇒ Object Also known as: at
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/capybara/result.rb', line 49 def [](*args) idx, length = args max_idx = case idx when Integer if !idx.negative? length.nil? ? idx : idx + length - 1 else nil end when Range idx.max end if max_idx.nil? full_results[*args] else loop do break if @result_cache.size > max_idx @result_cache << @results_enum.next end @result_cache[*args] end end |
#compare_count ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/capybara/result.rb', line 78 def compare_count # Only check filters for as many elements as necessary to determine result if @query.[:count] count_opt = Integer(@query.[:count]) loop do break if @result_cache.size > count_opt @result_cache << @results_enum.next end return @result_cache.size <=> count_opt end if @query.[:minimum] min_opt = Integer(@query.[:minimum]) begin @result_cache << @results_enum.next while @result_cache.size < min_opt rescue StopIteration return -1 end end if @query.[:maximum] max_opt = Integer(@query.[:maximum]) loop do return 1 if @result_cache.size > max_opt @result_cache << @results_enum.next end end if @query.[:between] min, max = @query.[:between].minmax loop do break if @result_cache.size > max @result_cache << @results_enum.next end return 0 if @query.[:between].include? @result_cache.size return @result_cache.size <=> min end 0 end |
#each(&block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/capybara/result.rb', line 37 def each(&block) return enum_for(:each) unless block_given? @result_cache.each(&block) loop do next_result = @results_enum.next @result_cache << next_result yield next_result end self end |
#empty? ⇒ Boolean
74 75 76 |
# File 'lib/capybara/result.rb', line 74 def empty? !any? end |
#failure_message ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/capybara/result.rb', line 123 def = @query. if count.zero? << " but there were no matches" else << ", found #{count} #{Capybara::Helpers.declension('match', 'matches', count)}: " << full_results.map(&:text).map(&:inspect).join(", ") end unless rest.empty? elements = rest.map { |el| el.text rescue "<<ERROR>>" }.map(&:inspect).join(", ") # rubocop:disable Style/RescueModifier << ". Also found " << elements << ", which matched the selector but not all filters." end end |
#matches_count? ⇒ Boolean
119 120 121 |
# File 'lib/capybara/result.rb', line 119 def matches_count? compare_count.zero? end |
#negative_failure_message ⇒ Object
137 138 139 |
# File 'lib/capybara/result.rb', line 137 def .sub(/(to find)/, 'not \1') end |