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.
HttpKernel, BrowserKit, DomCrawler).HttpTestCase.HttpKernel (Laravel’s core uses this).TestCase).HttpTestCase already uses BrowserKit/DomCrawler under the hood.zenstruck/browser.assertSee() with assertSeeIn('h1', 'Title') for more granular control.symfony/panther), which is not in Laravel’s default stack.BrowserKit, DomCrawler, Panther).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Symfony Dependency | High | Isolate in tests/ composer.json or use symfony/browser-kit directly. |
| Panther Setup | Medium | Only adopt if JS testing is critical; otherwise, avoid. |
| Test Flakiness | Medium | KernelBrowser may behave differently than Laravel’s native assertions. |
| Maintenance Overhead | Low | MIT license, active repo, but Laravel-specific quirks may arise. |
| Performance | Medium | PantherBrowser adds significant overhead; KernelBrowser adds minimal. |
HttpTestCase or Dusk (for JS)?
zenstruck/browser provides a more fluent API than Laravel’s.assertSee() vs. assertSeeIn()).HttpTestCase and HasBrowser, ensure consistent setup/teardown.TestCase to include HasBrowser.| Component | Laravel Native | zenstruck/browser | Integration Strategy |
|---|---|---|---|
| HTTP Testing | HttpTestCase |
KernelBrowser | Replace HttpTestCase with HasBrowser + KernelBrowser. |
| JS Testing | Dusk | PantherBrowser | Avoid unless Dusk is insufficient. |
| Assertions | Basic (assertSee, assertStatus) |
Fluent (assertSeeIn, assertJsonMatches) |
Extend Laravel’s assertions with zenstruck/browser. |
| Test Data | Factories | Foundry (optional) | Combine with Laravel’s factories or Foundry. |
| Debugging | dd(), dump() |
dd(), dump() + screenshots |
Enhance with BrowserExtension artifacts. |
Phase 1: KernelBrowser Adoption (Low Risk)
zenstruck/browser to tests/ composer.json.TestCase to include HasBrowser:
use Zenstruck\Browser\Test\HasBrowser;
abstract class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
use HasBrowser;
}
zenstruck/browser assertions (e.g., assertSeeIn instead of assertSee).actingAs, expectException, assertJsonMatches).Phase 2: PantherBrowser (High Risk, Optional)
symfony/panther and selenium-server.TestCase to support PantherBrowser:
use Zenstruck\Browser\Test\PantherBrowser;
abstract class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
protected function createPantherBrowser(): PantherBrowser
{
return new PantherBrowser();
}
}
Phase 3: Debugging & Artifacts
BrowserExtension in phpunit.xml:
<extensions>
<bootstrap class="Zenstruck\Browser\Test\BrowserExtension" />
</extensions>
| Laravel Feature | Compatibility Notes |
|---|---|
| Authentication | KernelBrowser’s actingAs() works with Laravel’s Auth facade. |
| Factories | Works with Laravel’s factories; Foundry integration possible but optional. |
| Middleware | KernelBrowser respects Laravel’s middleware (e.g., web, api). |
| Routing | Uses Laravel’s router; no conflicts expected. |
| Event Testing | KernelBrowser can test events via expectException() or use() callbacks. |
| API Testing | Supports get(), post(), etc., with JSON assertions (assertJsonMatches). |
BrowserExtension for debugging after core tests are migrated.| Task | Effort | Notes |
|---|---|---|
| Dependency Updates | Medium | Symfony dependencies may require manual version pinning. |
| Test Refactoring | High | Assertions must be updated to use zenstruck/browser syntax. |
| CI/CD Setup | High | PantherBrowser requires Selenium/Docker; KernelBrowser is lightweight. |
| Debugging | Low | BrowserExtension automates artifact collection (screenshots, logs). |
| Documentation | Medium | Need to update test docs to reflect new assertion syntax. |
assertSeeIn('h1', 'Title') vs. multiple assertSee calls).BrowserExtension) speeds up issue resolution.|
How can I help you explore Laravel packages today?