PlaywrightKernelClient

Playwright\Symfony\Client\PlaywrightKernelClient

BrowserKit client that intercepts browser requests and routes them through Symfony's HttpKernel.
This is the core client for in-process E2E testing of Symfony applications. It combines a real
Playwright browser with Symfony's HttpKernel to enable full browser testing while maintaining
access to Symfony internals (services, events, profiler, request/response inspection).
Primary role in architecture:
- Returned by PlaywrightTestCase client factories
- Intercepts HTTP requests from the browser to configured hosts (localhost, 127.0.0.1)
- Routes intercepted requests through Symfony's kernel in the same PHP process
- Provides access to last Symfony request/response for assertions
- Integrates AssetServer for fast static asset serving (bypasses kernel)
How request interception works:
1. User calls visit('/path') → navigates browser to http://localhost/path
2. Browser makes HTTP request → intercepted via BrowserSessionInterface->setupRouting()
3. Request matched against interceptedHosts → if matched, proceeds to step 4
4. AssetServer checks if request is for static asset → if yes, serves directly
5. Otherwise: RequestConverter → HttpKernel->handle() → Kernel->terminate() → ResponseConverter → browser
6. Test can inspect via getLastSymfonyRequest(), getLastSymfonyResponse()
Key features:
- In-process kernel execution (no real HTTP, no separate server)
- Full access to Symfony container, services, and profiler
- beforeRequest() and afterResponse() hooks for custom logic
- Cookie management and authentication helpers
- BrowserKit API compatibility (visit, click, submit, getCrawler)
When to use:
- Testing your Symfony application with real browser behavior
- Need to inspect Symfony internals (request, response, services)
- Want fast E2E tests without network overhead
When NOT to use:
- Testing external websites → use BrowserKit\PlaywrightClient instead
- Need real HTTP networking behavior → use BrowserKit\PlaywrightClient instead

$ composer require playwright-php/playwright-symfony

Methods

__construct()

Creates a client that routes browser requests through a Symfony kernel.

function __construct( BrowserSessionInterface $browser, private readonly HttpKernelInterface $kernel, private readonly RequestConverter $requestConverter, private readonly ResponseConverter $responseConverter, array $server = [], ?array $interceptedHosts = null, ?object $hookReceiver = null, ?AssetServer $assetServer = null, private readonly string $baseUrl = 'http://localhost', private ?LoggerInterface $logger = null, private readonly bool $debugLogging = false, )

catchExceptions()

void

Controls whether exceptions thrown by the kernel are converted to responses.

function catchExceptions(bool $catchExceptions): void

visit()

PageInterface

Navigates the browser page to a path relative to the configured base URL.

function visit(string $path): PageInterface

followRedirect()

Crawler

Navigates to the target of the redirect the browser was stopped on.

function followRedirect(): Crawler

getPage()

?PageInterface

Returns the client's Playwright page, or null after its session is closed.

function getPage(): ?PageInterface

context()

?BrowserContextInterface

Returns the client's isolated browser context, or null after its session is closed.

function context(): ?BrowserContextInterface

click()

Crawler

Clicks a BrowserKit link in the live page and returns its updated crawler.

function click(Link $link, array $serverParameters = []): Crawler

submit()

Crawler

Fills and submits a BrowserKit form in the live page.

function submit(Form $form, array $values = [], array $serverParameters = []): Crawler

getCrawler()

Crawler

Returns a crawler built from the live page content.

function getCrawler(): Crawler

setCookie()

void

Adds or replaces a cookie in both Playwright and BrowserKit.

function setCookie(string $name, string $value, array $options = []): void

getCookie()

?string

Returns a browser cookie value for the given URL.

function getCookie(string $name, ?string $url = null): ?string

clearCookies()

void

Clears every cookie from Playwright and BrowserKit.

function clearCookies(): void

clearCookie()

void

Removes a cookie from Playwright and BrowserKit.

function clearCookie(string $name, ?string $domain = null, string $path = '/'): void

authenticate()

void

Sets the legacy AUTH cookie used by the package's simple authentication helper.

function authenticate(string $identifier = 'user', array $context = []): void

loginUser()

static

Stores a Symfony security token and synchronizes its session cookie to Playwright.

function loginUser(object $user, string $firewallContext = 'main', array $tokenAttributes = []): static

logout()

static

Clears the selected Symfony firewall token and its browser session cookie.

function logout(string $firewallContext = 'main'): static

getSession()

?SessionInterface

Returns the Symfony session associated with the browser's session cookie.

function getSession(): ?SessionInterface

getLastSymfonyRequest()

?SymfonyRequest

Returns the last Symfony request handled through browser interception.

function getLastSymfonyRequest(): ?SymfonyRequest

getRequest()

object

Returns the intercepted Symfony request when available, otherwise the BrowserKit request.

function getRequest(): object

getLastSymfonyResponse()

?SymfonyResponse

Returns the last Symfony response handled through browser interception.

function getLastSymfonyResponse(): ?SymfonyResponse

getContainer()

?ContainerInterface

Returns the kernel's test container when available, exposing private test services. Returns null when the configured HttpKernelInterface does not implement KernelInterface.

function getContainer(): ?ContainerInterface

enableProfiler()

void

Enable the profiler for the next request, mirroring KernelBrowser::enableProfiler().

function enableProfiler(): void

getResponse()

object

Returns the intercepted Symfony response when available, otherwise the BrowserKit response.

function getResponse(): object

getProfile()

?Profile

Returns the profile collected for the last profiled request.

function getProfile(): ?Profile

getBaseUrl()

string

Returns the base URL used to resolve relative navigation paths.

function getBaseUrl(): string

setInterceptedHosts()

void

Replaces the host names whose requests are routed through the Symfony kernel.

function setInterceptedHosts(array $hosts): void

getInterceptedHosts()

array

Returns the host names whose requests are routed through the Symfony kernel.

function getInterceptedHosts(): array