PlaywrightEx.Route (PlaywrightEx v0.15.0)

Copy Markdown View Source

Resolve a request intercepted by PlaywrightEx.Page.route/4 or PlaywrightEx.BrowserContext.route/4.

Handles carry their connection and registration timeout. Operations return {:ok, result} or {:error, reason} and accept :connection and :timeout overrides. Each callback must resolve its handle once, before returning. Handles can only be used inside their callback process; returning without resolution is a callback failure.

Summary

Functions

Aborts the request. :error_code defaults to "failed" (Playwright network error code). Call inside the registered callback

Sends the request directly to the network. A matching page handler bypasses context routing when it continues a request.

Serves a response without contacting the network.

Types

t()

@opaque t()

Functions

abort(route, opts \\ [])

Aborts the request. :error_code defaults to "failed" (Playwright network error code). Call inside the registered callback:

{:ok, _} = PlaywrightEx.Route.abort(route, error_code: "connectionfailed")

continue(route, opts \\ [])

Sends the request directly to the network. A matching page handler bypasses context routing when it continues a request.

Accepts :url, :method, :headers (map or pairs), and binary :post_data. The URL must retain its original protocol. Browser restrictions on overriding headers (including Cookie) still apply. Headers carry over redirects; URL, method and body overrides apply only to the original request.

Examples

Forward the request unchanged from inside its callback:

{:ok, _} = PlaywrightEx.Route.continue(route)

To add a header, preserve the existing headers. Callback request metadata uses a list of %{name: name, value: value} entries; this operation accepts a map:

headers = Map.new(request.headers, fn header ->
  {String.downcase(header.name), header.value}
end)
{:ok, _} = PlaywrightEx.Route.continue(route,
  headers: Map.put(headers, "x-test-run", "true"))

fulfill(route, opts \\ [])

Serves a response without contacting the network.

Options: :status (default 200), :headers (map or list of name/value pairs), :content_type, and :body (binary, default empty). Binary bodies are encoded internally, including non-UTF-8 data. Header names are case insensitive; pair lists preserve duplicate names (for example, multiple Set-Cookie headers). Alternatively, supply :json (encoded with JSON.encode!/1, including nil and false) or :path (read from this Elixir host, including with remote browsers). These three body sources are mutually exclusive. JSON defaults to application/json. Files infer content type from their extension when the optional :mime dependency is installed; otherwise they use application/octet-stream (also the fallback for unknown extensions). Add {:mime, "~> 2.0"} to your dependencies to enable inference. Explicit Content-Type headers take precedence over defaults; :content_type overrides both. An unreadable file raises File.Error; unsupported JSON values raise an encoding error. Neither consumes the route handle.

:response is deferred. Protocol-only options such as :is_base64 are not accepted.

Examples

Choose one response per callback. These examples assume route is the handle received by the callback; its registration supplies connection and timeout.

JSON (also supports json: nil and json: false):

{:ok, _} = PlaywrightEx.Route.fulfill(route,
  status: 201, json: %{id: 123, name: "Ada"})

A local JavaScript fixture, with content type inferred from .js:

{:ok, _} = PlaywrightEx.Route.fulfill(route,
  path: "test/fixtures/formstack_stub.js")

An explicit binary response:

{:ok, _} = PlaywrightEx.Route.fulfill(route,
  content_type: "text/plain; charset=utf-8",
  headers: %{"x-test-response" => "stub"},
  body: "Hello from the test")