Browser Kit Testing

laravel/browser-kit-testing image

Browser Kit Testing stats

Downloads
6.1M
Stars
493
Open Issues
0
Forks
74

View on GitHub →

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

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 photo

Laravel is a web ecosystem full of delightful tools that are supercharged for developer happiness and productivity.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Laravel Browser Kit Testing Related Articles

Authentication With Laravel and MongoDB image

Authentication With Laravel and MongoDB

Read article
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Shift logo

Shift

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

Shift
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge