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

Launching Blade UI Kit v0.1 image

Launching Blade UI Kit v0.1

Read article
Kit: An Opinionated API Starter Kit for Laravel image

Kit: An Opinionated API Starter Kit for Laravel

Read article
Testing Vue components with Laravel Dusk image

Testing Vue components with Laravel Dusk

Read article
Laravel Nuxt UI Starter Kit image

Laravel Nuxt UI Starter Kit

Read article
Laravel Dusk Browser Testing: Best Practices & Tips image

Laravel Dusk Browser Testing: Best Practices & Tips

Read article
A Blade-Only Starter Kit for Laravel 12 Projects image

A Blade-Only Starter Kit for Laravel 12 Projects

Read article

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

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

PhpStorm

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

PhpStorm

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

Shift

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

Shift

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing