angelikatyborska

angelikatyborska

In a LiveView test, how can I get the full page HTML after the initial render?

In a LiveView test, how can I get the full page HTML after the initial render? The Phoenix.LiveViewTest.html#live/2 function returns the full page HTML, but then after some updates happen and I pass the Phoenix.LiveViewTest.View struct to Phoenix.LiveViewTest.html#render/1, I only get the HTML of this live view, no layouts.

Here’s a code snippet to show what I mean:

test "deletes user in listing", %{conn: conn, user: user} do
  {:ok, index_live, html} = live(conn, ~p"/users")

  # includes full page HTML, with all the layouts
  dbg(html)

  assert index_live |> element("#users-#{user.id} a", "Delete") |> render_click()

  # only includes this live view's HTML, no layouts - how can I get the full page HTML here, after the update?
  dbg(render(index_live))
end

I would like the full page HTML after updates so that I could send it to an HTML validator, which unfortunately only supports validating full documents, not fragments. But even if it supported validating fragments, there can still be HTML bugs that cannot be caught when validating a fragment only, like invalid tag nesting or referencing not existent elements in for and aria-labelledby and similar attributes, so having the full document available would be ideal.

First 3 of 3 Posts Switch mode

josevalim

josevalim

Creator of Elixir

Take a look at open_browser. I believe it writes the whole document to disk. It may not work out of the box but it may provide some pointers. :slight_smile:

angelikatyborska

angelikatyborska OP

Thank you! I got something like this working based on your hint:

test "deletes user in listing", %{conn: conn, user: user} do
  {:ok, index_live, html} = live(conn, ~p"/users")

  # includes full page HTML, with all the layouts
  dbg(html)

  assert index_live |> element("#users-#{user.id} a", "Delete") |> render_click()

  {_ref, _topic, pid} = index_live.proxy
  {:ok, {parsed_html, _static_assets_path}} = GenServer.call(pid, :html, 30_000)

  # includes full page HTML, with all the layouts
  html_after_update = Floki.raw_html(parsed_html)
end

I see that the proxy genserver Phoenix.LiveViewTest.ClientProxy is not publicly documented. I assume that means its API could change at any time, so I would be using that code at my own risk? I was looking not only to validate my own HTML, but also to recommend to other people how they can do the same :sweat_smile:

josevalim

josevalim

Creator of Elixir

Correct. The next step would be proposing a public API to retrieve this, so you don’t rely on internals. :slight_smile:

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement