stevegrossi

stevegrossi

I’m using Wallaby (0.17.0) for acceptance tests with phantomjs (2.1.1), and the latest Elixir (1.4.5) and Erlang (20). I’ve written a few acceptance tests, each of which passes when run individually, but when I run them together (via mix test test/acceptance) they fail because things that should be on the page aren’t. It’s as if all the tests are running in the same browser, even though Wallaby should “manage multiple browsers for you.”

I believe I’ve set everything up according to the setup section of the README, although I’m not running the tests with async: true because my app uses sockets and Phoenix.Ecto.SQL.Sandbox doesn’t play well with async. To be clear, the tests also fail with async: true, I just also get DBConnection.OwnershipErrors in each when attempting to connect to the socket.

Is there anything else necessary to ensure each test is run in its own phantomjs browser, or to clear out sessions between test runs?

Showing Posts 1 to 5

stevegrossi

stevegrossi OP

I’ve posted this to StackOverflow as well in someone would prefer to answer there for the points :slight_smile:

josevalim

josevalim

Creator of Elixir

Can you reproduce it in a sample application? As long as you call Wallaby.start_session per setup and have async: false, you should be covered.

stevegrossi

stevegrossi OP

I’ll try to reproduce it in a sample application, but in the meantime will note that I was able to resolve the issue (and get all Wallaby tests passing when run together with mix test test/acceptance) by manually logging out (click(link("Log Out"))) at the end of each test case.

I store an authentication token in LocalStorage, and when I print it to the page (and take a screenshot in each test case), I can see that the token is the same for all of my test cases, even though I’m calling Wallaby.start_session before each test case. I would assume that, as a result of this PR, Wallaby sessions should not be sharing LocalStorage:

https://github.com/keathley/wallaby/pull/93

I’ll continue investigating…

stevegrossi

stevegrossi OP

I believe this may be a bug with Wallaby. When I IO.puts both the session.id and result of execute_script("return localStorage.getItem('token')" at the start of each test, it’s clear that each test is run in a differens phantomjs session (the ids differ) but that the token I’m setting with localStorage.setItem is persisting across sessions.

I’ve reproduced this behavior in a minimal sample app: GitHub - stevegrossi/wallaby_test: A test of how Wallaby handles localstorage across sessions · GitHub and have submitted an issue to Wallaby: localStorage persisting across sessions · Issue #274 · elixir-wallaby/wallaby · GitHub

ZaeemM.Yaseen

ZaeemM.Yaseen

@stevegrossi I am encountering the same error where if i run the E2E feature test alone they pass but fail if run in bulk (all together). After seeing all of the links you shared on differen platform i came to conclusion that it can be resolved by adding logout in each of the feature test case. But i have tried that and still getting the error. Also read in github that it was resolved but where i am doing wrong that it is failing for me.
below is the feature test case being run in a docker container

  feature "pagination is preserved after deleting", %{session: session} do
    # Create 15 two factor form detectors to ensure pagination
    two_factor_form_detectors =
      Enum.map(1..15, fn i ->
        {:ok, two_factor_form_detector} =
          TwoFactorFormDetectors.create_two_factor_form_detector(%{
            name: "Delete Pagination Test #{String.pad_leading("#{i}", 2, "0")}",
            input_fields_selector: "input[name='code']",
            submit_button_selector: "button[type='submit']",
            is_multi_input: false
          })

        two_factor_form_detector
      end)

    # Get a detector from page 2 (not the last one on the page)
    # With descending ID sort, page 2 has IDs 5,4,3,2,1. Use detector #4 (index 3)
    target_detector = Enum.at(two_factor_form_detectors, 3)
    first_page_detector_name = "Delete Pagination Test 15"

    session =
      session
      |> visit("/app/page1")
      |> wait_for_element(css("[data-testid='two-factor-form-detectors-table'], table"))

    # Navigate to page 2 - wait for pagination to be available first
    session =
      session
      |> wait_for_element(
        css("[data-testid='pagination-page-2'], div[phx-click='forward_to_page'][phx-value-page_number='2']")
      )
      |> click(css("[data-testid='pagination-page-2'], div[phx-click='forward_to_page'][phx-value-page_number='2']"))
      |> wait_for_page_load()
      |> wait_for_element(css("[data-testid='two-factor-form-detectors-table']"))
      |> assert_has(css("[data-testid='two-factor-form-detectors-table'] tbody tr", text: target_detector.name))

    # Delete a detector from page 2
    session =
      session
      |> execute_script("window.confirm = function() { return true; }")
      |> click(css("#two_factor_form_detector_actions_container_#{target_detector.id} button[aria-haspopup='true']"))
      |> assert_has(css("#delete_two_factor_form_detector_button_#{target_detector.id}"))
      |> click(css("#delete_two_factor_form_detector_button_#{target_detector.id}"))
      |> wait_for_flash_message()
      |> assert_page_contains("Two factor form detector deleted")
      |> wait_for_element(css("[data-testid='two-factor-form-detectors-table']"))

    # Verify we're still on page 2 (not reset to page 1)
    # The first page detector should not be visible, and we should still see other page 2 detectors
    # The deleted detector should not be visible anymore
    session
    |> wait_for_element(css("[data-testid='two-factor-form-detectors-table']"))
    |> wait_for_element_to_disappear(css("[data-testid='two-factor-form-detectors-table-row-#{target_detector.id}']"))
    |> refute_has(css("[data-testid='two-factor-form-detectors-table']", text: first_page_detector_name))
    |> assert_has(css("[data-testid='two-factor-form-detectors-table']", text: "Delete Pagination Test"))
  end

And its failing with the message

** (Wallaby.ExpectationNotMetError) Expected to find 1 visible element that matched the css ‘[data-testid=‘two-factor-form-detectors-table’] tbody tr’ and contained the text ‘Delete Pagination Test 04’, but 0 visible elements were found.

code: |> assert_has(css(“[data-testid=‘two-factor-form-detectors-table’] tbody tr”, text: target_detector.name))

Below is the setup

defmodule AppWeb.WallabyCase do
  use ExUnit.CaseTemplate

  alias Ecto.Adapters.SQL.Sandbox

  using do
    quote do
      use Wallaby.Feature

      import Ecto.Query
      import App.AccountsFixtures
      import AppWeb.WallabyCase

      # Tag all Wallaby-based tests as End-to-End so they can be included/excluded via --only/--exclude e2e
      @moduletag :e2e

      # The default endpoint for testing
      @endpoint AppWeb.Endpoint
    end
  end

  setup tags do
    # Clear Redis caches before each test to ensure isolation
    clear_redis_caches()

    pid = Sandbox.start_owner!(App.Repo, shared: not tags[:async])

    on_exit(fn ->
      Sandbox.stop_owner(pid)
      # Clear Redis again after test completes
      clear_redis_caches()
    end)

    metadata = Phoenix.Ecto.SQL.Sandbox.metadata_for(App.Repo, pid)

    # Start Wallaby session with debugging
    IO.puts("Starting Wallaby session...")
    {:ok, session} = Wallaby.start_session(metadata: metadata)
    IO.puts("Wallaby session started: #{inspect(session)}")

    # Try to get session info to verify it's working
    try do
      session_info = Wallaby.Browser.current_url(session)
      IO.puts("Session info retrieved: #{inspect(session_info)}")
    rescue
      e -> IO.puts("Error getting session info: #{inspect(e)}")
    end

    {:ok, session: session}
  end

  defp clear_redis_caches do
    # Clear all Redis-based caches to ensure test isolation
    try do
      App.SessionCache.flush()
    rescue
      _ -> :ok
    end

    try do
      App.RedisCounterCache.flush()
    rescue
      _ -> :ok
    end
  end
end
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews