|
1 | 1 | defmodule PhoenixTest do
|
2 | 2 | @moduledoc """
|
3 |
| - PhoenixTest provides a unified way of writing feature tests -- regardless of |
4 |
| - whether you're testing LiveView pages or static pages. |
| 3 | + PhoenixTest provides a unified way of writing feature tests -- regardless of |
| 4 | + whether you're testing LiveView pages or static pages. |
5 | 5 |
|
6 |
| - It also handles navigation between LiveView and static pages seamlessly. So, you |
7 |
| - don't have to worry about what type of page you're visiting. Just write the |
8 |
| - tests from the user's perspective. |
| 6 | + It also handles navigation between LiveView and static pages seamlessly. So, you |
| 7 | + don't have to worry about what type of page you're visiting. Just write the |
| 8 | + tests from the user's perspective. |
9 | 9 |
|
10 |
| - Thus, you can test a flow going from static to LiveView pages and back without |
11 |
| - having to worry about the underlying implementation. |
| 10 | + Thus, you can test a flow going from static to LiveView pages and back without |
| 11 | + having to worry about the underlying implementation. |
12 | 12 |
|
13 |
| - This is a sample flow: |
| 13 | + This is a sample flow: |
14 | 14 |
|
15 |
| - ```elixir |
16 |
| - test "admin can create a user", %{conn: conn} do |
| 15 | + ```elixir |
| 16 | + test "admin can create a user", %{conn: conn} do |
| 17 | + conn |
| 18 | + |> visit("/") |
| 19 | + |> click_link("Users") |
| 20 | + |> fill_in("Name", with: "Aragorn") |
| 21 | + |> choose("Ranger") |
| 22 | + |> assert_has(".user", text: "Aragorn") |
| 23 | + end |
| 24 | + ``` |
| 25 | +
|
| 26 | + Note that PhoenixTest does _not_ handle JavaScript. If you're looking for |
| 27 | + something that supports JavaScript, take a look at |
| 28 | + [Wallaby](https://hexdocs.pm/wallaby/readme.html). |
| 29 | +
|
| 30 | + ## Setup |
| 31 | +
|
| 32 | + PhoenixTest requires Phoenix `1.7+` and LiveView `0.20+`. It may work with |
| 33 | + earlier versions, but I have not tested that. |
| 34 | +
|
| 35 | + ### Installation |
| 36 | +
|
| 37 | + Add `phoenix_test` to your list of dependencies in `mix.exs`: |
| 38 | +
|
| 39 | + ```elixir |
| 40 | + def deps do |
| 41 | + [ |
| 42 | + {:phoenix_test, "~> 0.2.12", only: :test, runtime: false} |
| 43 | + ] |
| 44 | + end |
| 45 | + ``` |
| 46 | +
|
| 47 | + ### Configuration |
| 48 | +
|
| 49 | + In `config/test.exs` specify the endpoint to be used for routing requests: |
| 50 | +
|
| 51 | + ```elixir |
| 52 | + config :phoenix_test, :endpoint, MyAppWeb.Endpoint |
| 53 | + ``` |
| 54 | +
|
| 55 | + ### Getting `PhoenixTest` helpers |
| 56 | +
|
| 57 | + `PhoenixTest` helpers can be included via `import PhoenixTest`. |
| 58 | +
|
| 59 | + But since each test needs a `conn` struct to get started, you'll likely want |
| 60 | + to set up a few things before that. |
| 61 | +
|
| 62 | + There are two ways to do that. |
| 63 | +
|
| 64 | + ### With `ConnCase` |
| 65 | +
|
| 66 | + If you plan to use `ConnCase` solely for `PhoenixTest`, then you can import |
| 67 | + the helpers there: |
| 68 | +
|
| 69 | + ```elixir |
| 70 | + using do |
| 71 | + quote do |
| 72 | + # importing other things for ConnCase |
| 73 | +
|
| 74 | + import PhoenixTest |
| 75 | +
|
| 76 | + # doing other setup for ConnCase |
| 77 | + end |
| 78 | + end |
| 79 | + ``` |
| 80 | +
|
| 81 | + ### Adding a `FeatureCase` |
| 82 | +
|
| 83 | + If you want to create your own `FeatureCase` helper module like `ConnCase`, |
| 84 | + you can copy the code below which can be `use`d from your tests (replace |
| 85 | + `MyApp` with your app's name): |
| 86 | +
|
| 87 | + ```elixir |
| 88 | + defmodule MyAppWeb.FeatureCase do |
| 89 | + use ExUnit.CaseTemplate |
| 90 | +
|
| 91 | + using do |
| 92 | + quote do |
| 93 | + use MyAppWeb, :verified_routes |
| 94 | +
|
| 95 | + import MyAppWeb.FeatureCase |
| 96 | +
|
| 97 | + import PhoenixTest |
| 98 | + end |
| 99 | + end |
| 100 | +
|
| 101 | + setup tags do |
| 102 | + pid = Ecto.Adapters.SQL.Sandbox.start_owner!(MyApp.Repo, shared: not tags[:async]) |
| 103 | + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) |
| 104 | +
|
| 105 | + {:ok, conn: Phoenix.ConnTest.build_conn()} |
| 106 | + end |
| 107 | + end |
| 108 | + ``` |
| 109 | +
|
| 110 | + Note that we assume your Phoenix project is using Ecto and its phenomenal |
| 111 | + `SQL.Sandbox`. If it doesn't, feel free to remove the `SQL.Sandbox` code |
| 112 | + above. |
| 113 | +
|
| 114 | + ## Usage |
| 115 | +
|
| 116 | + Now that we have all the setup out of the way, we can create tests like |
| 117 | + this: |
| 118 | +
|
| 119 | + ```elixir |
| 120 | + # test/my_app_web/features/admin_can_create_user_test.exs |
| 121 | +
|
| 122 | + defmodule MyAppWeb.AdminCanCreateUserTest do |
| 123 | + use MyAppWeb.FeatureCase, async: true |
| 124 | +
|
| 125 | + test "admin can create user", %{conn: conn} do |
17 | 126 | conn
|
18 | 127 | |> visit("/")
|
19 | 128 | |> click_link("Users")
|
20 |
| - |> fill_form("#user-form", name: "Aragorn", email: "aragorn@dunedan.com") |
| 129 | + |> fill_in("Name", with: "Aragorn") |
| 130 | + |> fill_in("Email", with: "aragorn@dunedain.com") |
21 | 131 | |> click_button("Create")
|
22 | 132 | |> assert_has(".user", text: "Aragorn")
|
23 | 133 | end
|
24 |
| - ``` |
| 134 | + end |
| 135 | + ``` |
| 136 | +
|
| 137 | + ### Filling out forms |
| 138 | +
|
| 139 | + We can fill out forms by targetting their inputs, selects, etc. by label: |
| 140 | +
|
| 141 | + ```elixir |
| 142 | + test "admin can create user", %{conn: conn} do |
| 143 | + conn |
| 144 | + |> visit("/") |
| 145 | + |> click_link("Users") |
| 146 | + |> fill_in("Name", with: "Aragorn") |
| 147 | + |> select("Elessar", from: "Aliases") |
| 148 | + |> choose("Human") # <- choose a radio option |
| 149 | + |> check("Ranger") # <- check a checkbox |
| 150 | + |> click_button("Create") |
| 151 | + |> assert_has(".user", text: "Aragorn") |
| 152 | + end |
| 153 | + ``` |
| 154 | +
|
| 155 | + For more info, see `fill_in/3`, `select/3`, `choose/2`, `check/2`, |
| 156 | + `uncheck/2`. |
| 157 | +
|
| 158 | + ### Submitting forms without clicking a button |
| 159 | +
|
| 160 | + Once we've filled out a form, you can click a button with |
| 161 | + `click_button/2` to submit the form. But sometimes you want to emulate what |
| 162 | + would happen by just pressing <Enter>. |
| 163 | +
|
| 164 | + For that case, you can use `submit/1` to submit the form you just filled |
| 165 | + out. |
| 166 | +
|
| 167 | + ```elixir |
| 168 | + session |
| 169 | + |> fill_in("Name", with: "Aragorn") |
| 170 | + |> check("Ranger") |
| 171 | + |> submit() |
| 172 | + ``` |
| 173 | +
|
| 174 | + For more info, see `submit/1`. |
| 175 | +
|
| 176 | + ### Targeting which form to fill out |
| 177 | +
|
| 178 | + If you find yourself in a situation where you have multiple forms with the |
| 179 | + same labels (even when those labels point to different inputs), then you |
| 180 | + might have to scope your form-filling. |
| 181 | +
|
| 182 | + To do that, you can scope all of the form helpers using `within/3`: |
| 183 | +
|
| 184 | + ```elixir |
| 185 | + session |
| 186 | + |> within("#user-form", fn session -> |
| 187 | + session |
| 188 | + |> fill_in("Name", with: "Aragorn") |
| 189 | + |> check("Ranger") |
| 190 | + |> click_button("Create") |
| 191 | + end) |
| 192 | + ``` |
25 | 193 |
|
26 |
| - Note that PhoenixTest does _not_ handle JavaScript. If you're looking for |
27 |
| - something that supports JavaScript, take a look at |
28 |
| - [Wallaby](https://hexdocs.pm/wallaby/readme.html). |
| 194 | + For more info, see `within/3`. |
29 | 195 | """
|
30 | 196 |
|
31 | 197 | import Phoenix.ConnTest
|
|
0 commit comments