Sha256: 3cd0b09ac19a352f2f1887e9e305b16a1ef68a4e996972f5cd9520559f0ab08a

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

module Vault::Test::HTMLHelpers
  # Save and open an HTML document in your browser.
  #
  # @param html [String] The page to open or nil to fetch it from
  #   `last_response.body` if testing a Sinatra app.
  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

  # Parse an HTML document into a `Nokogiri::HTML` instance and store it as
  # the current document.
  def set_doc(body)
    @doc = Nokogiri::HTML(body)
  end

  # Get the current document or parse `last_response.body` into a
  # `Nokogiri::HTML` instance.
  #
  # @return [Nokogiri::HTML] The current HTML document.
  def doc
    @doc || Nokogiri::HTML(last_response.body)
  end

  # Get a list of text matches in the current document for a CSS selector.
  #
  # @param selector [String] A CSS selector to match elements in the document
  #   with.
  # @return [Array] A list of matching elements from the current document.
  def css(selector)
    doc.css(selector)
  end

  # Assert that at least one element in the current document matches the CSS
  # selector.
  #
  # @param selector [String] A CSS selector to match elements in the document
  #   with.
  def assert_includes_css(selector)
    exists = doc.css(selector).first
    assert exists, "Last response must include #{selector}"
  end

  # Assert that the content in the first element matching the CSS selector
  # matches the specified value.
  #
  # @param selector [String] A CSS selector to match elements in the document
  #   with.
  # @param content [String] The content to match in the first matching
  #   element.
  def assert_css(selector, content)
    e = css(selector).first
    assert e, "Element not found: #{selector}"
    assert_includes e.content, content
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vault-test-tools-0.3.8 lib/vault-test-tools/html_helpers.rb
vault-test-tools-0.3.7 lib/vault-test-tools/html_helpers.rb
vault-test-tools-0.3.6 lib/vault-test-tools/html_helpers.rb
vault-test-tools-0.3.5 lib/vault-test-tools/html_helpers.rb