Services
Use Symfony's test container, isolated Playwright clients, and autowired browser context services.
The Symfony integration gives a browser test access to the same test container used by WebTestCase. Use services to prepare application state, then use the browser for the behavior under test.
Prepare state through the test container
PlaywrightTestCase extends Symfony's WebTestCase, so the standard test container is available:
use function Playwright\Testing\expect;
public function testAccountCanBeOpened(): void
{
$createAccount = static::getContainer()->get('app.test.create_account');
$createAccount('ada@example.com');
$page = $this->visit('/account');
expect($page->getByText('ada@example.com'))->toBeVisible();
}
The test container exposes private services through Symfony's normal test.service_container mechanism. Keep setup in a focused fixture or factory service so browser tests describe product behavior rather than persistence details.
The Playwright client exposes the same preferred container when client-oriented code needs it:
$container = static::getPlaywrightClient()->getContainer();
self::assertNotNull($container);
Primary and additional clients
The primary client is created lazily and reused inside the current test:
$primary = static::getPlaywrightClient();
$primary->visit('/account');
Create another client when a scenario needs an isolated browser context, such as two signed-in users:
$aliceUser = static::getContainer()->get('test.user.alice');
$bobUser = static::getContainer()->get('test.user.bob');
$alice = static::getPlaywrightClient();
$bob = static::createPlaywrightClient();
$alice->loginUser($aliceUser);
$bob->loginUser($bobUser);
$alicePage = $alice->visit('/messages');
$bobPage = $bob->visit('/messages');
The clients share the browser process and Symfony kernel. Their contexts, pages, cookies, and browser storage remain isolated.
Browser context services
Entries under playwright.browsers are registered as autowirable BrowserContextInterface services. The browser name maps to the constructor argument name:
playwright:
browsers:
default:
type: chromium
firefox_debug:
type: firefox
headless: false
use Playwright\Browser\BrowserContextInterface;
final class BrowserProbe
{
public function __construct(
private BrowserContextInterface $firefoxDebug,
) {
}
public function title(string $url): string
{
$page = $this->firefoxDebug->newPage();
$page->goto($url);
return $page->title();
}
}
The browser named by default_browser is also the default BrowserContextInterface alias. Named context services are useful inside autowired test-support services. PHPUnit test method parameters remain data-provider inputs, so retrieve services from the container or inject the browser into a support service.
Bundle services
| Service | Role |
|---|---|
BrowserContextInterface |
The configured default browser context. |
BrowserContextInterface $browserName |
A named browser context using camelCase argument naming. |
Playwright\Symfony\BrowserKit\PlaywrightClient |
A public BrowserKit-compatible client backed by the default context. |
Playwright\Symfony\Client\Interception\AssetServer |
The direct asset responder used by kernel clients. |
PlaywrightTestCase manages its own shared browser registry for test methods. The configured context services serve dependency-injected helpers and BrowserKit-style workflows; the test case API remains the shortest route for ordinary E2E tests.