diff --git a/lib/phoenix_test/assertions.ex b/lib/phoenix_test/assertions.ex index bc371e16..fdc722e4 100644 --- a/lib/phoenix_test/assertions.ex +++ b/lib/phoenix_test/assertions.ex @@ -47,6 +47,13 @@ defmodule PhoenixTest.Assertions do #{format_found_element(element)} """ + + {:found_many, elements} -> + raise """ + Expected not to find an element. + + But found #{Enum.count(elements)} elements with selector #{inspect(selector)} and text #{inspect(text)}: + """ end end diff --git a/lib/phoenix_test/query.ex b/lib/phoenix_test/query.ex index 4e69502b..1624e0cf 100644 --- a/lib/phoenix_test/query.ex +++ b/lib/phoenix_test/query.ex @@ -38,6 +38,14 @@ defmodule PhoenixTest.Query do {:found, element} -> element + + {:found_many, _elements} -> + msg = + """ + Found more than one element with selector #{inspect(selector)} and text #{inspect(text)}. + """ + + raise ArgumentError, msg end end @@ -81,21 +89,10 @@ defmodule PhoenixTest.Query do {:found, found_element} -> found_element - end - end - defp find_with_text(html, selector, text) do - elements_matched_selector = - html - |> Html.all(selector) - - Enum.find(elements_matched_selector, :not_found, fn element -> - Html.text(element) =~ text - end) - |> then(fn - :not_found -> {:not_found, elements_matched_selector} - found -> {:found, found} - end) + {:found_many, [found_element | _]} -> + found_element + end end defp find_one_of(html, elements) do @@ -110,9 +107,22 @@ defmodule PhoenixTest.Query do |> Enum.find({:not_found, elements}, fn {:not_found, _} -> false {:found, _} -> true + {:found_many, _} -> true end) end + defp find_with_text(html, selector, text) do + elements_matched_selector = Html.all(html, selector) + + elements_matched_selector + |> Enum.filter(fn element -> Html.text(element) =~ text end) + |> case do + [] -> {:not_found, elements_matched_selector} + [found] -> {:found, found} + [_ | _] = found_many -> {:found_many, found_many} + end + end + defp first(html, selector) do html |> Html.all(selector) diff --git a/test/phoenix_test/assertions_test.exs b/test/phoenix_test/assertions_test.exs index c2a21cf7..c443babb 100644 --- a/test/phoenix_test/assertions_test.exs +++ b/test/phoenix_test/assertions_test.exs @@ -118,9 +118,7 @@ defmodule PhoenixTest.AssertionsTest do msg = """ Expected not to find an element. - But found an element with selector ".multiple_links" and text "Multiple links": - - with content "Multiple links" + But found 2 elements with selector ".multiple_links" and text "Multiple links": """ assert_raise RuntimeError, msg, fn -> diff --git a/test/phoenix_test/static_test.exs b/test/phoenix_test/static_test.exs index fb146b63..70879657 100644 --- a/test/phoenix_test/static_test.exs +++ b/test/phoenix_test/static_test.exs @@ -37,11 +37,12 @@ defmodule PhoenixTest.StaticTest do |> assert_has("h1", "Page 2") end - test "follows first link when there are multiple links with same text", %{conn: conn} do - conn - |> visit("/page/index") - |> click_link("Multiple links") - |> assert_has("h1", "Page 3") + test "raises error when there are multiple links with same text", %{conn: conn} do + assert_raise ArgumentError, ~r/Found more than one element with selector/, fn -> + conn + |> visit("/page/index") + |> click_link("Multiple links") + end end test "handles navigation to a LiveView", %{conn: conn} do