zenstruck/browser
A Laravel-friendly browser testing toolkit built on Symfony BrowserKit and Panther. Easily crawl pages, click links, submit forms, assert on HTML, and drive real headless browsers—great for end-to-end tests and fluent, expressive UI assertions.
Symfony\Component\HttpKernel\HttpKernelInterface), requiring minimal setup (e.g., wrapping Laravel’s kernel in Symfony’s test client).symfony/panther), which Laravel can integrate via laravel/browser-kit-testing or standalone.BrowserExtension provides automatic test artifact capture (screenshots, source), which is valuable for Laravel’s debugging workflows.HttpTestCase with Symfony’s KernelTestCase.actingAs() can be mapped to Symfony’s actingAs() via a wrapper trait.route() helper may need adaptation for Symfony’s router (e.g., UrlGeneratorInterface).| Risk Area | Mitigation Strategy |
|---|---|
| Symfony Dependency | Use Symfony’s HTTP Kernel via Laravel’s app() container or a wrapper service. |
| Panther Overhead | Reserve Panther tests for critical JS paths; use KernelBrowser for API/non-JS. |
| Route/URL Conflicts | Standardize on Symfony’s router for consistency or create a Laravel-Symfony adapter. |
| Test Isolation | Leverage disableReboot()/enableReboot() to control Laravel’s service container state. |
| Artifact Storage | Configure BROWSER_SOURCE_DIR to point to Laravel’s storage/logs/browser/ or similar. |
TestKernel or service provider).Auth::login() or actingAs() integrate with Symfony’s actingAs()? Will a trait or service bridge the gap?HttpTestCase? Will it justify the switch?App\Exceptions\Handler) interact with Symfony’s error handling in tests?symfony/panther) for JS testing.BrowserTestCase to integrate with Zenstruck’s traits.HttpTestCase with a custom test case extending Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.use Zenstruck\Browser\Test\HasBrowser;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class BrowserTestCase extends KernelTestCase
{
use HasBrowser;
}
KernelBrowser (e.g., ->get('/api/endpoint')->assertJson()).symfony/panther and mtdowling/jmespath.php to composer.json.use Zenstruck\Browser\Test\HasBrowser;
use Symfony\Component\Panther\PantherTestCase;
class PantherTestCase extends PantherTestCase
{
use HasBrowser;
}
PantherBrowser (e.g., ->click('button')->assertSeeIn()).assertResponseOk()) with Zenstruck’s fluent syntax:
// Before (Laravel)
$response = $this->get('/posts');
$response->assertOk();
// After (Zenstruck)
$this->browser()->get('/posts')->assertSuccessful();
use() blocks for complex interactions (e.g., mocking services).| Component | Compatibility Notes |
|---|---|
| Laravel Routes | KernelBrowser uses Symfony’s router; ensure route names are consistent. |
| Middleware | Laravel middleware (e.g., ShareErrorsFromSession) must be kernel-aware. |
| Authentication | Laravel’s Auth facade may need a Symfony adapter (e.g., UserProviderInterface). |
| Blade Templates | KernelBrowser renders Blade via Symfony’s templating; no changes needed. |
| API Resources | Use assertJsonMatches() for JSON:API or GraphQL assertions. |
| File Uploads | PantherBrowser supports attachFile(); KernelBrowser requires multipart/form-data. |
reboot() for stateless tests.interceptRedirects() for form submissions.assert calls.KernelTestCase and PantherTestCase.How can I help you explore Laravel packages today?