1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/result.rb

163 lines
3.9 KiB
Ruby
Raw Normal View History

2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2018-01-08 15:23:54 -05:00
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:
#
2018-10-31 12:07:25 -04:00
# * \[\]
# * each()
# * at()
# * size()
# * count()
# * length()
# * first()
# * last()
# * empty?()
2018-11-01 12:08:35 -04:00
# * values_at()
# * sample()
#
# @see Capybara::Node::Element
#
class Result
include Enumerable
extend Forwardable
def initialize(elements, query)
2012-07-09 08:47:11 -04:00
@elements = elements
@result_cache = []
@filter_errors = []
@results_enum = lazy_select_elements { |node| query.matches_filters?(node, @filter_errors) }
@query = query
end
def_delegators :full_results, :size, :length, :last, :values_at, :inspect, :sample
2018-10-12 19:21:53 -04:00
alias index find_index
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
2018-01-08 15:23:54 -05:00
yield next_result
end
self
end
def [](*args)
idx, length = args
max_idx = case idx
when Integer
2018-05-10 16:20:23 -04:00
if !idx.negative?
length.nil? ? idx : idx + length - 1
else
nil
end
when Range
idx.end && idx.max # endless range will have end == nil
end
if max_idx.nil?
full_results[*args]
else
2018-08-17 16:57:12 -04:00
load_up_to(max_idx + 1)
@result_cache[*args]
end
end
alias :at :[]
def empty?
!any?
end
2017-11-14 15:14:24 -05:00
def compare_count
2018-10-11 13:59:17 -04:00
count, min, max, between = @query.options.values_at(:count, :minimum, :maximum, :between)
2016-08-31 17:55:03 -04:00
# Only check filters for as many elements as necessary to determine result
2018-10-11 13:59:17 -04:00
if count && (count = Integer(count))
2018-08-17 16:57:12 -04:00
return load_up_to(count + 1) <=> count
2016-08-31 17:55:03 -04:00
end
2018-10-11 13:59:17 -04:00
if min && (min = Integer(min))
2018-08-17 16:57:12 -04:00
return -1 if load_up_to(min) < min
end
2018-10-11 13:59:17 -04:00
if max && (max = Integer(max))
2018-08-17 16:57:12 -04:00
return 1 if load_up_to(max + 1) > max
end
2018-10-11 13:59:17 -04:00
if between
min, max = between.min, (between.end && between.max)
size = load_up_to(max ? max + 1 : min)
2018-10-11 13:59:17 -04:00
return between.include?(size) ? 0 : size <=> min
2016-08-31 17:55:03 -04:00
end
2018-05-14 17:30:34 -04:00
0
end
2017-11-14 15:14:24 -05:00
def matches_count?
2018-01-08 15:23:54 -05:00
compare_count.zero?
2017-11-14 15:14:24 -05:00
end
2012-07-09 07:21:44 -04:00
def failure_message
2016-08-30 14:37:54 -04:00
message = @query.failure_message
2018-01-13 16:06:03 -05:00
if count.zero?
2018-07-10 17:18:39 -04:00
message << ' but there were no matches'
2018-01-13 16:06:03 -05:00
else
message << ", found #{count} #{Capybara::Helpers.declension('match', 'matches', count)}: " \
<< full_results.map(&:text).map(&:inspect).join(', ')
2012-07-09 08:47:11 -04:00
end
unless rest.empty?
2018-07-10 17:18:39 -04:00
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?
2012-07-09 08:47:11 -04:00
end
message
end
def negative_failure_message
failure_message.sub(/(to find)/, 'not \1')
end
def unfiltered_size
@elements.length
end
2018-01-09 17:05:50 -05:00
private
2018-08-17 16:57:12 -04:00
def load_up_to(num)
loop do
break if @result_cache.size >= num
2018-09-24 12:43:46 -04:00
2018-08-17 16:57:12 -04:00
@result_cache << @results_enum.next
end
@result_cache.size
end
def full_results
2016-08-31 17:55:03 -04:00
loop { @result_cache << @results_enum.next }
@result_cache
end
def rest
@rest ||= @elements - full_results
end
def lazy_select_elements(&block)
# JRuby has an issue with lazy enumerators which
# causes a concurrency issue with network requests here
# https://github.com/jruby/jruby/issues/4212
if RUBY_PLATFORM == 'java'
2018-01-09 17:05:50 -05:00
@elements.select(&block).to_enum # non-lazy evaluation
else
@elements.lazy.select(&block)
end
end
end
end