Module: Vault::Test::HTMLHelpers

Defined in:
lib/vault-test-tools/html_helpers.rb

Instance Method Summary collapse

Instance Method Details

#assert_css(selector, content) ⇒ Object

Assert that the content in the first element matching the CSS selector matches the specified value.

Parameters:

  • selector (String)

    A CSS selector to match elements in the document with.

  • content (String)

    The content to match in the first matching element.



55
56
57
58
59
# File 'lib/vault-test-tools/html_helpers.rb', line 55

def assert_css(selector, content)
  e = css(selector).first
  assert e, "Element not found: #{selector}"
  assert_includes e.content, content
end

#assert_includes_css(selector) ⇒ Object

Assert that at least one element in the current document matches the CSS selector.

Parameters:

  • selector (String)

    A CSS selector to match elements in the document with.



43
44
45
46
# File 'lib/vault-test-tools/html_helpers.rb', line 43

def assert_includes_css(selector)
  exists = doc.css(selector).first
  assert exists, "Last response must include #{selector}"
end

#css(selector) ⇒ Array

Get a list of text matches in the current document for a CSS selector.

Parameters:

  • selector (String)

    A CSS selector to match elements in the document with.

Returns:

  • (Array)

    A list of matching elements from the current document.



34
35
36
# File 'lib/vault-test-tools/html_helpers.rb', line 34

def css(selector)
  doc.css(selector)
end

#docNokogiri::HTML

Get the current document or parse ‘last_response.body` into a `Nokogiri::HTML` instance.

Returns:

  • (Nokogiri::HTML)

    The current HTML document.



25
26
27
# File 'lib/vault-test-tools/html_helpers.rb', line 25

def doc
  @doc || Nokogiri::HTML(last_response.body)
end

#save_and_open_page(html = nil, name = 'page.html', i = 1) ⇒ Object

Save and open an HTML document in your browser.

Parameters:

  • html (String) (defaults to: nil)

    The page to open or nil to fetch it from ‘last_response.body` if testing a Sinatra app.



8
9
10
11
12
13
# File 'lib/vault-test-tools/html_helpers.rb', line 8

def save_and_open_page(html = nil, name = 'page.html', i = 1)
  html ||= last_response.body
  name = "page_#{i=i+1}.html" while File.exist? name
  File.open(name, 'w') { |f| f << html }
  system "open #{name}"
end

#set_doc(body) ⇒ Object

Parse an HTML document into a ‘Nokogiri::HTML` instance and store it as the current document.



17
18
19
# File 'lib/vault-test-tools/html_helpers.rb', line 17

def set_doc(body)
  @doc = Nokogiri::HTML(body)
end