Hound

From Elixir Wiki
Jump to navigation Jump to search

Hound[edit]

File:Hound logo.png
Hound logo

Hound is an Elixir library for writing integration tests for web applications. It provides a high-level API for interacting with and asserting against web pages, allowing developers to easily automate browser interactions and perform end-to-end testing. Hound uses the WebDriver protocol to communicate with a browser and supports multiple browser drivers, including Chrome, Firefox, and PhantomJS.

Installation[edit]

To use Hound in your Elixir project, you need to add it as a dependency in your `mix.exs` file:

```elixir defp deps do

 [
   {:hound, "~> 2.0"}
 ]

end ```

After adding Hound as a dependency, run `mix deps.get` to fetch the required files.

Getting Started[edit]

To start using Hound, you need to start a browser session using one of the supported drivers. The following example demonstrates starting a Chrome session:

```elixir alias Hound.Session

session = Session.start_chrome ```

Interacting with Web Pages[edit]

Once you have a browser session, you can use Hound to interact with web pages. Hound provides methods for navigating, filling in forms, clicking buttons, selecting options, and more. Here's an example that fills in a form and submits it:

```elixir session |> Session.visit("https://example.com/login") |> Session.fill_field("username", "testuser") |> Session.fill_field("password", "secretpassword") |> Session.click_button("login") ```

Assertions[edit]

Hound allows you to perform assertions to validate the behavior of your web application. You can assert the visibility of elements, their content, or even the current URL. The following example asserts the presence of a welcome message on a page:

```elixir session |> Session.assert_text("Welcome, testuser!") ```

Running Tests[edit]

Hound integrates well with Elixir's testing framework. You can create test modules and write integration tests using Hound as the underlying library. Here's an example of a simple Hound test using ExUnit:

```elixir defmodule MyAppWebTest do

 use ExUnit.Case, async: true
 use Hound.Helpers
 setup do
   # Start the Hound session
   {:ok, session} = Hound.Session.start_chrome
   {:ok, session: session}
 end
 test "login" do
   session
   |> visit("https://example.com/login")
   |> fill_field("username", "testuser")
   |> fill_field("password", "secretpassword")
   |> click_button("login")
   |> assert_text("Welcome, testuser!")
 end
 # Close the Hound session after each test
 def shutdown({:ok, session: session}) do
   Hound.Session.quit(session)
   :ok
 end

end ```

Conclusion[edit]

Hound is a powerful Elixir library that makes writing integration tests for web applications a breeze. Its high-level API and support for multiple browsers make it a valuable tool for automating browser interactions and ensuring the quality of your web applications.

See Also[edit]

  • ExUnit - Elixir's built-in testing framework.
  • Elixir - The programming language that Hound is built on.
  • WebDriver - The underlying protocol used by Hound to communicate with browsers.