PhoenixLiveView.element does not find a text in an a-tag

Hi there,

in my liveview tests I have this assertion:

assert index_live
             |> element("#contract-#{contract.id} a", "delete #{contract.id}")
             |> render_click()

When I run my tests, I get this failing test, but the requested text is there:

** (ArgumentError) selector "#contract-5395 a" returned 3 elements but none matched the text filter "delete 5395":

...
         <a class="..." data-confirm="Are you sure?" href="#" id="delete 5395" phx-click="delete" phx-value-id="5395"><svg> ... </svg></a>

I tried to get other parts of the a tag, but failed too.

What am I doing wrong?

Thank you! I appreciate your help.

Best
Volker

The text filter is for matching text within the html element, not attributes – like, <a>text</a>.

Also just a heads up: Whitespaces are not allowed in the id attribute.

1 Like

Thank you for the two nudges.

The following assertion did the trick:

assert index_live
             |> element(~s{[id="delete-#{contract.id}"]})
             |> render_click()

For (my) future reference, the ~s sigil will escape the “” and the square brackets support the match for attributes like “ID”.