Aurel
Testing (live)views with translations and accents
Hello there ![]()
I am facing an issue with a small phoenix app I am currently hacking.
As a frenchman, I am developing web apps for a French audience, and so I have to deal with all the diacritics (accents etc.) of the language.
Doing so, I faced some issues then testing my pages: there is probably a better way to handle them, but I could not figure it out ![]()
Issue 1: accents get escaped
For example, I cannot simply use the =~ operator to check the presence of some text in the HTML:
assert html =~ "S'inscrire" # đ Not working
assert html =~ dgettext("auth", "Register") # đ Neither
This is because the âapostropheâ is escaped as â'â in the final HTML.
I ended up adding a new function to my ConnCase
import Phoenix.HTML
# ...
@doc """
Call GetText *and escape* the result.
"""
def translation(domain \\ nil, key, bindings \\ %{}) do
if domain do
Gettext.dgettext(MaGardeWeb.Gettext, domain, key, bindings)
else
Gettext.gettext(MaGardeWeb.Gettext, key, bindings)
end
|> html_escape()
|> safe_to_string()
end
This way I can use a call to translation(...) in my tests in place of the Gettext functions whenever I need it : not ideal but better that looking up the escaped strings every time.
Is there an easier / better way to do this ?
Issue 2 : even when I use HTML escaping, I cannot select elements in LiveView tests
Once again, when I try to select the âSâinscrireâ button (means register in French), nothing seems to work:
lv |> element("main a", "S'inscrire") # đ Not working
lv |> element("main a", "S'inscrire") # đ Nope
lv |> element("main a", translation("auth", "Register")) # đ Still not good...
And when I look into the error that raised, it literally prints the escaped text content while complaining it cannot be found
:
** (ArgumentError) selector "main a" did not match text filter "S'inscrire", got:
<a href="/users/register" data-phx-link="redirect" data-phx-link-state="push"
class="font-semibold text-brand hover:underline">S'inscrire</a>
What did I do wrong ? and whatâs the right way to do this ?
Thanks a lot for any insight on these topics, since Iâm quite stuck right now.
Regards,
Aurélien
Most Liked
matt-savvy
For anyone finding this later:
I have a similar case, where I want to assert that a customerâs name is in the html somewhere, but customer names often have accent marks, etc.
# conn_case.ex or test utils
def escape(input) do
input
|> Phoenix.HTML.html_escape()
|> Phoenix.HTML.safe_to_string()
end
row = view |> element("##{customer.id}") |> render()
assert row =~ escape(customer.name)
Alternatively, you could use PhoenixTest which doesnât seem to have this issue
FlyingNoodle
Even for things like this I would avoid using the text because 2 days later some designer comes along and feels that âPlease click Sign In nowâ is much better and then you have to change your test.
You do you, but my experience has been to avoid using text to assert or as selectors.
FlyingNoodle
Only tangential, but I very rarely (or never?) test against actual text on the webpage because that is just so brittle.
Instead, I would assert that a button exists or that some element has been added or removed after some action.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









