Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Browser Kit Laravel Package

symfony/browser-kit

Symfony BrowserKit simulates a web browser in PHP for functional tests: make requests, follow links, fill and submit forms, and inspect responses. Includes an HttpClient-based implementation to perform real HTTP requests programmatically.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Laravel Compatibility: BrowserKit is Symfony-agnostic but integrates seamlessly with Laravel’s testing stack (PHPUnit, HttpTests). It leverages Laravel’s HttpClient (via Symfony’s HttpClient adapter) or standalone Guzzle, making it a drop-in replacement for manual HTTP assertions or Dusk’s browser automation.
  • Testing Paradigm Shift: Replaces UI-centric testing (Dusk/Selenium) with server-centric testing, aligning with Laravel’s API-first philosophy. Ideal for:
    • Form submissions (e.g., multi-step workflows).
    • Redirect validation (e.g., auth flows, 404s).
    • Session/cookie management (e.g., CSRF tokens, expiration).
  • Hybrid Testing: Complements Laravel’s HttpTests by adding DOM traversal (DomCrawler) and browser history tracking, enabling end-to-end validation without a real browser.

Integration Feasibility

  • Low Friction: BrowserKit requires zero Laravel-specific configuration. Key integration points:
    • PHPUnit Tests: Replace assertResponse() with ->clickLink()->submitForm().
    • Custom Test Helpers: Extend Laravel’s TestCase with BrowserKit methods (e.g., public function simulateUserFlow()).
    • Service Container: Bind Symfony’s BrowserKit and HttpClient as Laravel services (10-minute setup).
  • Existing Ecosystem: Works with:
    • Laravel’s Http facade (via HttpClient adapter).
    • DomCrawler for HTML assertions (e.g., assertSelectorTextContains()).
    • PHPUnit constraints (e.g., BrowserHistoryIsOnLastPage).

Technical Risk

Risk Area Mitigation Strategy
PHP Version Gaps BrowserKit v8.x requires PHP 8.4+; v7.x supports 8.2+. Laravel 10+ aligns with 8.2+.
Symfony Dependency Minimal footprint: Only symfony/browser-kit and symfony/http-client (~5MB).
Test Flakiness Eliminates browser instability (e.g., timing issues, JS races). Tests run in ~50ms/page.
Learning Curve Familiar API for Laravel devs (e.g., ->fillForm(), ->clickLink() mimics Dusk).
Missing Features Gaps like WebSockets or JS execution are intentional—use Dusk/Playwright for those.
CI/CD Impact No infrastructure changes needed (unlike Dusk’s ChromeDriver).

Key Questions for the Team

  1. Testing Scope:
    • What percentage of our tests are form/submission-heavy? (BrowserKit excels here.)
    • Do we test server-side redirects or session handling? (BrowserKit’s history tracking helps.)
  2. Tooling Trade-offs:
    • Are we willing to deprecate Dusk for non-JS flows? (BrowserKit is 80% faster.)
    • Do we need visual regression testing? (If yes, keep Dusk/Playwright.)
  3. PHP Version:
    • Can we upgrade to PHP 8.2+ for BrowserKit v7.x or 8.4+ for v8.x?
  4. Team Skills:
    • Is the team comfortable with Symfony components (e.g., HttpClient, DomCrawler)?
  5. Edge Cases:
    • Do we test file uploads, iframes, or custom headers? (BrowserKit handles these.)
    • Are there legacy Laravel apps (PHP <7.4) that need testing? (BrowserKit v5.x is deprecated.)

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • PHPUnit: BrowserKit integrates with PHPUnit’s TestCase via BrowserKitTestCase (custom wrapper).
    • HttpClient: Uses Laravel’s Http facade or standalone Guzzle (configurable).
    • DomCrawler: For HTML assertions (e.g., assertSelectorNotExists()).
    • PestPHP: Supports BrowserKit via BrowserKitTestCase or custom extensions.
  • Symfony Synergy:
    • Leverages Symfony’s HttpClient (already used in Laravel for HTTP requests).
    • Shares DomCrawler with Symfony’s Panther (headless browser testing).
  • Alternatives Replaced:
    • Laravel Dusk: For non-JS flows (e.g., form submissions, redirects).
    • Guzzle HTTP: For API-only tests (BrowserKit adds DOM/navigation layers).

Migration Path

  1. Assessment Phase (2–4 weeks):
    • Audit existing tests: Identify Dusk-heavy or form/submission tests.
    • Benchmark: Compare BrowserKit vs. Dusk execution time (target 80%+ speedup).
  2. Pilot Phase (3–6 weeks):
    • Rewrite 1–2 critical test suites (e.g., auth flows, checkout).
    • Use parallel testing to validate coverage parity.
  3. Full Adoption (6–8 weeks):
    • Deprecate Dusk for server-side flows.
    • Keep Dusk/Playwright for JS-dependent tests.
  4. Tooling Setup:
    • Create a BrowserKitTestCase base class:
      use Symfony\Component\BrowserKit\TestCase as SymfonyTestCase;
      
      class BrowserKitTestCase extends SymfonyTestCase {
          protected function createClient(array $options = []): BrowserKitClient {
              return static::createClient($options, [
                  'browser' => new BrowserKitBrowser(),
              ]);
          }
      }
      
    • Configure composer.json:
      "require-dev": {
          "symfony/browser-kit": "^8.0",
          "symfony/http-client": "^6.4"
      }
      

Compatibility

Component Compatibility Notes
Laravel 10/11 Full support (PHP 8.2+).
Laravel 9 Use BrowserKit v7.x (PHP 8.1+).
PHP 8.4+ Required for BrowserKit v8.x (native HTML5 parser).
Guzzle 7.x BrowserKit’s HttpClient adapter supports Guzzle.
PHPUnit 10+ Works with PHPUnit’s latest constraints (e.g., BrowserHistoryIsOnLastPage).
PestPHP Supports BrowserKit via custom extensions.
Dusk Co-existence: Use Dusk for JS tests, BrowserKit for server-side tests.

Sequencing

  1. Phase 1: API/HTML Hybrid Tests
    • Replace HttpTests assertions with BrowserKit’s ->request() + DomCrawler.
    • Example:
      $client = $this->createClient();
      $crawler = $client->request('GET', '/checkout');
      $crawler->submitForm('Proceed', ['payment' => 'credit_card']);
      $this->assertSelectorTextContains('h1', 'Order Confirmation');
      
  2. Phase 2: Form/Navigation Flows
    • Migrate Dusk tests for non-JS forms (e.g., checkout, login).
    • Example:
      $client->clickLink('Login');
      $client->submitForm('Sign In', ['email' => 'user@example.com']);
      $this->assertBrowserHistoryIsOnLastPage();
      
  3. Phase 3: Edge Cases
    • Test cookies, redirects, and file uploads with BrowserKit.
    • Example:
      $client->getCookieJar()->set(new Cookie('remember_me', '1', time() + 3600));
      $client->request('POST', '/upload', ['file' => new UploadedFile(...)]);
      

Operational Impact

Maintenance

  • Pros:
    • No Browser Infrastructure: Eliminates ChromeDriver/Sauce Labs costs.
    • Pure PHP: Tests run locally or in CI without setup.
    • Symfony Backing: Actively maintained (3K+ stars, Symfony team support).
  • Cons:
    • Symfony Dependency: Requires familiarity with Symfony components.
    • Test Refactoring: Dusk → BrowserKit migration may need 1–2 dev weeks.
  • Long-Term Costs:
    • Zero: No licensing or cloud costs for browser testing.

Support

  • Debugging:
    • Faster: Tests fail in ~50ms vs. Dusk’s 2–5s (no browser spin-up).
    • Logs: BrowserKit
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests