View Source PhoenixTest (PhoenixTest v0.2.5)

PhoenixTest provides a unified way of writing feature tests -- regardless of whether you're testing LiveView pages or static pages.

It also handles navigation between LiveView and static pages seamlessly. So, you don't have to worry about what type of page you're visiting. Just write the tests from the user's perspective.

Thus, you can test a flow going from static to LiveView pages and back without having to worry about the underlying implementation.

This is a sample flow:

  test "admin can create a user", %{conn: conn} do
    conn
    |> visit("/")
    |> click_link("Users")
    |> fill_form("#user-form", name: "Aragorn", email: "aragorn@dunedan.com")
    |> click_button("Create")
    |> assert_has(".user", "Aragorn")
  end

Note that PhoenixTest does not handle JavaScript. If you're looking for something that supports JavaScript, take a look at Wallaby.

Summary

Functions

Assert helper to ensure an element with given CSS selector and text is present.

Perfoms action defined by button.

Performs action defined by button with CSS selector.

Clicks a link with given text and performs the action.

Clicks a link with given CSS selector and text and performs the action. selector to target the link.

Fills form data, validating that input fields are present.

Opposite of assert_has/3 helper. Verifies that element with given CSS selector and text is not present.

Submits form in the same way one would do by pressing <Enter>.

Entrypoint to create a session.

Functions

Link to this function

assert_has(session, selector, text)

View Source

Assert helper to ensure an element with given CSS selector and text is present.

It'll raise an error if no elements are found, but it will not raise if more than one matching element is found.

Link to this function

click_button(session, text)

View Source

Perfoms action defined by button.

  • If the button has a phx-click on it, it'll send the event to the LiveView.

  • If the button doesn't have a phx-click on it, it'll submit the parent form.

This function can be preceded by fill_form to fill out a form and subsequently submit the form. Note that fill_form/3 + click_button/2 works for both static and live pages.

If click_button/2 is used alone (without a phx-click), it is assumed it is a form with a single button (e.g. "Delete").

Examples

# form with single button or button with `phx-click`
session
|> click_button("Delete")

# fill out form and then submit
session
|> fill_form("#user-form", name: "Aragorn")
|> click_button("Create")
Link to this function

click_button(session, selector, text)

View Source

Performs action defined by button with CSS selector.

See click_button/2 for more details.

Link to this function

click_link(session, text)

View Source

Clicks a link with given text and performs the action.

Here's how it handles different types of a tags:

  • With href: follows it to the next page
  • With phx-submit: it'll send the event to the appropriate LiveView
  • With live redirect: it'll follow the live navigation to the next LiveView
  • With live patch: it'll patch the current LiveView
Link to this function

click_link(session, selector, text)

View Source

Clicks a link with given CSS selector and text and performs the action. selector to target the link.

See click_link/2 for more details.

Link to this function

fill_form(session, selector, data)

View Source

Fills form data, validating that input fields are present.

This can be used by both static and live pages.

If the form is a LiveView form, and if the form has a phx-change attribute defined, fill_form/3 will trigger the phx-change event.

This can be followed by a click_button/3 to submit the form.

Examples

session
|> fill_form("#user-form", name: "Aragorn")
|> click_button("Create")

If your form has nested data -- for example, with an input such as <input name="user[email]"> -- you can pass a nested map as the last argument:

session
|> fill_form("#user-form", user: %{email: "aragorn@dunedain.com"})
|> click_button("Create")
Link to this function

refute_has(session, selector, text)

View Source

Opposite of assert_has/3 helper. Verifies that element with given CSS selector and text is not present.

It'll raise an error if any elements that match selector and text are found.

Link to this function

submit_form(session, selector, data)

View Source

Submits form in the same way one would do by pressing <Enter>.

Note that this does not validate presence of the submit button.

In the case of LiveView forms, it'll submit the form with LiveView's phx-submit event.

If it's a static form, this is equivalent to filling the form and submitting it with the form's method and to the form's action.

Note: if your form has a submit button, it's recommended you test with fill_form/3 + click_button/2 instead.

Examples

session
|> submit_form("#user-form", name: "Aragorn")

If your form has nested data -- for example, with an input such as <input name="user[email]"> -- you can pass a nested map as the last argument:

session
|> submit_form("#user-form", user: %{email: "aragorn@dunedain.com"})

Entrypoint to create a session.

visit/2 takes a Plug.Conn struct and the path to visit.

It returns a session which the rest of the PhoenixTest functions can use.

Note that visit/2 is smart enough to know if the page you're visiting is a LiveView or a static view. You don't need to worry about which type of page you're visiting.