orchestra/testbench-browser-kit
Adds Laravel BrowserKit testing to Orchestra Testbench for package development. Swap your base test case to Orchestra\Testbench\BrowserKit\TestCase to use fluent visit/see/form APIs in functional tests across supported Laravel versions.
composer require --dev orchestra/testbench-browser-kit
Orchestra\Testbench\TestCase with Orchestra\Testbench\BrowserKit\TestCase in your test base class:
use Orchestra\Testbench\BrowserKit\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
public $baseUrl = 'http://localhost';
}
public function testBasicRoute()
{
$this->visit('/')
->see('Expected Text');
}
BrowserKit integrates with Laravel’s testing helpers.Route Testing:
Use visit() and visitRoute() for GET requests:
$this->visit('/dashboard')
->seeRouteIs('dashboard');
$this->visitRoute('profile.edit', ['user' => 1]);
Form Interaction: Chain methods for form submission:
$this->visit('/register')
->type('John Doe', 'name')
->check('terms')
->press('Submit')
->seePageIs('/welcome');
API Testing:
Use json() for API endpoints:
$this->json('POST', '/api/users', ['name' => 'Test'])
->seeJsonEquals(['success' => true]);
Authentication:
Authenticate users with actingAs():
$user = create('App\Models\User');
$this->actingAs($user)
->visit('/profile')
->see('Welcome, ' . $user->name);
Middleware Isolation: Disable middleware for specific tests:
$this->withoutMiddleware()
->visit('/admin')
->assertResponseOk();
TestCase to test package routes, middleware, and views in isolation.getPackageProviders() and getPackageAliases() to mock dependencies.assert* methods (e.g., assertViewHas()) for granular checks.Base URL Mismatch:
Ensure $baseUrl in TestCase matches your test environment (e.g., http://localhost).
Fix: Override $baseUrl in child test classes or use TestCase::setBaseUrl().
Session Persistence:
withSession() data is not automatically persisted across tests.
Fix: Use actingAs() for authenticated sessions or manually set session data per test.
Middleware Conflicts:
Disabling middleware globally (WithoutMiddleware) may hide critical issues.
Fix: Use withoutMiddleware() sparingly and test middleware logic separately.
CSRF Token Errors:
Forms may fail if CSRF protection is enabled but tokens aren’t included.
Fix: Use press() or manually add CSRF tokens in attach() for file uploads.
Dynamic Routes:
visitRoute() requires named routes to exist in the test environment.
Fix: Define routes in routes/web.php or use Route::get() in TestCase::setUp().
dump($this->response->getContent()) to debug HTML/API responses.APP_DEBUG=true) to log HTTP requests.public function tearDown(): void
{
Artisan::call('migrate:reset');
parent::tearDown();
}
Custom Helpers:
Extend TestCase to add package-specific assertions:
class PackageTestCase extends TestCase
{
protected function assertPackageConfig()
{
$this->assertConfig('package.key', 'expected_value');
}
}
Mocking Dependencies:
Use Testbench’s getApplication() to mock services:
$this->app->instance('App\Contracts\Service', MockService::class);
Custom Middleware:
Override getMiddleware() to inject test-specific middleware:
protected function getMiddleware()
{
return [
\App\Http\Middleware\TestMiddleware::class,
];
}
BrowserKit Extensions:
Add custom assertions to TestCase:
protected function seePackageView($viewName)
{
$this->see($viewName);
$this->assertViewIs($viewName);
}
How can I help you explore Laravel packages today?