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.

1 Like

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:

1 Like

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:

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

1 Like