Laravel Browser Testing, Automation using Laravel Dusk

Laravel Browser Testing, Automation using Laravel Dusk

Laravel Dusk is a package that provides the functionality of unit testing in the browser. It does not require JDK or Selenium. This package uses Chrome driver by default but it can be configured to use Selenium with different browser as well.

First, we get information about the installation part and then we will do one test case with login authentication. I hope this information will be useful for the projects to do unit test and perform test cases using Dusk.

1. Installation

You should have composer dependency in your project.  For the installation please run this command in your Laravel project.

composer require –dev laravel/dusk

2. Register service provider

After the installation, you need to register Dusk service provided in the project.

Open the config/app.php file and add this line into provider array.


3. Configure service provider in a Staging environment

By registering service provider in the application, it will work in all environment but we would not require this in live environment so we will configure this in only Staging, local environment only.

For the environment set up, please open your AppServiceProvider and configure environment like this

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Laravel\Dusk\DuskServiceProvider;

class AppServiceProvider extends ServiceProvider

* Register any application services.
* @return void
public function register()
if ($this->app->environment(‘local’, ‘testing’, ‘staging’)) {

4. Complete installation

php artisan dusk:install

By running this command if we open tests directory, one new directory created called Browser for scaffolding which is necessary for dusk tests.

5. Create Test

We are checking here Laravel pre-build authentication for user. To create authentication first;

php artisan make:auth

Now create test by dusk:make Testname

php artisan dusk:make AuthTest

Above command will create AuthTest class in our Browser Directory.

Please make sure that you have run the migration and users in the database.

Now we create one function that test authentication of the user as of now we are passing the wrong credential so we will check how its work.
class AuthTest extends DuskTestCase
$this->browse(function ($browser) {
->type(’email’, ‘’)
->type(‘password’, ‘your password’)
->assertSee(‘You are logged in!’);

By this function, we can check user login and get the success messages or not.

6. Run the Test

php artisan dusk

Now you will get the result

for the successful test (2 tests, 2 assertions)

for the fail test


Tests: 3, Assertions: 0, Errors: 3.

For the fail case Dusk takes screenshots of the page where the error occurs and save to screen-shots directory. The screenshots give us the idea of errors. By using this Laravel package you can validate or test any of the forms like this.

The following two tabs change content below.

Darshan Ambaliya

Sr. Software Developer (PHP) at Dev Information Technology Ltd.
I am Sr.Software Engineer at Dev IT Ltd . Currently exploring JavaScript frameworks like Angular, React. I also like to play chess.

Latest posts by Darshan Ambaliya (see all)

Leave a Reply

Your email address will not be published.