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.

$ composer require playwright-php/playwright-symfony

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()

void

Starts the registry's primary session if it has not been started yet.

function start(): void

equals()

bool

Whether this registry uses the same browser configuration as another registry.

function equals(self $other): bool

stop()

void

Closes every session, the shared browser, and its Playwright client process.

function stop(): void

resetSessions()

void

Closes every managed session while keeping the shared browser reusable.

function resetSessions(): void

close()

void

Closes this registry and all resources it manages.

function close(): void

restartContext()

void

Replaces the primary session while keeping the shared browser running.

function restartContext(): void

getContext()

?BrowserContextInterface

Returns the primary session's browser context, starting it when needed.

function getContext(): ?BrowserContextInterface

getPage()

?PageInterface

Returns the primary session's page, starting it when needed.

function getPage(): ?PageInterface

setupRouting()

void

Registers request routing on the primary session's page.

function setupRouting(callable $routeHandler): void

isHeadless()

bool

Whether the browser is configured to run headless.

function isHeadless(): bool

getBrowserType()

string

Returns the configured browser engine name.

function getBrowserType(): string

fromEnvironment()

self

Creates a registry from PLAYWRIGHT_BROWSER and PLAYWRIGHT_HEADLESS.

function fromEnvironment(): self

createSession()

BrowserSessionInterface

Creates 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()

void

Closes a session created by this registry without stopping the shared browser.

function closeSession(BrowserSessionInterface $session): void