Jido.Browser.Adapter behaviour (Jido Browser v2.1.0)

Copy Markdown View Source

Behaviour for browser automation adapters.

Adapters implement the low-level browser control protocol, allowing Jido.Browser to work with different browser automation backends.

Return Value Contract

All operations (except start_session and end_session) return a consistent 3-tuple on success:

{:ok, session, result_map}

This ensures:

  • Callers always receive the potentially-updated session
  • Consistent API regardless of whether the operation modifies state
  • Session can be threaded through operation chains

Implementing an Adapter

defmodule MyAdapter do
  @behaviour Jido.Browser.Adapter

  @impl true
  def start_session(opts) do
    # Start browser, return {:ok, session} or {:error, reason}
  end

  @impl true
  def end_session(session) do
    # Clean up resources, return :ok or {:error, reason}
  end

  @impl true
  def navigate(session, url, opts) do
    # Navigate and return session + result
    {:ok, updated_session, %{url: url}}
  end

  @impl true
  def screenshot(session, opts) do
    # Even read-only ops return the session for consistency
    {:ok, session, %{bytes: png_data, mime: "image/png"}}
  end

  # ... implement other callbacks
end

Optional Callbacks

  • evaluate/3 - JavaScript evaluation (not all backends support this)
  • pooling is modeled separately through Jido.Browser.PoolAdapter

Built-in Adapters

Summary

Callbacks

Clicks an element matching the selector.

Executes adapter-native operations that are not part of the base behaviour.

Ends a browser session and cleans up resources.

Executes JavaScript in the browser context.

Extracts content from the current page.

Navigates to a URL.

Takes a screenshot of the current page.

Starts a new browser session.

Types text into an element matching the selector.

Callbacks

click(session, selector, opts)

@callback click(
  session :: Jido.Browser.Session.t(),
  selector :: String.t(),
  opts :: keyword()
) ::
  {:ok, Jido.Browser.Session.t(), map()} | {:error, term()}

Clicks an element matching the selector.

Returns the updated session plus a result map.

command(session, action, opts)

(optional)
@callback command(
  session :: Jido.Browser.Session.t(),
  action :: atom(),
  opts :: keyword()
) ::
  {:ok, Jido.Browser.Session.t(), map()} | {:error, term()}

Executes adapter-native operations that are not part of the base behaviour.

end_session(session)

@callback end_session(session :: Jido.Browser.Session.t()) :: :ok | {:error, term()}

Ends a browser session and cleans up resources.

evaluate(session, script, opts)

(optional)
@callback evaluate(
  session :: Jido.Browser.Session.t(),
  script :: String.t(),
  opts :: keyword()
) ::
  {:ok, Jido.Browser.Session.t(), %{result: term()}} | {:error, term()}

Executes JavaScript in the browser context.

extract_content(session, opts)

@callback extract_content(session :: Jido.Browser.Session.t(), opts :: keyword()) ::
  {:ok, Jido.Browser.Session.t(), %{content: String.t(), format: atom()}}
  | {:error, term()}

Extracts content from the current page.

navigate(session, url, opts)

@callback navigate(
  session :: Jido.Browser.Session.t(),
  url :: String.t(),
  opts :: keyword()
) ::
  {:ok, Jido.Browser.Session.t(), map()} | {:error, term()}

Navigates to a URL.

Returns the updated session with current_url set, plus a result map.

screenshot(session, opts)

@callback screenshot(session :: Jido.Browser.Session.t(), opts :: keyword()) ::
  {:ok, Jido.Browser.Session.t(), %{bytes: binary(), mime: String.t()}}
  | {:error, term()}

Takes a screenshot of the current page.

start_session(opts)

@callback start_session(opts :: keyword()) :: Jido.Browser.Session.t() | {:error, term()}

Starts a new browser session.

type(session, selector, text, opts)

@callback type(
  session :: Jido.Browser.Session.t(),
  selector :: String.t(),
  text :: String.t(),
  opts :: keyword()
) :: {:ok, Jido.Browser.Session.t(), map()} | {:error, term()}

Types text into an element matching the selector.

Returns the updated session plus a result map.