Skip to content

Commit 2e92cbe

Browse files
committed
Support options 'label' and 'value' for assert_has/refute_has
1 parent 6181eb0 commit 2e92cbe

File tree

2 files changed

+75
-36
lines changed

2 files changed

+75
-36
lines changed

lib/phoenix_test/playwright.ex

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,6 @@ defmodule PhoenixTest.Playwright do
248248
conn
249249
end
250250
251-
def assert_has_value(conn, label, value, opts \\ []) do
252-
opts = Keyword.validate!(opts, exact: true)
253-
254-
assert_found(conn,
255-
selector: Selector.label(label, opts),
256-
expression: "to.have.value",
257-
expectedText: [%{string: value}]
258-
)
259-
end
260-
261251
def assert_has_selected(conn, label, value, opts \\ []) do
262252
opts = Keyword.validate!(opts, exact: true)
263253
@@ -583,7 +573,7 @@ defmodule PhoenixTest.Playwright do
583573
end
584574

585575
def assert_has(conn, selector, opts) do
586-
if !found?(conn, selector, opts) do
576+
if not found?(conn, selector, opts) do
587577
flunk("Could not find element #{selector} #{inspect(opts)}")
588578
end
589579

@@ -633,34 +623,55 @@ defmodule PhoenixTest.Playwright do
633623
conn
634624
|> maybe_within()
635625
|> Selector.concat(Selector.css(selector))
626+
|> Selector.and(Selector.label(opts[:label], exact: true))
636627
|> Selector.concat("visible=true")
637628
|> Selector.concat(Selector.text(opts[:text], opts))
638629

639-
if opts[:count] do
640-
if opts[:at],
641-
do: raise(ArgumentError, message: "Options `count` and `at` can not be used together.")
642-
643-
params =
644-
%{
645-
expression: "to.have.count",
646-
expected_number: opts[:count],
647-
selector: Selector.build(selector),
648-
timeout: timeout(opts)
649-
}
650-
651-
{:ok, found?} = Frame.expect(conn.frame_id, params)
652-
found?
653-
else
654-
params =
655-
%{
656-
selector: selector |> Selector.concat(Selector.at(opts[:at])) |> Selector.build(),
657-
timeout: timeout(opts)
658-
}
659-
660-
case Frame.wait_for_selector(conn.frame_id, params) do
661-
{:ok, _} -> true
662-
_ -> false
663-
end
630+
cond do
631+
opts[:value] ->
632+
if opts[:count],
633+
do: raise(ArgumentError, message: "Options `value` and `count` can not be used together.")
634+
635+
if opts[:text],
636+
do: raise(ArgumentError, message: "Options `value` and `text` can not be used together.")
637+
638+
params =
639+
%{
640+
expression: "to.have.value",
641+
expected_text: [%{string: opts[:value]}],
642+
selector: selector |> Selector.concat(Selector.at(opts[:at])) |> Selector.build(),
643+
timeout: timeout(opts)
644+
}
645+
646+
{:ok, found?} = Frame.expect(conn.frame_id, params)
647+
found?
648+
649+
opts[:count] ->
650+
if opts[:at],
651+
do: raise(ArgumentError, message: "Options `count` and `at` can not be used together.")
652+
653+
params =
654+
%{
655+
expression: "to.have.count",
656+
expected_number: opts[:count],
657+
selector: Selector.build(selector),
658+
timeout: timeout(opts)
659+
}
660+
661+
{:ok, found?} = Frame.expect(conn.frame_id, params)
662+
found?
663+
664+
true ->
665+
params =
666+
%{
667+
selector: selector |> Selector.concat(Selector.at(opts[:at])) |> Selector.build(),
668+
timeout: timeout(opts)
669+
}
670+
671+
case Frame.wait_for_selector(conn.frame_id, params) do
672+
{:ok, _} -> true
673+
_ -> false
674+
end
664675
end
665676
end
666677

test/phoenix_test/playwright_test.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,34 @@ defmodule PhoenixTest.PlaywrightTest do
589589
|> assert_has(".has_extra_space", text: "Has extra space")
590590
end
591591

592+
test "asserts input with label", %{conn: conn} do
593+
conn
594+
|> visit("/live/index")
595+
|> within("#email-form", fn conn ->
596+
assert_has(conn, "input", label: "Email")
597+
end)
598+
end
599+
600+
test "asserts input with value", %{conn: conn} do
601+
conn
602+
|> visit("/live/index")
603+
|> within("#email-form", fn conn ->
604+
conn
605+
|> fill_in("Email", with: "someone@example.com")
606+
|> assert_has("input", value: "someone@example.com")
607+
end)
608+
end
609+
610+
test "asserts input with label and value", %{conn: conn} do
611+
conn
612+
|> visit("/live/index")
613+
|> within("#email-form", fn conn ->
614+
conn
615+
|> fill_in("Email", with: "someone@example.com")
616+
|> assert_has("input", label: "Email", value: "someone@example.com")
617+
end)
618+
end
619+
592620
test "raises an error if the element cannot be found at all", %{conn: conn} do
593621
assert_raise AssertionError, ~r/Could not find element/, fn ->
594622
conn

0 commit comments

Comments
 (0)