Class: RSpecHTML::Search
- Inherits:
-
Object
- Object
- RSpecHTML::Search
- Extended by:
- Forwardable
- Defined in:
- lib/rspec_html/search.rb
Overview
Provides element/attribute/text searching for HTML entities rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#siblings ⇒ Object
readonly
Returns the value of attribute siblings.
Instance Method Summary collapse
-
#[](val) ⇒ Object
rubocop:enable Naming/PredicateName.
- #all ⇒ Object
- #attributes ⇒ Object
- #checked? ⇒ Boolean
-
#children(text: false) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
- #css(*args) ⇒ Object
-
#has_css?(*args) ⇒ Boolean
rubocop:disable Naming/PredicateName.
- #has_xpath?(*args) ⇒ Boolean
- #include?(val) ⇒ Boolean
-
#initialize(element, siblings, element_wrapper) ⇒ Search
constructor
A new instance of Search.
- #new_from_find(tag, options) ⇒ Object
- #new_from_where(tag, options) ⇒ Object
- #present? ⇒ Boolean (also: #exist?)
- #size ⇒ Object (also: #length)
- #text ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #truncated_text ⇒ Object
- #xpath(*args) ⇒ Object
Constructor Details
#initialize(element, siblings, element_wrapper) ⇒ Search
Returns a new instance of Search.
14 15 16 17 18 |
# File 'lib/rspec_html/search.rb', line 14 def initialize(element, siblings, element_wrapper) @element = element @siblings = siblings @element_wrapper = element_wrapper end |
Instance Attribute Details
#siblings ⇒ Object (readonly)
Returns the value of attribute siblings.
7 8 9 |
# File 'lib/rspec_html/search.rb', line 7 def siblings @siblings end |
Instance Method Details
#[](val) ⇒ Object
rubocop:enable Naming/PredicateName
67 68 69 70 71 72 |
# File 'lib/rspec_html/search.rb', line 67 def [](val) return index(val) if val.is_a?(Integer) return range(val) if val.is_a?(Range) @element&.attr(val.to_s) end |
#all ⇒ Object
51 52 53 54 55 |
# File 'lib/rspec_html/search.rb', line 51 def all return [] if @siblings.nil? @siblings.map { |sibling| Element.new(sibling, @element&.name) } end |
#attributes ⇒ Object
85 86 87 |
# File 'lib/rspec_html/search.rb', line 85 def attributes @element&.attributes.to_h { |key, val| [key.to_sym, val.to_s] } end |
#checked? ⇒ Boolean
45 46 47 48 49 |
# File 'lib/rspec_html/search.rb', line 45 def checked? raise ElementNotFoundError, "Element does not exist: #{element_wrapper.reconstituted}" if @element.nil? @element.attributes.key?('checked') end |
#children(text: false) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
96 97 98 99 100 |
# File 'lib/rspec_html/search.rb', line 96 def children(text: false) # rubocop:disable Metrics/CyclomaticComplexity @element&.children &.map { |child| Element.new(child, child.name, siblings: @element.children) } &.reject { |child| text ? false : child.name == 'text' } || [] end |
#css(*args) ⇒ Object
32 33 34 |
# File 'lib/rspec_html/search.rb', line 32 def css(*args) self.class.new(@element&.css(*args), :css, element_wrapper) end |
#has_css?(*args) ⇒ Boolean
rubocop:disable Naming/PredicateName
58 59 60 |
# File 'lib/rspec_html/search.rb', line 58 def has_css?(*args) !blank?(@element&.css(*args)) end |
#has_xpath?(*args) ⇒ Boolean
62 63 64 |
# File 'lib/rspec_html/search.rb', line 62 def has_xpath?(*args) !blank?(@element&.xpath(*args)) end |
#include?(val) ⇒ Boolean
28 29 30 |
# File 'lib/rspec_html/search.rb', line 28 def include?(val) text.include?(val) end |
#new_from_find(tag, options) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/rspec_html/search.rb', line 102 def new_from_find(tag, ) Element.new( find(tag), tag, options: , siblings: find(tag, all: true) ) end |
#new_from_where(tag, options) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/rspec_html/search.rb', line 111 def new_from_where(tag, ) Element.new( where(tag, ), tag, options: , siblings: where(tag, , all: true) ) end |
#present? ⇒ Boolean Also known as: exist?
40 41 42 |
# File 'lib/rspec_html/search.rb', line 40 def present? !@element.nil? end |
#size ⇒ Object Also known as: length
89 90 91 92 93 |
# File 'lib/rspec_html/search.rb', line 89 def size return @element.size if @element.respond_to?(:size) @siblings&.size || 0 end |
#text ⇒ Object
74 75 76 |
# File 'lib/rspec_html/search.rb', line 74 def text @element&.text&.gsub(/\s+/, ' ')&.strip || '' end |
#to_a ⇒ Object
24 25 26 |
# File 'lib/rspec_html/search.rb', line 24 def to_a all.to_a end |
#to_s ⇒ Object
20 21 22 |
# File 'lib/rspec_html/search.rb', line 20 def to_s @element&.to_s end |
#truncated_text ⇒ Object
78 79 80 81 82 83 |
# File 'lib/rspec_html/search.rb', line 78 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) ⇒ Object
36 37 38 |
# File 'lib/rspec_html/search.rb', line 36 def xpath(*args) self.class.new(@element&.xpath(*args), :xpath, element_wrapper) end |