Browser Kit Testing
Browser Kit Testing stats
- Downloads
- 6.1M
- Stars
- 493
- Open Issues
- 0
- Forks
- 74
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
visitandvisitRoute. - 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, andpress. - 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\Responseobjects. - PHPUnit Assertions: Utilize a variety of custom assertion methods tailored for Laravel applications.
Installation
-
Install via Composer:
composer require laravel/browser-kit-testing --dev -
Update your base
TestCaseto extendLaravel\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.