BrowserRegistry
Playwright\Symfony\Client\BrowserRegistry
Manages a shared Playwright browser and the isolated sessions created from it.
This class acts as a factory and lifecycle manager for Playwright browser contexts and pages.
It handles browser startup, shutdown, session cleanup, and provides a centralized point for
browser configuration (browser type, headless mode, launch options).
Primary role in architecture:
- Used by PlaywrightTestCase to share a single browser instance across multiple tests
- Provides browser context and page instances to PlaywrightKernelClient
- Manages browser lifecycle hooks (start, stop, restartContext)
- Configures request routing for the kernel-based interception flow
Key responsibilities:
- Start/stop Playwright browsers (chromium, firefox, webkit)
- Create and manage browser contexts and pages
- Close contexts between tests for isolation while reusing the browser instance
- Set up routing callbacks for request interception via setupRouting()
- Read configuration from environment variables (PLAYWRIGHT_BROWSER, PLAYWRIGHT_HEADLESS)
Usage:
- Typically instantiated via BrowserRegistry::fromEnvironment() in PlaywrightTestCase
- Browser instance is shared across tests in the same test class (static $sharedBrowser)
- Sessions are closed between tests to ensure test isolation
This is NOT a browser itself - it's a registry/manager that creates and holds browser instances.
Methods
__construct()
Configures the browser process shared by sessions created from this registry.
function __construct( private readonly string $browserType = 'chromium', private readonly bool $headless = true, private readonly array $launchOptions = [], )
start()
voidStarts the registry's primary session if it has not been started yet.
function start(): void
equals()
boolWhether this registry uses the same browser configuration as another registry.
function equals(self $other): bool
stop()
voidCloses every session, the shared browser, and its Playwright client process.
function stop(): void
resetSessions()
voidCloses every managed session while keeping the shared browser reusable.
function resetSessions(): void
close()
voidCloses this registry and all resources it manages.
function close(): void
restartContext()
voidReplaces the primary session while keeping the shared browser running.
function restartContext(): void
getContext()
?BrowserContextInterfaceReturns the primary session's browser context, starting it when needed.
function getContext(): ?BrowserContextInterface
getPage()
?PageInterfaceReturns the primary session's page, starting it when needed.
function getPage(): ?PageInterface
setupRouting()
voidRegisters request routing on the primary session's page.
function setupRouting(callable $routeHandler): void
isHeadless()
boolWhether the browser is configured to run headless.
function isHeadless(): bool
getBrowserType()
stringReturns the configured browser engine name.
function getBrowserType(): string
fromEnvironment()
selfCreates a registry from PLAYWRIGHT_BROWSER and PLAYWRIGHT_HEADLESS.
function fromEnvironment(): self
createSession()
BrowserSessionInterfaceCreates an isolated context and page while reusing the browser process. The returned session is managed by this registry and remains open until closeSession(), resetSessions(), or stop() closes it.
function createSession(): BrowserSessionInterface
closeSession()
voidCloses a session created by this registry without stopping the shared browser.
function closeSession(BrowserSessionInterface $session): void