Fluffy (Fluffy v0.6.2)

Copy Markdown View Source

Pipeable feature testing for Phoenix applications.

Start a session with the Phoenix (:phoenix) or Playwright (:playwright) backend, then compose locators, actions, expectations, and event waits. Import Fluffy.Expect for expectations, or use Fluffy.Assert for ExUnit-style assertions. The Phoenix backend selects its Static (Phoenix.ConnTest) or LiveView (Phoenix.LiveViewTest) driver for each page; the Playwright backend uses the Playwright driver.

Unless a function documents additional entries, action, assertion, reload, and event-wait option lists accept:

  • :timeout (non_neg_integer/0) - Maximum time in milliseconds to wait for the operation. Playwright browser bookkeeping and navigation synchronization use the session timeout separately.

Summary

Lifecycle and navigation

Closes a page. Handles selecting it must explicitly switch to another page before continuing.

Closes the given page handle, preserving the session's current-page selection.

Closes a session and its resources. Returns :ok or {:error, reason}.

Returns a live handle to the selected page.

Navigates backward in browser history. Does nothing if there is no entry.

Navigates forward in browser history. Does nothing if there is no entry.

Creates a blank page in the session's browser context and returns its handle.

Returns live handles for all open pages, including browser pages discovered without an event wait.

Reloads the active page's current document and returns the updated session.

Starts an isolated :phoenix or :playwright session.

Selects a page handle for this pipeline, without changing other session handles.

Actions

Drags the source locator onto the target locator in the same frame. Requires Playwright.

Fills a control with the literal string representation of value.

Hovers over a locator. Requires Playwright.

Presses Enter, Space, or Tab on one strict target.

Types text character by character, emitting keyboard events. Accepts :delay in milliseconds. Requires Playwright.

Selects options by value or label, or explicitly with %{value: value}, %{label: label}, or %{index: index}.

Selects local paths or in-memory Fluffy.FilePayload values for a file input, or clears its FileList with [].

Submits one form through its native submission path.

Events

Consumes a pending event and returns its value. Its deadline starts at registration.

Removes the most recently registered listener matching this source, event type, and handler.

Registers a persistent handler. Handlers run independently, linked to the registering caller.

Registers a handler for the first matching event, removing it before invocation.

Registers an event wait immediately. Await the pending handle once with await/1.

Diagnostics and native access

Opens a temporary HTML snapshot of the active page in your default browser.

Groups the operations in fun under a named diagnostic step.

Runs a driver-specific native operation and returns the reconciled session.

Lifecycle and navigation

close_page(session)

@spec close_page(Fluffy.Session.t()) :: Fluffy.Session.t()

Closes a page. Handles selecting it must explicitly switch to another page before continuing.

close_page(session, page)

@spec close_page(Fluffy.Session.t(), Fluffy.Page.t()) :: Fluffy.Session.t()

Closes the given page handle, preserving the session's current-page selection.

close_session(session)

Closes a session and its resources. Returns :ok or {:error, reason}.

current_page(session)

@spec current_page(Fluffy.Session.t()) :: Fluffy.Page.t()

Returns a live handle to the selected page.

go_back(session, options \\ [])

(Playwright only)

Navigates backward in browser history. Does nothing if there is no entry.

go_forward(session, options \\ [])

(Playwright only)

Navigates forward in browser history. Does nothing if there is no entry.

new_page(session)

(Playwright only)
@spec new_page(Fluffy.Session.t()) :: Fluffy.Page.t()

Creates a blank page in the session's browser context and returns its handle.

The session keeps its current page selection. Use switch_page/2 to select the new page. Pages share the context's cookies and storage.

main = current_page(session)
other = new_page(session)

session
|> switch_page(other)
|> visit("/dashboard")
|> switch_page(main)

pages(session)

@spec pages(Fluffy.Session.t()) :: [Fluffy.Page.t()]

Returns live handles for all open pages, including browser pages discovered without an event wait.

reload(session, options \\ [])

@spec reload(Fluffy.Session.t(), [action_option()]) :: Fluffy.Session.t()

Reloads the active page's current document and returns the updated session.

The Phoenix backend re-dispatches the current URL and selects the Static or LiveView driver for the returned page. The Playwright backend asks the active browser page to reload.

start_session(backend, options \\ [])

@spec start_session(backend(), [session_option()]) :: Fluffy.Session.t()

Starts an isolated :phoenix or :playwright session.

Configure the application endpoint once with config :fluffy, endpoint: MyAppWeb.Endpoint. The session base URL uses the endpoint's active HTTP or HTTPS listener when available and otherwise falls back to MyAppWeb.Endpoint.url(). An explicit :endpoint or :base_url option overrides the application configuration for this session.

Options

switch_page(session, page)

@spec switch_page(Fluffy.Session.t(), Fluffy.Page.t()) :: Fluffy.Session.t()

Selects a page handle for this pipeline, without changing other session handles.

visit(session, path)

Actions

blur(session, locator, options \\ [])

check(session, locator, options \\ [])

click(session, locator, options \\ [])

drag_to(session, source, target, options \\ [])

(Playwright only)

Drags the source locator onto the target locator in the same frame. Requires Playwright.

fill(session, locator, value, options \\ [])

Fills a control with the literal string representation of value.

Accepts any value implementing String.Chars, including numbers, dates, and custom structs. Conversion uses to_string/1, without HTML escaping.

focus(session, locator, options \\ [])

hover(session, locator, options \\ [])

(Playwright only)

Hovers over a locator. Requires Playwright.

press(session, locator, key, options \\ [])

Presses Enter, Space, or Tab on one strict target.

On a LiveView page, direct or window phx-keydown/phx-keyup bindings and phx-key filters are dispatched with their browser-shaped key and current value payload before and around the supported structural default action. Plain event names and push-only Phoenix.LiveView.JS bindings are portable; client-side JS commands, custom LiveSocket metadata, modifiers, key repeat, and timing assertions require Playwright.

press(locator, "Enter") means the browser's implicit Enter behavior. Use submit/2 when the intent is simply to submit a form.

press_sequentially(session, locator, text, options \\ [])

(Playwright only)

Types text character by character, emitting keyboard events. Accepts :delay in milliseconds. Requires Playwright.

select_option(session, locator, requested, options \\ [])

@spec select_option(Fluffy.Session.t(), Fluffy.Locator.t(), term(), [action_option()]) ::
  Fluffy.Session.t()

Selects options by value or label, or explicitly with %{value: value}, %{label: label}, or %{index: index}.

Values and labels accept String.Chars and are converted without HTML escaping. Indexes remain zero-based integers. A list requests multiple options; wrap a charlist in %{value: charlist} or %{label: charlist} to use it as one option.

set_input_files(session, locator_or_chooser, selection, options \\ [])

Selects local paths or in-memory Fluffy.FilePayload values for a file input, or clears its FileList with [].

One path or payload selects one file. A non-empty homogeneous list selects files in list order on an input with the multiple attribute. [] clears the current selection. The complete value is validated and snapshotted before the page changes. Static and LiveView drivers carry those snapshots into form submission or the supported managed-upload lifecycle; Playwright uses its native path or payload transport.

The default aggregate selection limit is configured with config :fluffy, file_input_max_bytes: 10_000_000. Override it for one action with :max_bytes. File contents are never included in size or validation errors.

Pass a chooser returned by Fluffy.Event.file_chooser/1 directly. It targets its original page and preserves the session's current-page selection. Choosers require Playwright. The returned session keeps this action pipeable:

pending = wait_for(session, Fluffy.Event.file_chooser())

session
|> click(by_role(:button, name: "Upload"))
|> set_input_files(await(pending), "test/fixtures/report.pdf")
|> click(by_role(:button, name: "Save"))

Register the wait before clicking the button. await/1 returns the chooser; set_input_files/4 fills its input and returns the session.

Options

  • :timeout (non_neg_integer/0) - Maximum time in milliseconds to wait for the operation. Playwright browser bookkeeping and navigation synchronization use the session timeout separately.

  • :max_bytes (non_neg_integer/0) - Maximum aggregate size of the selected files in bytes.

submit(session, locator, options \\ [])

Submits one form through its native submission path.

This is a semantic form action, not a synthetic keyboard event. Clicking a particular submit button remains the way to select a submitter and its name/value or override attributes.

Playwright follows browser-native constraint validation. Static and LiveView deliberately bypass it and submit the current structural form state; use Playwright when the test concerns invalid events, validity UI, focus, or browser-blocked submission.

uncheck(session, locator, options \\ [])

Events

await(pending)

@spec await(Fluffy.Event.Pending.t()) :: term()

Consumes a pending event and returns its value. Its deadline starts at registration.

off(session, event, handler, options \\ [])

Removes the most recently registered listener matching this source, event type, and handler.

on(session, event, handler, options \\ [])

Registers a persistent handler. Handlers run independently, linked to the registering caller.

once(session, event, handler, options \\ [])

Registers a handler for the first matching event, removing it before invocation.

wait_for(session, event, options \\ [])

Registers an event wait immediately. Await the pending handle once with await/1.

Diagnostics and native access

open_browser(session)

@spec open_browser(Fluffy.Session.t()) :: Fluffy.Session.t()

Opens a temporary HTML snapshot of the active page in your default browser.

Returns the unchanged session so it can be used in a pipeline. LiveView pages use Phoenix.LiveViewTest.open_browser/1. Static pages resolve local assets through the session endpoint; Playwright pages resolve assets against the page URL. Scripts are removed. This is an HTML snapshot, not an offline archive: browser resources must remain accessible, and DOM properties such as unsaved input values, canvas contents, and shadow roots are not serialized.

The temporary file remains available after the session closes.

session
|> visit("/chamber")
|> open_browser()

step(session, name, fun)

(macro)

Groups the operations in fun under a named diagnostic step.

Playwright sessions with tracing enabled record a nested trace group at the call site's source location. Phoenix sessions and untraced Playwright sessions simply run the callback. The callback must return the updated session, which is also returned by step/3.

unwrap(session, fun)

Runs a driver-specific native operation and returns the reconciled session.

Static callbacks receive the current Plug.Conn and must return an updated Plug.Conn. LiveView callbacks receive the current Phoenix.LiveViewTest.View, and Playwright callbacks receive a Fluffy.Playwright.Handle. Successful LiveView and Playwright callback return values are ignored.

Native operations are not cross-driver compatible. Prefer the shared Fluffy API whenever it covers the behavior under test.

Types

action_option()

@type action_option() :: {:timeout, non_neg_integer()}

backend()

@type backend() :: :phoenix | :playwright

file_input_option()

@type file_input_option() ::
  {:timeout, non_neg_integer()} | {:max_bytes, non_neg_integer()}

session_option()

@type session_option() ::
  {:endpoint, atom()}
  | {:base_url, binary()}
  | {:headers, [{atom() | binary(), binary()}]}
  | {:conn, struct()}
  | {:timeout, pos_integer()}
  | {:browser_context, keyword()}