Suggestion/Discussion: Bring `open_browser` to controller tests

This past weekend I’ve been doing some work on a PR to bring open_browser to PhoenixTest. For those who don’t know it, PhoenixTest is a new library by @germsvel (introduced here) which provides a DSL that works for both LiveView tests and controller tests. It’s a great lib and I find its terse DSL to be the real value prop over the unification aspect, though that part is definitely very useful as well.

In any event, I’ve been able to get open_browser to work with controller tests. Of course, my PR still hasn’t been tested by anyone other than me so that combined with the fact this isn’t already in Phoenix has me thinking that maybe I’m missing something key. Admittedly, this post is also putting out some feelers to the core team about this.

Otherwise, could this be brought to Phoenix?

I do see how it would be a bit of a task to bring it over. Some code from LiveView would have to ported over to Phoenix then made available back to LiveView. The open_browser function itself would need a place to live. To me the most obvious place is Phoenix.ConnCase except that that will cause problems when importing LiveViewTest. Both are needed for LiveView tests so there would be a conflict open_browser function. The main problem here is explaining to users that they will have to use except: [open_browser: 1] so I don’t see that as a viable option. Of course, it could be moved to Phoenix.ConnCase and it could just know about LiveViews, but that’s likely also not ideal.

Anyway, that was a lot of speculation so I will I’ll just end this here.

What do you all think? How big a demand is there even for this feature? Perhaps it’s just something that should just live in PhoenixTest (if my PR gets through)?

4 Likes

This is an awesome idea. I wouldn’t sweat it about porting it to Phoenix just yet. I’m glad to see there’s appetite to add this feature to PhoenixTest so I’d focus on that so people can start using it and providing feedback. I’m sure the Phoenix team will figure out the best to integrate it, especially when it has been proven in a third party library by that time.

2 Likes

Right, this is definitely the way to go about it. Thanks!

This is now live in PhoenixTest. If anyone has controller tests and is interested in trying it out that would be much appreciated!

7 Likes

Hi @sodapopcan thank you! It looks like it works, if I understand it right, a ‘controller test’ is a test for a ‘dead view’ that includes a controller as well (otherwise there’s no page to render or no place for actions to respond to). This allows for a nice test-first workflow with some visibility into the page :slight_smile:

I cobbled together a sample based on the message explaining the PR. Basically took the test I wrote, assigned the session and put a call to open_browser in the middle. (The test currently fails, because the form doesn’t exist yet).

defmodule LivingsoftwareUkWeb.AdminCanCreateUserTest do
  use LivingsoftwareUkWeb.FeatureCase, async: true

  test "Prospect can initiate contact", %{conn: conn} do
   session = conn
    |> visit("/")
    |> click_link("Talk to us")

    open_browser(session)

   session
    |> fill_form("#contact-us-form", name: "Gandalf the great", email: "gandalf@example.com")

Result: I get a test failure (as expected #contact-us-form is missing) and I get to see the page as it is so far in the browser.

Only thing is Safari doesn’t load the styles and images. Chrome does. But that is not something the library can do something about I guess.

Great work, thank you very much!

Yep, that’s right!

Oh no that sounds like a bug, although I just switch my default browser to Safari and tried it locally with both a LiveView and controller test and saw styles in both cases. Do you mind logging an issue with your system info and all that? There isn’t any good reason it should fail in Safari :slight_smile:

Thank you! The lib is thanks to the great work of @germsvel!

1 Like

There isn’t a “good” reason indeed. It looked like all the paths were pointing to the correct locations. I’ll have another look today, and try it on another Mac as well.
And thanks to @germsvel indeed. I came for the asserts with more focused html snippets when they fail, and might be staying for open_browser and having the same style tests for live- and dead- views.

Love that people are using the library! And @sodapopcan is being modest. This open_browser was all his doing. And I love that it works with non-LiveView pages. It’s sooo cool to have a feature that isn’t available any other way!

2 Likes