orchestra/testbench-dusk
Laravel Dusk helper for Laravel package development. Integrates with Orchestra Testbench to run browser tests against a package’s test app, making it easier to write and maintain Dusk suites for packages.
Install Dependencies
Add to your package's composer.json:
"require-dev": {
"orchestra/testbench-dusk": "^11.0",
"laravel/dusk": "^8.3.5",
"phpunit/phpunit": "^12.0"
}
Run composer update.
Configure Dusk Publish the Dusk config:
php artisan vendor:publish --provider="Laravel\Dusk\DuskServiceProvider" --tag=dusk-config
Update dusk.config.php to include:
'browser' => [
'disable_search_engine_choice_screen' => true,
'disable_smooth_scrolling' => true,
],
First Test
Create a test class extending Orchestra\TestbenchDusk\TestCase:
use Orchestra\TestbenchDusk\TestCase;
class ExampleTest extends TestCase {
public function test_package_feature() {
$this->browse(function ($browser) {
$browser->visit('/')
->assertSee('Expected Package Content');
});
}
}
Run with:
php artisan dusk
Testing Package UI Components Use Dusk to verify your package’s frontend interactions (e.g., modals, forms, or livewire components) in isolation. Example:
public function test_package_modal() {
$this->browse(function ($browser) {
$browser->visit('/admin/dashboard')
->click('@open-modal-button')
->waitForText('Modal Title')
->assertSee('Package-Specific Content');
});
}
Testbench + Dusk Integration
Leverage Testbench’s getPackageProviders() and getEnvironmentSetUp() alongside Dusk:
protected function getEnvironmentSetUp($app) {
// Testbench setup
$app->make(Kernel::class)->bootstrap();
// Dusk-specific tweaks
$app['config']->set('app.debug', true);
}
Package-Specific Context
Override getPackageAliases() to ensure your package’s Blade directives/aliases are available:
protected function getPackageAliases($app) {
return [
'package-alias' => 'Package\\ServiceProvider',
];
}
CI Optimization
Use beforeServingApplication() to preload assets or mock APIs:
protected function beforeServingApplication($server) {
$server->addMiddleware(function ($request, $next) {
if ($request->is('api/*')) {
return response()->json(['data' => 'mocked']);
}
return $next($request);
});
}
Livewire/Alpine Testing
Combine with dusk-livewire helpers:
$browser->wire('livewire-component')
->assertSee('Livewire Content')
->call('actionMethod');
$this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
factory(\App\Models\User::class)->create();
$this->artisan('package:command')
->assertExitCode(0)
->assertOutputContains('Success');
$browser->visit('/')
->type('email', 'test@example.com')
->press('Submit')
->waitFor('.success-message');
Headless Mode Conflicts
--disable-search-engine-choice-screen in CI.dusk.config.php:
'browser' => [
'headless' => true,
'args' => [
'--disable-gpu',
'--no-sandbox',
'--disable-dev-shm-usage',
],
],
Asset Compilation
mix-manifest.json) cause 404s.npm run dev in CI or use beforeServingApplication():
$this->artisan('vendor:publish', ['--tag' => 'public']);
Session Persistence
persistentSession() in TestCase:
protected $persistentSession = true;
Livewire Debugging
$this->app->make(\Livewire\Livewire::class)->debug();
$browser->driver->manage()->logs()->get('browser');
$this->expectException(\Facebook\WebDriver\Exception\NoSuchElementException::class);
$browser->waitFor(5)->toBeVisible('#element');
Custom Dusk Helpers
Extend TestCase to add package-specific methods:
protected function assertPackageFeature($browser, $feature) {
$browser->assertSee('Package Feature: ' . $feature);
}
Mocking HTTP Requests
Use Http::fake() in beforeApplication():
protected function beforeApplication() {
Http::fake([
'api.example.com/*' => Http::response('Mocked'),
]);
}
Parallel Testing
Configure in phpunit.xml:
<phpunit>
<extensions>
<extension class="Dusk\Extensions\ParallelExtension"/>
</extensions>
</phpunit>
refreshDatabase() for each test:
public function setUp(): void {
parent::setUp();
$this->refreshDatabase();
}
$this->browse(function ($browser) {
$browser->driver->manage()->window()->setSize(1920, 1080);
});
php artisan dusk:cache
How can I help you explore Laravel packages today?