Module: Dommy::Rails::PageInspector

Defined in:
lib/dommy/rails/page_inspector.rb

Class Method Summary collapse

Class Method Details

.authenticity_token?(document) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/dommy/rails/page_inspector.rb', line 30

def authenticity_token?(document)
  document.query_selector_all("input[type='hidden'][name='authenticity_token']").to_a.any? do |input|
    input.get_attribute("value").to_s != ""
  end
end

.checkable_fields(document, name: nil, checked: nil) ⇒ Object



53
54
55
# File 'lib/dommy/rails/page_inspector.rb', line 53

def checkable_fields(document, name: nil, checked: nil)
  Dommy::Internal::ElementMatching.find_checkable_fields(document, name: name, checked: checked)
end

.csrf_meta_tags?(document) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/dommy/rails/page_inspector.rb', line 24

def csrf_meta_tags?(document)
  param = document.query_selector("meta[name='csrf-param']")
  token = document.query_selector("meta[name='csrf-token']")
  present_content?(param) && present_content?(token)
end


36
37
38
# File 'lib/dommy/rails/page_inspector.rb', line 36

def links(document, text: nil, href: nil)
  Dommy::Internal::ElementMatching.find_links(document, text: text, href: href && UrlMatcher.new(href))
end

.meta_matches?(document, name: nil, property: nil, content: nil) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'lib/dommy/rails/page_inspector.rb', line 16

def meta_matches?(document, name: nil, property: nil, content: nil)
  document.query_selector_all("meta").to_a.any? do |meta|
    (name.nil? || meta.get_attribute("name").to_s == name.to_s) &&
      (property.nil? || meta.get_attribute("property").to_s == property.to_s) &&
      (content.nil? || meta.get_attribute("content").to_s == content.to_s)
  end
end

.present_content?(element) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/dommy/rails/page_inspector.rb', line 61

def present_content?(element)
  element && element.get_attribute("content").to_s != ""
end

.selects(document, name: nil, label: nil) ⇒ Object



57
58
59
# File 'lib/dommy/rails/page_inspector.rb', line 57

def selects(document, name: nil, label: nil)
  Dommy::Internal::ElementMatching.find_selects(document, name: name, label: label)
end

.title_matches?(document, expected) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/dommy/rails/page_inspector.rb', line 12

def title_matches?(document, expected)
  Dommy::Internal::DomMatching.text_matches?(document.title.to_s, expected, exact: true)
end

.turbo_frames(document, id = nil, text: nil) ⇒ Object



40
41
42
43
44
45
# File 'lib/dommy/rails/page_inspector.rb', line 40

def turbo_frames(document, id = nil, text: nil)
  frames = document.query_selector_all("turbo-frame").to_a
  frames = frames.select { |frame| frame.get_attribute("id").to_s == id.to_s } if id
  frames = frames.select { |frame| Dommy::Internal::DomMatching.text_matches?(frame.text_content, text) } if text
  frames
end

.xpath_matches(document, expression, text: nil) ⇒ Object



47
48
49
50
51
# File 'lib/dommy/rails/page_inspector.rb', line 47

def xpath_matches(document, expression, text: nil)
  nodes = document.xpath(expression).to_a
  nodes = nodes.select { |node| Dommy::Internal::DomMatching.text_matches?(node.text_content, text) } if text
  nodes
end