Laravel Dusk Configuration & How to use?
Use Laravel Dusk, browser automation testing
Laravel Dusk is a powerful browser automation tool for Laravel. With Dusk you can programmatically test your own applications or visit any website on the internet using a real Chrome browser.
Let’s Start:
📦 Package Install: Laravel Dusk
composer require --dev laravel/dusk
php artisan dusk:install
You will see a new browser directory under tests
folder. In this folder we will create our test file and write the code.
⚙️Setup Configuration: env, database
For Dusk testing we will use new SQLite database.
First go to config/database.php
, Add a new SQLite database configuration.
'dusk_testing' => [ 'driver' => 'sqlite', 'database' => database_path('dusk_database.sqlite'), 'prefix' => '', 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),],
Create a SQLite database under database directory.
Create a custom
.env
file.
Then we will create a new .env
file called .env.dusk.local
you can name as you like, but make sure you write .env
at the front.
APP_NAME=Laravel Dusk
APP_ENV=local
APP_KEY=base64:pRcfQxwaRlwRb0aN2lbJGQe1MhpCK7IHpQ6EJPSoqcQ=
APP_DEBUG=true
APP_LOG=daily
APP_URL=http://127.0.0.1:8000 //make sure this should writeLOG_CHANNEL=stackDB_CONNECTION=dusk_testing
If we want to use this .env file when we run our server then we need to use these this command to run our server:
php artisan serve --env=dusk.local
Before we go for testing we have do one more work, we need to tell dusk “hey dusk, you will use this database.” Yes, we have declare a database in our custom .env
file but sometimes dusk hasn’t identify the default database.
So go to tests/CreatesApplication.php
file and add this code into createApplication
method.
$app['config']->set('database.dafult', 'dusk_testing');
That’s It.