8000 test(browser): move browser scenarios to Fluffy by ftes · Pull Request #705 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,6 @@ jobs:
with:
otp-version: '29.1.2'
elixir-version: '1.20.5'
- uses: actions/setup-node@v7.1.0
with:
node-version: '26'
cache: npm
cache-dependency-path: assets/package-lock.json
# The compiled build restores only on an exact mix.lock match; see the
# test job's build cache.
- name: Cache browser job dependencies
Expand All @@ -238,7 +233,6 @@ jobs:
run: |
mix deps.get
mix assets.setup
npm ci --prefix assets
- name: Install Chromium and system dependencies
env:
PLAYWRIGHT_BROWSERS_PATH: ${{ github.workspace }}/_build/browser/browsers
Expand All @@ -247,7 +241,7 @@ jobs:
# wanted; when that mirror's index is inconsistent the whole step fails.
run: |
sudo grep -rl "dl.google.com" /etc/apt/sources.list.d | xargs -r sudo rm -f
assets/node_modules/.bin/playwright install --with-deps chromium --only-shell
mix playwright.install --with-deps chromium --only-shell
- name: Run browser scenarios
timeout-minutes: 2
run: mix test.browser
Expand All @@ -258,7 +252,7 @@ jobs:
name: browser-failure-diagnostics
path: |
_build/browser/screenshots
_build/browser/traces
_build/browser/test-results
if-no-files-found: ignore
retention-days: 7

Expand Down
41 changes: 0 additions & 41 deletions assets/package-lock.json

This file was deleted.

3 changes: 0 additions & 3 deletions assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@
"private": true,
"scripts": {
"test": "node --test js/*_test.mjs"
},
"devDependencies": {
"playwright": "1.65.1"
}
}
11 changes: 11 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ config :huddlz, :auth_rate_limits,
# Configure PhoenixTest
config :phoenix_test, :endpoint, HuddlzWeb.Endpoint

# Browser scenarios (mix test.browser). The endpoint's sandbox plug and
# AllowEctoSandbox read the sandbox metadata from the user agent.
config :fluffy, endpoint: HuddlzWeb.Endpoint, ecto_repos: [Huddlz.Repo]
config :fluffy, Fluffy.Sandbox, header: "user-agent"

config :playwright, :test,
timezone_id: "America/New_York",
screenshot: :only_on_failure,
trace: :retain_on_failure,
output_dir: "_build/browser/test-results"

# Disable swoosh api client in test
config :swoosh, :api_client, false

Expand Down
2 changes: 1 addition & 1 deletion lib/huddlz_web/hooks/allow_ecto_sandbox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ defmodule HuddlzWeb.Hooks.AllowEctoSandbox do
if connected?(socket), do: get_connect_info(socket, :user_agent)
end)

Sandbox.allow(metadata, Ecto.Adapters.SQL.Sandbox)
Sandbox.allow(metadata, Fluffy.Sandbox)
end
end
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ defmodule Huddlz.MixProject do
{:dns_cluster, "~> 0.3"},
{:bandit, "~> 1.5"},
{:corsica, "~> 2.1"},
{:cucumber, "~> 1.2", only: [:dev, :test]},
{:cucumber, github: "ftes/cucumber", branch: "playwright", only: [:dev, :test]},
{:mox, "~> 1.0", only: :test},
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:phoenix_test, "~> 0.12", only: :test},
{:phoenix_test_playwright, "~> 0.19.0", only: :test, runtime: false},
{:fluffy, "~> 0.9.0-rc.7", only: :test},
{:playwright, "~> 1.65.0-rc.6", hex: :playwright_ex, only: :test},
{:slugify, "~> 1.3"},
{:remote_ip, "~> 1.1"},
{:envious, "~> 1.0"}
Expand Down
10 changes: 5 additions & 5 deletions mix.lock

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions test/browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ One-time setup from the repository root:
```sh
MIX_ENV=test mix deps.get
MIX_ENV=test mix assets.setup
npm ci --prefix assets
PLAYWRIGHT_BROWSERS_PATH="$PWD/_build/browser/browsers" assets/node_modules/.bin/playwright install chromium --only-shell
MIX_ENV=test PLAYWRIGHT_BROWSERS_PATH="$PWD/_build/browser/browsers" mix playwright.install chromium --only-shell
```

On Linux, add `--with-deps` to the Playwright install command to install the required system libraries.
Expand Down Expand Up @@ -43,20 +42,20 @@ The command builds the actual app assets, starts the test endpoint and Chromium,
| Crop on upload | The crop sheet opens on choosing a picture, real pointer drags move it, and the upload is the 16:9 crop that was chosen; full-screen sheet at 320px |
| Password recovery | A reset finished in the same browser returns to the huddl where Sign in was chosen; the destination rides on the session cookie written during a LiveView redirect, which the fast suite does not keep |

Browser scenarios live in `features/`, their steps in `steps/`, and Cucumber hooks in `support/`. `browser_features_test.exs` supplies explicit feature/step/support globs to Cucumber. The existing fast root and its steps are loaded only by the normal test runner. No Cucumber fork or second library is needed. Use descriptive tags such as `@calendar_browser`; `@browser` conflicts with the adapter's browser-selection option.
Browser scenarios live in `features/`, their steps in `steps/`, and Cucumber hooks in `support/`. `browser_features_test.exs` supplies explicit feature/step/support globs and the case template to Cucumber. The existing fast root and its steps are loaded only by the normal test runner. Use descriptive tags such as `@calendar_browser`; `@browser` conflicts with the adapter's browser-selection option.

Keep this suite serial: its SQL sandbox ownership and global Mox stubs are shared with HTTP/LiveView processes. Do not add `@async` or extra workers without changing that fixture strategy. Each scenario gets a fresh browser context. Authentication uses a signed session cookie; apart from password recovery, these are not sign-in UI tests. Places/geocoding are deterministic stubs, with no external service credentials needed. The test endpoint serves the app over HTTP. Each browser context answers HTTPS requests locally so third-party services cannot delay page loads. Google Fonts requests receive the Inter fixture from `test/fixtures/fonts` to preserve layout measurements. Other HTTPS requests, including Maps embeds, receive an empty response. Browser contexts use `America/New_York`; `@mobile` selects a 320×720 viewport.

Browser setup uses the adapter's public `Case.do_setup_all/1` and `Case.do_setup/1` functions because Cucumber creates the ExUnit modules. The hook supplies a compatible module name for diagnostic filenames, preserving the scenario name. SQL sandbox HTTP metadata and the LiveView allowance hook are enabled only in test builds, before authentication queries.
Cucumber generates each feature's test module with `HuddlzWeb.BrowserCase` as its case template, which brings in `Playwright.Test` and owns the SQL sandbox. SQL sandbox HTTP metadata and the LiveView allowance hook are enabled only in test builds, before authentication queries.

## CI and diagnostics

The **Browser scenarios (Chromium)** job runs separately on every PR, including stacked PRs, and every push to `main`, alongside the fast tests. It installs the pinned Playwright version from `assets/package-lock.json`, Chromium headless shell, and Linux libraries. It uses one worker, a two-minute test-step timeout, and no automatic retries. Dependency compilation is cached; browser binaries and diagnostics are not part of that cache.
The **Browser scenarios (Chromium)** job runs separately on every PR, including stacked PRs, and every push to `main`, alongside the fast tests. It installs the Playwright driver that Fluffy pins, Chromium headless shell, and Linux libraries. It uses one worker, a two-minute test-step timeout, and no automatic retries. Dependency compilation is cached; browser binaries and diagnostics are not part of that cache.

Screenshots and traces are captured for every attempt under `_build/browser/screenshots` and `_build/browser/traces`. Failed CI runs upload these directories as `browser-failure-diagnostics` for seven days. Inspect the original failure before rerunning:
A failed scenario keeps a screenshot, its trace and `error-context.md` under `_build/browser/test-results`, one directory per scenario; passing scenarios keep none. Explicit screenshots go to `_build/browser/screenshots`. Failed CI runs upload these directories as `browser-failure-diagnostics` for seven days. Inspect the original failure before rerunning:

```sh
assets/node_modules/.bin/playwright show-trace _build/browser/traces/<trace.zip>
MIX_ENV=test mix playwright.cli show-trace _build/browser/test-results/<scenario>/trace.zip
```

Assertions wait for observable browser state instead of sleeping. When an interaction needs LiveView to have connected after navigation, use `assert_has("body .phx-connected")` before sending input. JavaScript used in steps observes layout, image loading, or native form submissions; it does not implement the application behavior. The failed-cover scenario intentionally requests a missing image, so its expected 404 is not a suite-wide console failure.
Steps use Fluffy's own locators, actions and assertions. They are strict: a locator that matches more than one element fails the step instead of acting on the first. Assertions wait for observable browser state instead of sleeping. `visit` waits for the LiveView to connect, so steps don't assert `.phx-connected` after it. JavaScript used in steps observes layout, image loading, or native form submissions; it does not implement the application behavior. The failed-cover scenario intentionally requests a missing image, so its expected 404 is not a suite-wide console failure.
4 changes: 3 additions & 1 deletion test/browser/browser_features_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Cucumber.compile_features!(
features: ["test/browser/features/**/*.feature"],
steps: ["test/browser/steps/**/*.exs"],
support: ["test/browser/support/**/*.exs"]
support: ["test/browser/support/**/*.exs"],
case_template: HuddlzWeb.BrowserCase,
step_wrapper: {HuddlzWeb.BrowserCase, :wrap}
)
78 changes: 37 additions & 41 deletions test/browser/steps/appearance_menu_steps.exs
Original file line number Diff line number Diff line change
@@ -1,65 +1,61 @@
defmodule BrowserAppearanceMenuSteps do
use Cucumber.StepDefinition
use Fluffy.Assert

import ExUnit.Assertions
import Huddlz.Test.BrowserHelpers
import PhoenixTest
import PhoenixTest.Playwright, only: [press: 3]
import Fluffy, except: [step: 3]
import Fluffy.Locator

@menu_open "document.querySelector('#theme-menu').matches(':popover-open')"
alias Fluffy.Playwright.Keyboard

step "I open the appearance menu with the keyboard", context do
conn =
context.conn
|> press("#theme-menu-trigger", "Enter")
|> assert_browser(@menu_open)
step "I open the appearance menu with the keyboard", %{session: session} do
session
|> press(trigger(), "Enter")
|> assert_has(open_menu())

Map.put(context, :conn, conn)
:ok
end

step "I pick {string} with the keyboard", %{args: [label]} = context do
conn = tab_to_option(context.conn, label)
Map.put(context, :conn, press(conn, ":focus", "Enter"))
step "I pick {string} with the keyboard", %{args: [label], session: session} do
session
|> tab_to_option(label)
|> Keyboard.press("Enter")

:ok
end

step "the page uses the dark appearance and the menu is closed", context do
conn =
assert_browser(
context.conn,
"document.documentElement.dataset.theme === 'dark' && !#{@menu_open}"
)
step "the page uses the dark appearance and the menu is closed", %{session: session} do
session
|> assert(attribute(by_css("html"), "data-theme", "dark"))
|> refute_has(open_menu())

Map.put(context, :conn, conn)
:ok
end

step "I press Escape", context do
Map.put(context, :conn, press(context.conn, ":focus", "Escape"))
step "I press Escape", %{session: session} do
Keyboard.press(session, "Escape")
:ok
end

step "the menu is closed and focus is back on its trigger", context do
conn =
context.conn
|> assert_browser("!#{@menu_open}")
|> assert_has("#theme-menu-trigger:focus")
step "the menu is closed and focus is back on its trigger", %{session: session} do
session
|> refute_has(open_menu())
|> assert(focused(trigger()))

Map.put(context, :conn, conn)
:ok
end

defp trigger, do: by_role(:button, name: ~r/^Appearance: /)
defp open_menu, do: by_css("#theme-menu:popover-open")

# Tab from the trigger into the menu, one option at a time, until the
# wanted one has focus.
defp tab_to_option(conn, label, tries \\ 4)

defp tab_to_option(_conn, label, 0), do: flunk("no appearance option labelled #{label}")

defp tab_to_option(conn, label, tries) do
conn = press(conn, ":focus", "Tab")
defp tab_to_option(session, label, tries \\ 4)

{:ok, focused} =
PlaywrightEx.Frame.evaluate(conn.frame_id,
expression: "document.activeElement.querySelector('.theme-option-label')?.textContent",
timeout: 5_000
)
defp tab_to_option(_session, label, 0), do: flunk("no appearance option labelled #{label}")

if focused == label, do: conn, else: tab_to_option(conn, label, tries - 1)
defp tab_to_option(session, label, tries) do
session = Keyboard.press(session, "Tab")
focused = text_content(session, by_css(":focus .theme-option-label"))
if focused == label, do: session, else: tab_to_option(session, label, tries - 1)
end
end
Loading
0