2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2012-08-09 20:38:07 -04:00
|
|
|
require 'forwardable'
|
|
|
|
|
2012-06-08 10:47:01 -04:00
|
|
|
module Capybara
|
2013-01-28 18:58:15 -05:00
|
|
|
|
|
|
|
##
|
2015-02-25 23:16:38 -05: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-25 23:16:38 -05:00
|
|
|
# @see Capybara::Node::Element
|
2013-01-28 18:58:15 -05:00
|
|
|
#
|
2012-06-08 10:47:01 -04:00
|
|
|
class Result
|
|
|
|
include Enumerable
|
2012-08-09 20:38:07 -04:00
|
|
|
extend Forwardable
|
2012-06-08 10:47:01 -04:00
|
|
|
|
|
|
|
def initialize(elements, query)
|
2012-07-09 08:47:11 -04:00
|
|
|
@elements = elements
|
2016-08-05 17:44:33 -04:00
|
|
|
@result_cache = []
|
2017-03-07 22:34:26 -05:00
|
|
|
@results_enum = lazy_select_elements { |node| query.matches_filters?(node) }
|
2012-06-08 10:47:01 -04:00
|
|
|
@query = query
|
|
|
|
end
|
|
|
|
|
2016-08-05 17:44:33 -04:00
|
|
|
def_delegators :full_results, :size, :length, :last, :values_at, :inspect, :sample
|
|
|
|
|
|
|
|
alias :index :find_index
|
|
|
|
|
|
|
|
def each(&block)
|
2016-10-18 11:54:57 -04:00
|
|
|
return enum_for(:each) unless block_given?
|
|
|
|
|
2016-08-05 17:44:33 -04:00
|
|
|
@result_cache.each(&block)
|
|
|
|
loop do
|
|
|
|
next_result = @results_enum.next
|
|
|
|
@result_cache << next_result
|
|
|
|
block.call(next_result)
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](*args)
|
2016-08-31 17:55:03 -04:00
|
|
|
if (args.size == 1) && ((idx = args[0]).is_a? Integer) && (idx >= 0)
|
2016-08-05 17:44:33 -04:00
|
|
|
@result_cache << @results_enum.next while @result_cache.size <= idx
|
|
|
|
@result_cache[idx]
|
|
|
|
else
|
|
|
|
full_results[*args]
|
|
|
|
end
|
|
|
|
rescue StopIteration
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
alias :at :[]
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
!any?
|
|
|
|
end
|
2012-06-08 10:47:01 -04:00
|
|
|
|
2017-11-13 16:04:47 -05:00
|
|
|
# rubocop:disable Metrics/MethodLength
|
2017-11-14 15:14:24 -05:00
|
|
|
def compare_count
|
2016-08-31 17:55:03 -04:00
|
|
|
# Only check filters for as many elements as necessary to determine result
|
|
|
|
if @query.options[:count]
|
|
|
|
count_opt = Integer(@query.options[:count])
|
|
|
|
loop do
|
|
|
|
break if @result_cache.size > count_opt
|
|
|
|
@result_cache << @results_enum.next
|
|
|
|
end
|
2017-11-14 15:14:24 -05:00
|
|
|
return @result_cache.size <=> count_opt
|
2016-08-31 17:55:03 -04:00
|
|
|
end
|
2016-08-05 17:44:33 -04:00
|
|
|
|
|
|
|
if @query.options[:minimum]
|
2016-08-31 17:55:03 -04:00
|
|
|
min_opt = Integer(@query.options[:minimum])
|
2016-08-05 17:44:33 -04:00
|
|
|
begin
|
2016-08-31 17:55:03 -04:00
|
|
|
@result_cache << @results_enum.next while @result_cache.size < min_opt
|
2016-08-05 17:44:33 -04:00
|
|
|
rescue StopIteration
|
2017-11-14 15:14:24 -05:00
|
|
|
return -1
|
2016-08-05 17:44:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @query.options[:maximum]
|
2016-08-31 17:55:03 -04:00
|
|
|
max_opt = Integer(@query.options[:maximum])
|
2016-08-05 17:44:33 -04:00
|
|
|
begin
|
2016-08-31 17:55:03 -04:00
|
|
|
@result_cache << @results_enum.next while @result_cache.size <= max_opt
|
2017-11-14 15:14:24 -05:00
|
|
|
return 1
|
2016-08-05 17:44:33 -04:00
|
|
|
rescue StopIteration
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-31 17:55:03 -04:00
|
|
|
if @query.options[:between]
|
2017-11-14 15:14:24 -05:00
|
|
|
max = Integer(@query.options[:between].max)
|
|
|
|
min = Integer(@query.options[:between].min)
|
2016-08-31 17:55:03 -04:00
|
|
|
loop do
|
|
|
|
break if @result_cache.size > max
|
|
|
|
@result_cache << @results_enum.next
|
|
|
|
end
|
2017-11-14 15:14:24 -05:00
|
|
|
return 0 if (@query.options[:between] === @result_cache.size)
|
|
|
|
return -1 if @result_cache.size < min
|
|
|
|
return 1
|
2016-08-31 17:55:03 -04:00
|
|
|
end
|
|
|
|
|
2017-11-14 15:14:24 -05:00
|
|
|
return 0
|
2012-06-08 10:47:01 -04:00
|
|
|
end
|
2017-11-13 16:04:47 -05:00
|
|
|
# rubocop:enable Metrics/MethodLength
|
2012-06-08 10:47:01 -04:00
|
|
|
|
2017-11-14 15:14:24 -05:00
|
|
|
def matches_count?
|
|
|
|
compare_count == 0
|
|
|
|
end
|
|
|
|
|
2012-07-09 07:21:44 -04:00
|
|
|
def failure_message
|
2016-08-30 14:37:54 -04:00
|
|
|
message = @query.failure_message
|
2012-07-09 08:47:11 -04:00
|
|
|
if count > 0
|
2016-08-05 17:44:33 -04:00
|
|
|
message << ", found #{count} #{Capybara::Helpers.declension("match", "matches", count)}: " << full_results.map(&:text).map(&:inspect).join(", ")
|
2012-07-09 08:47:11 -04:00
|
|
|
else
|
|
|
|
message << " but there were no matches"
|
|
|
|
end
|
2016-08-05 17:44:33 -04:00
|
|
|
unless rest.empty?
|
2017-11-14 15:14:24 -05:00
|
|
|
elements = rest.map { |el| el.text rescue "<<ERROR>>" }.map(&:inspect).join(", ")
|
2012-07-09 08:47:11 -04:00
|
|
|
message << ". Also found " << elements << ", which matched the selector but not all filters."
|
|
|
|
end
|
|
|
|
message
|
2012-06-08 10:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def negative_failure_message
|
2014-02-16 12:13:58 -05:00
|
|
|
failure_message.sub(/(to find)/, 'not \1')
|
2012-06-08 10:47:01 -04:00
|
|
|
end
|
2016-08-05 17:44:33 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def full_results
|
2016-08-31 17:55:03 -04:00
|
|
|
loop { @result_cache << @results_enum.next }
|
2016-08-05 17:44:33 -04:00
|
|
|
@result_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def rest
|
|
|
|
@rest ||= @elements - full_results
|
|
|
|
end
|
|
|
|
|
|
|
|
def lazy_select_elements(&block)
|
2017-02-16 15:31:33 -05: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'
|
|
|
|
@elements.select(&block).to_enum # non-lazy evaluation
|
2016-08-05 17:44:33 -04:00
|
|
|
else
|
2016-08-17 14:41:40 -04:00
|
|
|
@elements.lazy.select(&block)
|
2016-08-05 17:44:33 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-08 10:47:01 -04:00
|
|
|
end
|
|
|
|
end
|