Class: RSpecHTML::Search

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#siblingsObject (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

#allObject



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

#attributesObject



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

Returns:

  • (Boolean)

Raises:



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

Returns:

  • (Boolean)


58
59
60
# File 'lib/rspec_html/search.rb', line 58

def has_css?(*args)
  !blank?(@element&.css(*args))
end

#has_xpath?(*args) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rspec_html/search.rb', line 62

def has_xpath?(*args)
  !blank?(@element&.xpath(*args))
end

#include?(val) ⇒ Boolean

Returns:

  • (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, options)
  Element.new(
    find(tag),
    tag,
    options: 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, options)
  Element.new(
    where(tag, options),
    tag,
    options: options,
    siblings: where(tag, options, all: true)
  )
end

#present?Boolean Also known as: exist?

Returns:

  • (Boolean)


40
41
42
# File 'lib/rspec_html/search.rb', line 40

def present?
  !@element.nil?
end

#sizeObject 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

#textObject



74
75
76
# File 'lib/rspec_html/search.rb', line 74

def text
  @element&.text&.gsub(/\s+/, ' ')&.strip || ''
end

#to_aObject



24
25
26
# File 'lib/rspec_html/search.rb', line 24

def to_a
  all.to_a
end

#to_sObject



20
21
22
# File 'lib/rspec_html/search.rb', line 20

def to_s
  @element&.to_s
end

#truncated_textObject



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