orchestra/testbench-dusk
Helper for testing Laravel packages with Laravel Dusk. Provides a Testbench-based setup to run browser tests in a package development workflow, maintained under the Orchestra namespace with ongoing support and community contributions.
Install Dependencies
Add to your package's composer.json:
"require-dev": {
"orchestra/testbench-dusk": "^11.0",
"laravel/dusk": "^8.5",
"phpunit/phpunit": "^12.0"
}
Run composer update in your package directory.
Configure Dusk Publish the Dusk config (if using Laravel Dusk):
php artisan vendor:publish --provider="Laravel\Dusk\DuskServiceProvider" --tag="dusk-config"
Ensure DUSK_BROWSER is set in .env.testing (e.g., chrome).
First Test Case
Create a test class extending Orchestra\Testbench\DuskTestCase:
use Orchestra\Testbench\DuskTestCase;
class ExampleDuskTest extends DuskTestCase
{
public function test_example()
{
$this->browse(function ($browser) {
$browser->visit('/')
->assertSee('Welcome');
});
}
}
Run tests with:
phpunit
Package Integration
Use withPackage() to load your package in tests:
protected function getPackageProviders($app)
{
return ['YourPackage\\ServiceProvider'];
}
Isolated Package Testing
beforeServingApplication() to mock dependencies or configure the app:
protected function beforeServingApplication($app)
{
$app['config']->set('your-package.key', 'value');
}
afterServingApplication():
protected function afterServingApplication($app)
{
$app['config']->set('your-package.key', null);
}
Dusk-Specific Helpers
click(), assertPathIs(), pause(5000)).$browser->assertSeeInPackageView('your-package::partial', 'Expected Text');
Test Data Setup
create() or factory() from Testbench to seed test data:
$this->browse(function ($browser) use ($user) {
$browser->loginAs($user)
->visit('/dashboard');
});
Cross-Browser Testing
phpunit.xml:
<env name="DUSK_BROWSER" value="chrome|firefox"/>
CI/CD Integration
DUSK_HEADLESS=true in CI for faster tests:
DUSK_HEADLESS=true phpunit --filter=DuskTest
Testbench + Dusk Synergy
migrate() and artisan() with Dusk’s browser interactions:
$this->artisan('migrate:fresh')
->browse(function ($browser) {
$browser->visit('/admin')
->assertPathIs('/admin');
});
Package-Specific Dusk Commands
Browser class for package-specific actions:
namespace YourPackage\Dusk;
use Laravel\Dusk\Browser;
class PackageBrowser extends Browser
{
public function triggerPackageAction()
{
$this->click('@package-button');
}
}
Mocking External Services
Mockery or Http::fake() within Dusk tests:
$this->browse(function ($browser) {
Http::fake([
'api.example.com/*' => Http::response('Mocked', 200),
]);
$browser->visit('/api-dependent-page');
});
Parallel Testing
--group flag:
phpunit --group=auth --parallel
Headless Mode Quirks
assertSee for dynamic content).pause(5000) or waitFor() for critical interactions:
$browser->waitFor('.loader', 10)->assertMissing('.loader');
Browser Driver Conflicts
composer.json or use Laravel’s dusk:driver command to auto-download:
php artisan dusk:driver
State Persistence
refresh() or withoutMiddleware() for isolated tests:
$this->browse(function ($browser) {
$browser->withoutMiddleware()->visit('/');
});
Package Loading Order
getPackageProviders() returns all required providers, including Laravel’s DuskServiceProvider.Deprecated Methods
tweakApplication() is deprecated; use beforeServingApplication() instead.Visual Debugging
$this->browse(function ($browser) {
$browser->screenshot('debug-failed-test');
});
phpunit.xml:
<env name="DUSK_SCREENSHOTS" value="true"/>
Log Output
$browser->driver->manage()->logs('browser')->get('available');
Slow Tests
--stop-on-failure:
phpunit --stop-on-failure --verbose
Custom Dusk Traits
trait PackageDuskTrait
{
protected function loginAsPackageAdmin($browser)
{
$browser->visit('/login')
->type('email', 'admin@example.com')
->type('password', 'password')
->press('Login');
}
}
Dusk Service Providers
DuskServiceProvider to register package-specific Dusk commands or macros:
namespace YourPackage\Dusk;
use Laravel\Dusk\DuskServiceProvider as BaseProvider;
class DuskServiceProvider extends BaseProvider
{
public function boot()
{
$this->macro('assertPackageFeature', function () {
// Custom assertion logic
});
}
}
Test Data Factories
create() with Dusk for consistent test data:
$user = create(User::class, ['email' => 'test@example.com']);
$this->browse(function ($browser) use ($user) {
$browser->loginAs($user);
});
Environment-Specific Config
DUSK_TIMEOUT):
if (app()->environment('testing')) {
$this->browse(function ($browser) {
$browser->driver->manage()->timeouts()->implicitWait(2);
});
}
refresh() or withoutMiddleware() to avoid test pollution.--group=dusk and cache dependencies.@dusk tags to test methods for clarity:
/**
* @dusk Tests the package's admin dashboard.
*/
public function test_admin_dashboard() { ... }
How can I help you explore Laravel packages today?