class RSpecHTML::Search
Provides element/attribute/text searching for HTML entities
Public Class Methods
new(element, siblings)
click to toggle source
# File lib/rspec_html/search.rb, line 6 def initialize(element, siblings) @element = element @siblings = siblings end
Public Instance Methods
[](val)
click to toggle source
rubocop:enable Naming/PredicateName
# File lib/rspec_html/search.rb, line 38 def [](val) return index(val) if val.is_a?(Integer) return range(val) if val.is_a?(Range) @element&.attr(val.to_s) end
css(*args)
click to toggle source
# File lib/rspec_html/search.rb, line 15 def css(*args) self.class.new(@element&.css(*args), :css) end
has_css?(*args)
click to toggle source
rubocop:disable Naming/PredicateName
# File lib/rspec_html/search.rb, line 29 def has_css?(*args) !@element&.css(*args)&.empty? end
has_xpath?(*args)
click to toggle source
# File lib/rspec_html/search.rb, line 33 def has_xpath?(*args) !@element&.xpath(*args)&.empty? end
include?(val)
click to toggle source
# File lib/rspec_html/search.rb, line 11 def include?(val) text.include?(val) end
new_from_find(tag, options)
click to toggle source
# File lib/rspec_html/search.rb, line 63 def new_from_find(tag, options) Element.new( find(tag), tag, options: options, siblings: find(tag, all: true) ) end
new_from_where(tag, options)
click to toggle source
# File lib/rspec_html/search.rb, line 72 def new_from_where(tag, options) Element.new( where(tag, options), tag, options: options, siblings: where(tag, options, all: true) ) end
present?()
click to toggle source
# File lib/rspec_html/search.rb, line 23 def present? !@element.nil? end
Also aliased as: exist?
size()
click to toggle source
# File lib/rspec_html/search.rb, line 56 def size return @element.size if @element.respond_to?(:size) @siblings.size end
Also aliased as: length
text()
click to toggle source
# File lib/rspec_html/search.rb, line 45 def text @element&.text&.gsub(/\s+/, ' ')&.strip || '' end
truncated_text()
click to toggle source
# File lib/rspec_html/search.rb, line 49 def truncated_text max = RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length return text if text.size <= max "#{text[0..max]}...#{text[-max..-1]}" end
xpath(*args)
click to toggle source
# File lib/rspec_html/search.rb, line 19 def xpath(*args) self.class.new(@element&.xpath(*args), :xpath) end
Private Instance Methods
find(tag, all: false)
click to toggle source
# File lib/rspec_html/search.rb, line 129 def find(tag, all: false) return @element&.css(tag.to_s)&.first unless all @element&.css(tag.to_s) end
index(val)
click to toggle source
# File lib/rspec_html/search.rb, line 83 def index(val) zero_index_error if val.zero? self.class.new(@siblings[val - 1], @element.name) end
range(val)
click to toggle source
# File lib/rspec_html/search.rb, line 88 def range(val) zero_index_error if val.first.zero? self.class.new(@siblings[(val.first - 1)..(val.last - 1)], :range) end
where(tag, query, all: false)
click to toggle source
# File lib/rspec_html/search.rb, line 97 def where(tag, query, all: false) matched = if query[:class] where_class(tag, query[:class]) & where_xpath(tag, query.merge(class: nil)) else where_xpath(tag, query) end return nil unless matched || all return matched&.first unless all matched end
where_class(tag, class_or_classes)
click to toggle source
# File lib/rspec_html/search.rb, line 123 def where_class(tag, class_or_classes) classes = class_or_classes.is_a?(Array) ? class_or_classes : class_or_classes.to_s.split selector = classes.map(&:to_s).join('.') @element&.css("#{tag}.#{selector}") end
where_conditions(query)
click to toggle source
# File lib/rspec_html/search.rb, line 114 def where_conditions(query) query.compact.map do |key, value| next if value.nil? next %(@#{key}="#{value}") unless key == :text %[contains(text(),"#{value}")] end.join ' and ' end
where_xpath(tag, query)
click to toggle source
# File lib/rspec_html/search.rb, line 109 def where_xpath(tag, query) conditions = "[#{where_conditions(query)}]" unless query.compact.empty? @element&.xpath("//#{tag}#{conditions}") end
zero_index_error()
click to toggle source
# File lib/rspec_html/search.rb, line 93 def zero_index_error raise ArgumentError, 'Index for matched sets starts at 1, not 0.' end