Package

Laravel Browser Kit Testing

Provides backwards compatibility for BrowserKit testing in the latest Laravel release.

Package

laravel/browser-kit-testing

View on GitHub →

Laravel BrowserKit Testing

Laravel BrowserKit Testing is a package that offers a fluent API for simulating HTTP requests within your Laravel application, allowing you to test output, interact with forms, and more. It aims to simplify the process of writing tests that mimic user interactions.

Key Features

  • HTTP Requests: Easily simulate GET and POST requests using methods like visit and visitRoute.
  • Assertions: Assert specific texts, URLs, and JSON structures in responses with methods like see, dontSee, seePageIs, and various JSON assertions.
  • Form Interaction: Interact with forms through methods like type, select, check, and press.
  • Session and Authentication: Manipulate session data and authenticate users within tests.
  • Middleware Control: Optionally disable middleware to test routes and controllers in isolation.
  • Custom HTTP Requests: Create custom HTTP requests and inspect raw Illuminate\Http\Response objects.
  • PHPUnit Assertions: Utilize a variety of custom assertion methods tailored for Laravel applications.

Installation

  1. Install via Composer:

    composer require laravel/browser-kit-testing --dev
  2. Update your base TestCase to extend Laravel\BrowserKitTesting\TestCase:

    namespace Tests;
     
    use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
     
    abstract class TestCase extends BaseTestCase
    {
    use CreatesApplication;
    public $baseUrl = 'http://localhost';
    }

Usage Examples

  • Basic Page Visit and Text Assertions:

    public function testBasicExample()
    {
    $this->visit('/')
    ->see('Laravel')
    ->dontSee('Rails');
    }
  • Form Interaction:

    public function testNewUserRegistration()
    {
    $this->visit('/register')
    ->type('Taylor', 'name')
    ->check('terms')
    ->press('Register')
    ->seePageIs('/dashboard');
    }
  • JSON API Testing:

    public function testBasicExample()
    {
    $this->json('POST', '/user', ['name' => 'Sally'])
    ->seeJson([
    'created' => true,
    ]);
    }
  • Session and Authentication:

    public function testApplication()
    {
    $user = factory(App\User::class)->create();
     
    $this->actingAs($user)
    ->withSession(['foo' => 'bar'])
    ->visit('/')
    ->see('Hello, '.$user->name);
    }

This package is essential for developers looking to thoroughly test Laravel applications by simulating and asserting a wide range of user interactions, enhancing test reliability and application robustness.

Laravel Browser Kit Testing Related Articles

Authentication With Laravel and MongoDB image

Authentication With Laravel and MongoDB

Read article
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Pushsilver logo

Pushsilver

Pushsilver is simple invoicing for freelancers and small studios — send polished invoices, automate reminders, and get paid on time without the bloat.

Pushsilver
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift