Wallaby testing when browser session is required (i.e. login)

@slouchpie thank you for your answer and advices, some later I’ll try playwright, your experience is encouraging

FWIW we’ve been using Playwright quite successfully as well! It’s especially nice if you’re already using PhoenixTest.

could you share a code example? I often see mention of such a solution, but I can’t imagine how to do it compactly enough.

In your router:


  if Mix.env() == :test do
    scope "/autologin", Web do
      pipe_through(:browser)
      get("/:account_id", AutologinTestController, :autologin)
    end
  end

And then in your test/support/autologin_test_controller.ex:

defmodule Web.AutologinTestController do
  @moduledoc "This controller is used for tests only and is compiled
  in MIX_ENV=test environment only. It allows to log in to accounts by
  visiting /autologin/:account_id, working around Oauth2/Google login."

  use Web, :controller

  def autologin(conn, %{"account_id" => account_id}) do
    account = DB.Repo.get!(DB.Account, account_id)
    Web.LogIn.login(conn, account)
  end
end

Adjust for your code structure, but that’s roughly it. A few lines.

Remember to wrap the route in the if clause and then also put the controller in test/support rather than usual location so it doesn’t get compiled in other environments.

The Web.Login.login/2 is the same function that’s used by actual controllers so you are sure it’s setting all the session variables the same way as production code.

2 Likes