Skip to content

mirego/html_test_helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation



HTMLTestHelpers provides function helpers for unit testing allowing easy assertions for
HTML elements data queried by data-testid attribute.

Installation

The package can be installed by adding html_test_helpers to your list of dependencies in mix.exs:

def deps do
  [
    {:html_test_helpers, github: "mirego/html_test_helpers", tag: "v0.1.1", only: :test}
  ]
end

Usage

Assuming that you have the following HTML:

<!DOCTYPE html>
<html>
  <body>
    <section id="content">
      <p data-testid="paragraph-id">First paragraph content</p>
      <ul>
        <li data-testid="test-li-id-1" class="li-class-1">First line</li>
        <li data-testid="test-li-id-2" class="li-class-2">Second line</li>
      </ul>
      <a
        data-testid="test-link-id"
        class="my-link-class my-other-class"
        href="/expected/link"
        >Details</a
      >
      <span data-testid="test-footer-id" class="footer small">2020</span>
    </section>
  </body>
</html>

You can validate your expected response as follow :

raw_html
|> assert_html_text("paragraph-id", "First paragraph content")
|> assert_html_attribute("test-link-id", "href", "/expected/link")
|> assert_html_attribute("test-link-id", "class", :contains, "my-link-class")
|> assert_html_attribute("test-footer-testid", "class", :equals, "footer small")
# =>
# [{"html", [],
#   [
#     {"body", [],
#     [
#       {"section", [{"id", "content"}],
#         [
#           {"p", [{"data-testid", "paragraph-id"}],
#           ["First paragraph content"]},
#           {"ul", [],
#           [
#             {"li",
#               [{"data-testid", "test-li-id-1"}, {"class", "li-class-1"}],
#               [" First line "]},
#             {"li",
#               [{"data-testid", "test-li-id-2"}, {"class", "li-class-2"}],
#               [" Second line "]}
#           ]},
#           {"a",
#           [
#             {"data-testid", "test-link-id"},
#             {"class", "my-link-class my-other-class"},
#             {"href", "/expected/link"}
#           ], ["Details"]},
#           {"span",
#           [{"data-testid", "test-footer-id"}, {"class", "footer small"}],
#           ["2020"]}
#         ]}
#     ]}
#   ]}
# ]}

if there is an error :

assert_html_text(raw_html, "paragraph-id", "First paragraph content")
# =>
# ** (ExUnit.AssertionError)

# Value identified by data-testid[paragraph-id] is not as expected.

# left:  "First paragraph content"
# right: "wrong content"

#     (html_test_helpers) lib/html_test_helpers.ex:106: HTMLTestHelpers.assert_html_text/3

Also if you just need the value identified by data-testid attribute you can use :

html_texts(raw_html, "test-li-id")
# =>
# ["First line", "Second line]

html_attributes(raw_html, "test-li-id", "class")
# =>
# ["li-class-1", "li-class-2"]

Contributors

  • @romarickb

License

html_test_helpers is © 2019 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We’re a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.

About

HTMLTestHelpers provides function helpers for unit testing allowing easy assertions for HTML elements data queried by data-testid attribute.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages