PlaywrightTestCase

Playwright\Symfony\Test\PlaywrightTestCase

Base test case for E2E testing Symfony applications with Playwright and in-process kernel routing.
This class provides a complete testing environment that combines:
- Real Playwright browser (chromium/firefox/webkit) for authentic browser behavior
- Symfony HttpKernel integration for in-process request handling
- Access to Symfony internals (container, services, profiler, request/response)
- BrowserKit-compatible API for familiar testing patterns
Architecture overview:
- Extends WebTestCase → uses Symfony's functional-test lifecycle and test container
- Lazily creates a shared BrowserRegistry → manages browser lifecycle across tests
- Creates PlaywrightKernelClient instances → intercepts requests and routes through kernel
- Reuses one browser process for isolated client contexts
Key features:
- Browser sharing: One browser instance per test class (performance optimization)
- Context isolation: Client contexts are closed after each test
- Request interception: All requests to localhost/127.0.0.1 routed through kernel
- Asset optimization: Static assets served directly without kernel overhead
- Hook system: beforeRequest() and afterResponse() for custom logic
How request flow works:
1. Test calls visit('/login') or creates a Playwright client
2. Browser navigates to http://localhost/login
3. Request intercepted by PlaywrightKernelClient
4. AssetServer checks if it's a static asset → serves directly if yes
5. Otherwise: RequestConverter → HttpKernel->handle() → ResponseConverter
6. Response fulfilled in browser → page renders with full JS/CSS
7. Test can inspect: getLastRequest(), getLastResponse(), getProfile()
Configuration:
- Reads from bundle parameters: playwright.intercepted_hosts, playwright.base_url
- Reads from environment: PLAYWRIGHT_BROWSER, PLAYWRIGHT_HEADLESS
- Can configure via kernel container parameters
Common methods:
- visit(string $path): PageInterface → Navigate to path, returns Playwright page
- getPlaywrightClient() → primary PlaywrightKernelClient, created lazily
- createPlaywrightClient() → fresh client with an isolated context and page
- setCookie(), authenticate(), logout() → Helpers for auth testing
- getLastRequest(), getLastResponse() → Inspect intercepted Symfony objects
- beforeRequest(), afterResponse() → Override for custom hooks
Example usage:
```php
class LoginTest extends PlaywrightTestCase
{
protected static function createKernel(array $options = []): KernelInterface
{
return new Kernel('test', false);
}
public function testUserCanLogin(): void
{
$page = $this->visit('/login');
$page->fill('#username', 'admin');
$page->fill('#password', 'secret');
$page->click('button[type="submit"]');
$this->assertPageContains('Welcome back');
$response = $this->getLastResponse();
$this->assertSame(200, $response->getStatusCode());
}
}
```
Requirements:
- Playwright browsers installed via: vendor/bin/playwright-install --browsers

$ composer require playwright-php/playwright-symfony

Methods

tearDownAfterClass()

void
function tearDownAfterClass(): void

getBaseUrl()

string

Returns the base URL configured for browser navigation.

function getBaseUrl(): string

beforeRequest()

void

Runs before each intercepted request is passed to the Symfony kernel.

function beforeRequest(SymfonyRequest $request): void

afterResponse()

void

Runs after each intercepted response is returned by the Symfony kernel.

function afterResponse(SymfonyResponse $response): void