Connection (astra.connection)
Overview
Connection components manage the browser process and Playwright context. They are responsible for session locking, crash detection and page state monitoring.
BrowserController
- class BrowserController(session_path, headless=True)[source]
Controller for the Playwright browser engine.
This class ensures that only one instance of Astra uses a specific session directory and provides a stable interface for page interactions.
- async start()[source]
Launches the browser and navigates to WhatsApp Web.
- Returns:
The primary Playwright Page object.
- Raises:
BrowserLimitError – If another instance is using the same session.
- Return type:
playwright.async_api.Page
- async get_state()[source]
Detects the current state of WhatsApp Web (QR, Loading, Connected).
- Return type:
- async inject_local_storage(storage)[source]
Directly injects localStorage data into the active page.
- Parameters:
storage (dict)
- property context: playwright.async_api.BrowserContext
Provides direct access to the Playwright BrowserContext.
- property page: playwright.async_api.Page
Provides direct access to the Playwright Page object.
Page monitor & retry
- class PageMonitor(page)[source]
Monitors the browser page to detect authentication status and QR codes.
- Parameters:
page (playwright.async_api.Page)
Usage example
controller = BrowserController(session_path="./.sessions/default", headless=True)
page = await controller.start()
state = await controller.get_state()
Async notes
Browser start/stop operations are async and must be awaited.
Page interactions use Playwright’s async API (
await page.evaluate(...)).