phoenix_test
phoenix_test copied to clipboard
`click_button/2` seems to infer full text when trying to partial match and causes an error when the button contains elements between the text
When trying to target a button that has elements surrounding part of the text, click_button infers the full plain text of the button (based on the buttons already on the page it seems), but then LiveViewTest fails to match against that provided text.
Is this correct functionality of click_button?
Following the trail I can see that Button.build called in live.ex transforms the text, but I'm not sure to what end as LiveViewTest.element/3 already performs a partial text match:
def click_button(session, text) do
locator = Locators.button(text: text)
html = render_html(session)
#### IO.inspect(text) == "Showing"
button =
html
|> Query.find_by_role!(locator)
|> Button.build(html)
#### IO.inspect(button.text) == "Showing Active"
click_button(session, button.selector, button.text)
end
Modifying the call to click_button/3 to pass in the original text attribute fixes the error, but I am curious why we are modifying the provided filter. Perhaps for form submission?
Test body:
test "lists inactive containers", %{session: session} do
[container1, container2] = insert_pair(:container, status: :Inactive)
container3 = insert(:container)
session
|> visit(~p"/data/containers")
|> click_button("Showing")
|> assert_has("td", text: container1.unit_no)
|> assert_has("td", text: container2.unit_no)
|> refute_has("td", text: container3.unit_no)
end
Error:
1) test index lists inactive containers (ProjWeb.Feature.ContainerTest)
test/proj_web/features/container_test.exs:16
** (ArgumentError) selector "button" returned 2 elements but none matched the text filter "Showing Active":
<button class="leading-normal phx-submit-loading:opacity-75 hover:bg-zinc-200 leading-4 px-2 border border-black flex items-center gap-2 align-baseline py-1"><span class="hero-plus w-5 h-5"></span>New
</button>
<button class="leading-normal phx-submit-loading:opacity-75 hover:bg-zinc-200 leading-4 px-2 border border-black flex items-center gap-2 align-baseline py-1 markload" phx-click="toggle_active">
Showing <span class="text-sm px-0.5 bg-green-200 border border-solid border-green-700 text-green-950 leading-[0.9rem]">
Active
</span></button>
code: |> click_button("Showing")
stacktrace:
(phoenix_live_view 0.20.17) lib/phoenix_live_view/test/live_view_test.ex:1102: Phoenix.LiveViewTest.call/2
(phoenix_test 0.6.0) lib/phoenix_test/live.ex:84: PhoenixTest.Live.click_button/3
test/proj_web/features/container_test.exs:22: (test)