aeat.adapters.outbound.aeat.browser._factory module

Default Playwright browser-session factory for auth providers.

Auth providers accept a adapters.outbound.aeat.auth.BrowserSessionFactory: an async callable that returns a adapters.outbound.aeat.auth.BrowserSessionLike. The default factory supplies that protocol with a Playwright-backed BrowserSession, while adapters.outbound.aeat.auth.select_provider() still accepts browser_session_factory=None so tests and callers can inject their own in-process implementations.

This module provides:

The factory path owns Playwright startup, optional browser extra checks, and best-effort teardown logging. Auth providers remain typed to the protocol, while this module carries the concrete runtime wiring.

class DefaultBrowserSession(playwright, session)[source]

Bases: object

Concrete BrowserSessionLike.

Auth providers depend on the protocol rather than on BrowserSession or Playwright directly. DefaultBrowserSession is the production adapter for that protocol: it owns one Playwright runtime, one BrowserSession, and a Profile through the wrapped session. Its close() method tears the pair down in order, so provider close() paths do not need a second runtime-specific hook.

Parameters:
property profile: Profile

The Profile associated with the underlying session.

async create_context(*, provisioner=None, storage_state_path=None, storage_state=None)[source]

Delegate context creation to the underlying BrowserSession.

Accepts the same keyword arguments as BrowserSession.create_context() and forwards them unchanged, so certificate provisioners and persisted Cl@ve storage state use the same path as callers that work with BrowserSession directly.

Return type:

BrowserContext

Parameters:
async navigate(page, url)[source]

Navigate through BrowserSession.navigate().

Return type:

Response | None

Parameters:
  • page (Page)

  • url (str)

async close()[source]

Close the session and stop the Playwright runtime.

Idempotent: subsequent calls are no-ops. BrowserSession.close() runs first; Playwright.stop() runs in the finally block so Playwright resources are released even when the session teardown raises.

Return type:

None

async default_browser_session_factory(settings)[source]

Start Playwright and return a wrapped DefaultBrowserSession.

The returned object satisfies adapters.outbound.aeat.auth.BrowserSessionLike and owns its Playwright runtime for the full lifetime. The Profile name follows the active bucket when one exists and falls back to a diagnostic sentinel so browser connectivity probes can run before profile setup is complete.

Auth providers pass their own kind-namespaced storage-state paths to BrowserSession.create_context(); the profile storage path built here is only the fallback for direct callers. Call await session.close() when you are done. Auth providers already do that in their close() path.

Return type:

DefaultBrowserSession

Parameters:

settings (Settings)

async create_browser_session(settings, profile)[source]

Start Playwright and return a DefaultBrowserSession.

Wraps a BrowserSession for profile after the optional browser extra has been checked by _start_playwright. If wrapper construction fails after Playwright starts, the partially opened runtime is stopped before the original failure is re-raised.

Return type:

DefaultBrowserSession

Parameters:
shared_playwright_runtime()[source]

Yield a centrally owned Playwright runtime for bulk browser workflows.

Callers that need several Profile-scoped contexts can start Playwright once here and pass the yielded runtime into opened_browser_page(). The context manager owns only the Playwright runtime; each page/context pair is still owned by the helper that opens it.

Return type:

AsyncIterator[Playwright]

opened_browser_page(playwright, settings, profile, *, provisioner=None, storage_state_path=None, storage_state=None)[source]

Yield a BrowserSession page/context pair and close both.

The helper builds a short-lived BrowserSession around the supplied Playwright runtime, forwards provisioner and storage-state arguments to BrowserSession.create_context(), yields the fresh (page, context) pair, and closes the context and browser session during teardown.

Return type:

AsyncIterator[tuple[Page, BrowserContext]]

Parameters: