How to test and assert order of elements in LiveView?

I have a LiveView page that returns a list of students names and ids sorted in a table.

In test,
{:ok, index, html} = live(conn, Routes.students_path(conn, :index, class.id))

I can see that the html here has the names of the students sorted but there are other unnecessary contents(for my test in this case) in between these names. So how can I eliminate other contents and just test only the names are in the expected sort order?

Have you considered using Floki to parse your HTML output? I’m sure you’ll find your way with it.

In LiveViewTest helper is a function element/3 that you use to retrieve html elements from your view and then validate the elements. The documentation has some nice examples:

https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html#element/3

I would also recommend to keep the tests of views simple and check only that the important stuff is there.

Move the ordering of your students to a public function and test that function. This will make testing so much easier.