Aurel

Aurel

Testing (live)views with translations and accents

Hello there :waving_hand:

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 :person_shrugging:

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 :cry: :

** (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&#39;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

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

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

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.

Where Next?

Popular in Questions Top

Trevoke
I have an umbrella app. Some of the apps inside depend on other apps in the umbrella, unsurprisingly. I’m writing a test for one of the...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement