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?
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:
I’m working through this same issue right now; testing an ordering function in LiveView where users can drag divs to re-order them.
To the best of my knowledge element/3 will throw if more than one item matches. It seems like there is no way to do this except for using something like Floki? If anyone has any other ides or suggestions I’d love to hear them.
I had started a PR for GitHub - mohamed-tallarium/Mononcle (which uses Floki to represent some higher-level assertions) that would take a tab-separated string to represent an expected table and compare it against the table in the DOM.
(Certainly I’ve used that technique in other environments to make the expected table state as clear as possible)
It also has a bunch of other useful functions for parsing HTML. It pairs nicely with Pages, a library that implements the page pattern for UI testing and works with controllers and LiveViews without the involvement of a web browser.
So I don’t know why I never thought of this before but out of the box you can use CSS selectors along with has_element?/3 for much more fine-grained and reliable results than my current regex solution: