Canine is a simple series of webpages used to demonstrate the howling success of the Hound testing tool.
Whatever you call it, UI testing/End-to-End Testing/End-to-User Testing/Acceptance Testing--it is often an intensely manual and time-consuming process. Hound carries some of the load through browser automation. Browser automations means that I can automate my user interactions — clicks, fill inputs, file uploads, selecting options, radio buttons, etc. I use this app to demonstrate an overview of Hound and it’s helpers, as well as an example of how Hound tests saved me days of manual end-user testing.
Create a simple Phoenix called Canine.
> mix phx.new canine --no-ecto
Add Hound as a dependency in mix.exs
.
{:hound, "~> 1.0"}
Hound requires a webdriver for browser automation. We will use selenium. Install and run:
> brew install selenium-server-standalone
> selenium-server
Start Hound in test/test_helpers.exs
. Add this above ExUnit.start()
Application.ensure_all_started(:hound)
In config/dev.exs
add:
config :canine, region: System.get_env("REGION")
In config/test.exs
add:
config :hound, browser: "chrome"
config :canine, region: System.get_env("REGION")
. . . and set server
to true:
config :canine, CanineWeb.Endpoint,
http: [port: 4001],
server: true
Start the Phoenix server
- install dependencies with
mix deps.get
- install Node.js dependencies with
cd assets && npm install
- start Phoenix endpoint with a REGION value from the list ["northeast", "midwest", "south", "west"]
- elixir:
REGION=midwest mix phx.server
- interactive elixir:
REGION=midwest iex -S mix phx.server
- elixir:
- visit
localhost:4000
from your browser.
Run tests (in this demo app, Hound is used as a part of ExUnit tests)
mix test
The acceptance tests live in test/canine_web/acceptance/
and use test/support/acceptance_case.exs
, an ExUnit.CaseTemplate
.
- test/
- canine_web/
- acceptance/
- form_test.exs
- region_test.exs
- welcome_test.exs
- support/
- acceptance_case.ex
- test_helper.exs
- navigate_to/2
- navigates to a url or relative path
- find_element/3
- accepts a strategy, selector, and optional retries value
- visible_text/1
- gets the visible text of an element
- page_title/0
- gets the title of the current page
- sets Metadata in Hound session
- uses change_session_to/2 to use multiple browser sessions for permutations
- the
:xpath
strategy - click/1
- clicks on an element
- current_url/0
- gets url of the current page
- uses execute_script/2 to select value from a select list
- executes javascripts
- fill_field/2
- sets a field's value
- uses change_session_to/2 to use multiple browser sessions for permutations
- take_screenshot/1
- takes a screenshot of the current page
- slow down tests with
:timer.sleep()
To highlight Hound's Metadata, Canine uses a Plug called Regionalize
for set regional vernacular. If this were a real app, this value would be part of an authentication process, but since this is a demo app for testing purposes, we aren't going to worry about the prod environment. We will use Application.get_env("REGION")
.
MIX_ENV=dev: In the dev environment, Application.get_env("REGION")
is from an entry in config/dev.exs
that uses System.get_evn("REGION")
to grab the "REGION" value from the shell. If a "REGION" is not found, the user is sent to a non-functional signup page.
MIX_ENV=test: In the test environment, Application.get_env("REGION")
is from Hound's metadata. Metadata parameters are set like this:
Hound.start_session(metadata: %{region: "northeast"})
If the metadata is not found, the test session will fail and alert you with an error.
** (RuntimeError) could not find a session for process #PID<X.XXX.X>
- Hound Helpers, in particular
- Hound Navigation Helpers
- Hound Element Helpers
- Hound Javascript Execution Helpers
- Hound Screenshot Helpers
- Configuring Hound: https://github.com/HashNuke/hound/blob/master/notes/configuring-hound.md
- Official website: http://www.phoenixframework.org/
- Guides: http://phoenixframework.org/docs/overview
- Docs: https://hexdocs.pm/phoenix
- Mailing list: http://groups.google.com/group/phoenix-talk
- Source: https://github.com/phoenixframework/phoenix