2016-03-07 16:52:19 -08:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 12:23:54 -08:00
|
|
|
|
2012-08-09 20:38:07 -04:00
|
|
|
require 'forwardable'
|
|
|
|
|
2012-06-08 16:47:01 +02:00
|
|
|
module Capybara
|
2013-01-28 18:58:15 -05:00
|
|
|
##
|
2015-02-26 17:16:38 +13:00
|
|
|
# A {Capybara::Result} represents a collection of {Capybara::Node::Element} on the page. It is possible to interact with this
|
2013-01-28 18:58:15 -05:00
|
|
|
# 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?()
|
|
|
|
#
|
2015-02-26 17:16:38 +13:00
|
|
|
# @see Capybara::Node::Element
|
2013-01-28 18:58:15 -05:00
|
|
|
#
|
2012-06-08 16:47:01 +02:00
|
|
|
class Result
|
|
|
|
include Enumerable
|
2012-08-09 20:38:07 -04:00
|
|
|
extend Forwardable
|
2012-06-08 16:47:01 +02:00
|
|
|
|
|
|
|
def initialize(elements, query)
|
2012-07-09 14:47:11 +02:00
|
|
|
@elements = elements
|
2016-08-05 14:44:33 -07:00
|
|
|
@result_cache = []
|
2017-03-07 19:34:26 -08:00
|
|
|
@results_enum = lazy_select_elements { |node| query.matches_filters?(node) }
|
2012-06-08 16:47:01 +02:00
|
|
|
@query = query
|
|
|
|
end
|
|
|
|
|
2016-08-05 14:44:33 -07:00
|
|
|
def_delegators :full_results, :size, :length, :last, :values_at, :inspect, :sample
|
|
|
|
|
|
|
|
alias :index :find_index
|
|
|
|
|
|
|
|
def each(&block)
|
2016-10-18 08:54:57 -07:00
|
|
|
return enum_for(:each) unless block_given?
|
|
|
|
|
2016-08-05 14:44:33 -07:00
|
|
|
@result_cache.each(&block)
|
|
|
|
loop do
|
|
|
|
next_result = @results_enum.next
|
|
|
|
@result_cache << next_result
|
2018-01-08 12:23:54 -08:00
|
|
|
yield next_result
|
2016-08-05 14:44:33 -07:00
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](*args)
|
2018-05-11 15:14:51 -07:00
|
|
|
idx, length = args
|
|
|
|
max_idx = case idx
|
|
|
|
when Integer
|
2018-05-10 13:20:23 -07:00
|
|
|
if !idx.negative?
|
2018-05-11 15:14:51 -07:00
|
|
|
length.nil? ? idx : idx + length - 1
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
when Range
|
|
|
|
idx.max
|
|
|
|
end
|
|
|
|
|
|
|
|
if max_idx.nil?
|
2016-08-05 14:44:33 -07:00
|
|
|
full_results[*args]
|
2018-05-11 15:14:51 -07:00
|
|
|
else
|
2018-08-17 13:57:12 -07:00
|
|
|
load_up_to(max_idx + 1)
|
2018-05-11 15:14:51 -07:00
|
|
|
@result_cache[*args]
|
2016-08-05 14:44:33 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
alias :at :[]
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
!any?
|
|
|
|
end
|
2012-06-08 16:47:01 +02:00
|
|
|
|
2017-11-14 12:14:24 -08:00
|
|
|
def compare_count
|
2016-08-31 14:55:03 -07:00
|
|
|
# Only check filters for as many elements as necessary to determine result
|
2018-08-17 13:57:12 -07:00
|
|
|
if (count = @query.options[:count])
|
|
|
|
count = Integer(count)
|
|
|
|
return load_up_to(count + 1) <=> count
|
2016-08-31 14:55:03 -07:00
|
|
|
end
|
2016-08-05 14:44:33 -07:00
|
|
|
|
2018-08-17 13:57:12 -07:00
|
|
|
if (min = @query.options[:minimum])
|
|
|
|
min = Integer(min)
|
|
|
|
return -1 if load_up_to(min) < min
|
2016-08-05 14:44:33 -07:00
|
|
|
end
|
|
|
|
|
2018-08-17 13:57:12 -07:00
|
|
|
if (max = @query.options[:maximum])
|
|
|
|
max = Integer(max)
|
|
|
|
return 1 if load_up_to(max + 1) > max
|
2016-08-05 14:44:33 -07:00
|
|
|
end
|
|
|
|
|
2018-08-17 13:57:12 -07:00
|
|
|
if (between = @query.options[:between])
|
|
|
|
min, max = between.minmax
|
|
|
|
size = load_up_to(max + 1)
|
|
|
|
return 0 if between.include? size
|
|
|
|
return size <=> min
|
2016-08-31 14:55:03 -07:00
|
|
|
end
|
|
|
|
|
2018-05-14 14:30:34 -07:00
|
|
|
0
|
2012-06-08 16:47:01 +02:00
|
|
|
end
|
|
|
|
|
2017-11-14 12:14:24 -08:00
|
|
|
def matches_count?
|
2018-01-08 12:23:54 -08:00
|
|
|
compare_count.zero?
|
2017-11-14 12:14:24 -08:00
|
|
|
end
|
|
|
|
|
2012-07-09 13:21:44 +02:00
|
|
|
def failure_message
|
2016-08-30 11:37:54 -07:00
|
|
|
message = @query.failure_message
|
2018-01-13 13:06:03 -08:00
|
|
|
if count.zero?
|
2018-07-10 14:18:39 -07:00
|
|
|
message << ' but there were no matches'
|
2018-01-13 13:06:03 -08:00
|
|
|
else
|
2018-07-10 14:18:39 -07:00
|
|
|
message << ", found #{count} #{Capybara::Helpers.declension('match', 'matches', count)}: " << full_results.map(&:text).map(&:inspect).join(', ')
|
2012-07-09 14:47:11 +02:00
|
|
|
end
|
2016-08-05 14:44:33 -07:00
|
|
|
unless rest.empty?
|
2018-07-10 14:18:39 -07: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.'
|
2012-07-09 14:47:11 +02:00
|
|
|
end
|
|
|
|
message
|
2012-06-08 16:47:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def negative_failure_message
|
2014-02-16 20:13:58 +03:00
|
|
|
failure_message.sub(/(to find)/, 'not \1')
|
2012-06-08 16:47:01 +02:00
|
|
|
end
|
2016-08-05 14:44:33 -07:00
|
|
|
|
2018-07-12 12:32:41 -07:00
|
|
|
def unfiltered_size
|
|
|
|
@elements.length
|
|
|
|
end
|
|
|
|
|
2018-01-09 14:05:50 -08:00
|
|
|
private
|
2016-08-05 14:44:33 -07:00
|
|
|
|
2018-08-17 13:57:12 -07:00
|
|
|
def load_up_to(num)
|
|
|
|
loop do
|
|
|
|
break if @result_cache.size >= num
|
|
|
|
@result_cache << @results_enum.next
|
|
|
|
end
|
|
|
|
@result_cache.size
|
|
|
|
end
|
|
|
|
|
2016-08-05 14:44:33 -07:00
|
|
|
def full_results
|
2016-08-31 14:55:03 -07:00
|
|
|
loop { @result_cache << @results_enum.next }
|
2016-08-05 14:44:33 -07:00
|
|
|
@result_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def rest
|
|
|
|
@rest ||= @elements - full_results
|
|
|
|
end
|
|
|
|
|
|
|
|
def lazy_select_elements(&block)
|
2017-02-16 12:31:33 -08:00
|
|
|
# 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 14:05:50 -08:00
|
|
|
@elements.select(&block).to_enum # non-lazy evaluation
|
2016-08-05 14:44:33 -07:00
|
|
|
else
|
2016-08-17 11:41:40 -07:00
|
|
|
@elements.lazy.select(&block)
|
2016-08-05 14:44:33 -07:00
|
|
|
end
|
|
|
|
end
|
2012-06-08 16:47:01 +02:00
|
|
|
end
|
|
|
|
end
|