Cerberus.Browser (cerberus v0.1.7)

Copy Markdown View Source

Browser-only extensions for richer real-browser workflows.

These helpers are intentionally scoped to browser sessions.

Summary

Functions

Adds a cookie to the active browser context.

Adds cookies to the active browser context.

Adds a Phoenix Plug.Session cookie to the active browser context.

Asserts that a dialog matching the text locator was observed.

Removes all cookies from the active browser context.

Returns the cookie by name or nil when not present.

Passes the cookie by name (or nil) to callback and returns session.

Returns all browser cookies visible to the active page.

Passes all browser cookies visible to the active page to callback and returns session.

Drags from source_selector to target_selector.

Evaluates JavaScript and ignores the result, returning the original session.

Evaluates JavaScript and controls result handling.

Presses a keyboard key on a matched element.

Captures a browser screenshot.

Returns the session cookie (commonly _app_key) when present.

Passes the session cookie (or nil) to callback and returns session.

Types text into a matched element.

Returns encoded SQL sandbox metadata for the configured Cerberus Ecto repos.

Returns encoded SQL sandbox metadata for browser user_agent session wiring.

Runs a popup flow, yielding both main and popup sessions to callback_fun.

Types

cookie()

@type cookie() :: %{
  name: String.t() | nil,
  value: term(),
  domain: String.t() | nil,
  path: String.t() | nil,
  http_only: boolean(),
  secure: boolean(),
  same_site: String.t() | nil,
  session: boolean()
}

Functions

add_cookie(session, name, value, opts \\ [])

@spec add_cookie(
  session,
  String.t(),
  String.t(),
  Cerberus.Options.browser_add_cookie_opts()
) :: session
when session: var

Adds a cookie to the active browser context.

Options

  • :domain (term/0) - Cookie domain override. The default value is nil.

  • :path (term/0) - Cookie path override. The default value is "/".

  • :http_only (boolean/0) - Marks cookie as HttpOnly. The default value is false.

  • :secure (boolean/0) - Marks cookie as Secure. The default value is false.

  • :same_site (term/0) - Cookie sameSite policy (:strict, :lax, :none). The default value is :lax.

add_cookies(session, cookies)

@spec add_cookies(session, [Cerberus.Options.browser_cookie_arg()]) :: session
when session: var

Adds cookies to the active browser context.

This follows Playwright's bulk cookie shape: each cookie is a keyword list with :name, :value, and optional :url or :domain/:path fields. When :url/:domain are omitted, Cerberus falls back to the browser session base URL.

add_session_cookie(session, cookie, session_options)

@spec add_session_cookie(
  session,
  Cerberus.Options.browser_session_cookie_arg(),
  keyword()
) :: session
when session: var

Adds a Phoenix Plug.Session cookie to the active browser context.

session_options must match the options used by plug Plug.Session in your endpoint or router. For example: MyAppWeb.Endpoint.session_options().

assert_dialog(session, locator, opts \\ [])

@spec assert_dialog(
  session,
  Cerberus.Locator.t(),
  Cerberus.Options.browser_assert_dialog_opts()
) ::
  session
when session: var

Asserts that a dialog matching the text locator was observed.

assert_dialog/3 matches dialog text from the active dialog or recently observed dialog events, then auto-accepts the observed dialog. Prompt dialogs are auto-accepted with an empty string input.

Options

  • :timeout (pos_integer/0) - Wait timeout in milliseconds for dialog lifecycle events.

  • :browser (keyword/0) - Per-call browser config overrides used for timeout defaults. The default value is [].

clear_cookies(session, opts \\ [])

@spec clear_cookies(session, Cerberus.Options.browser_clear_cookies_opts()) :: session
when session: var

Removes all cookies from the active browser context.

Options

  • :timeout (non_neg_integer/0) - Maximum wait time in milliseconds. The default value is 0.

cookie(session, name)

@spec cookie(Cerberus.Session.t(), String.t()) :: cookie() | nil

Returns the cookie by name or nil when not present.

cookie(session, name, callback)

@spec cookie(session, String.t(), (cookie() | nil -> term())) :: session
when session: var

Passes the cookie by name (or nil) to callback and returns session.

cookies(session)

@spec cookies(Cerberus.Session.t()) :: [cookie()]

Returns all browser cookies visible to the active page.

cookies(session, callback)

@spec cookies(session, ([cookie()] -> term())) :: session when session: var

Passes all browser cookies visible to the active page to callback and returns session.

drag(session, source_selector, target_selector, opts \\ [])

@spec drag(session, String.t(), String.t(), Cerberus.Options.browser_drag_opts()) ::
  session
when session: var

Drags from source_selector to target_selector.

Options

evaluate_js(session, expression)

@spec evaluate_js(Cerberus.Session.t(), String.t()) :: Cerberus.Session.t()

Evaluates JavaScript and ignores the result, returning the original session.

evaluate_js(session, expression, callback_or_opts)

Evaluates JavaScript and controls result handling.

Use either:

  • callback form (evaluate_js(session, expression, fn value -> ... end)) to keep piping
  • return_result: true (evaluate_js(session, expression, return_result: true)) to return the JS value

Options

  • :return_result (boolean/0) - When true, returns the computed value instead of the session. The default value is false.

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

@spec press(
  session,
  Cerberus.Locator.t(),
  String.t(),
  Cerberus.Options.browser_press_opts()
) :: session
when session: var

Presses a keyboard key on a matched element.

Options

screenshot(session, opts \\ [])

@spec screenshot(session, String.t() | Cerberus.Options.screenshot_opts()) ::
  session | binary()
when session: var

Captures a browser screenshot.

opts_or_path accepts either a path string or keyword options.

Use either:

  • callback form (screenshot(session, opts, fn png_binary -> ... end)) to keep piping
  • return_result: true to return the PNG binary

open: true opens the saved screenshot path in the default system image viewer.

Options

  • :path (term/0) - Optional file path for the screenshot output. The default value is nil.

  • :full_page (boolean/0) - Captures the full document instead of only the viewport.

  • :open (boolean/0) - Opens the saved screenshot with the default system viewer. The default value is false.

  • :return_result (boolean/0) - Returns the screenshot PNG binary instead of the session. The default value is false.

screenshot(session, path, callback)

@spec screenshot(
  session,
  String.t() | Cerberus.Options.screenshot_opts(),
  (binary() -> term())
) ::
  session
when session: var

session_cookie(session)

@spec session_cookie(Cerberus.Session.t()) :: cookie() | nil

Returns the session cookie (commonly _app_key) when present.

session_cookie(session, callback)

@spec session_cookie(session, (cookie() | nil -> term())) :: session when session: var

Passes the session cookie (or nil) to callback and returns session.

type(session, locator, text, opts \\ [])

@spec type(
  session,
  Cerberus.Locator.t(),
  String.t(),
  Cerberus.Options.browser_type_opts()
) :: session
when session: var

Types text into a matched element.

Options

  • :clear (boolean/0) - Clears the field before typing. The default value is false.

  • :timeout (non_neg_integer/0) - Browser action timeout in milliseconds.

user_agent_for_sandbox(context)

@spec user_agent_for_sandbox(map()) :: String.t()

Returns encoded SQL sandbox metadata for the configured Cerberus Ecto repos.

user_agent_for_sandbox(repo, context)

@spec user_agent_for_sandbox(module() | [module()], map()) :: String.t()

Returns encoded SQL sandbox metadata for browser user_agent session wiring.

This helper is intended for browser-session sandbox wiring:

import Cerberus
import Cerberus.Browser

metadata = user_agent_for_sandbox(MyApp.Repo, context)
session(:browser, user_agent: metadata)

with_popup(session, trigger_fun, callback_fun, opts \\ [])

@spec with_popup(
  session,
  (session -> term()),
  (session, session -> term()),
  Cerberus.Options.browser_with_popup_opts()
) :: session
when session: var

Runs a popup flow, yielding both main and popup sessions to callback_fun.

Options

  • :timeout (pos_integer/0) - Wait timeout in milliseconds for popup capture. The default value is 1500.