The power of Laravel Dusk, a browser automation testing tool

The power of Laravel Dusk, a browser automation testing tool

Mar 15, 2019 / Ramesh Mhetre

Laravel Dusk is a tool which provides an expressive, easy-to-use browser automation and testing API. Practically its just another PHP package for your project to fully functional browser tests.

By default, Dusk does not require you to install JDK or Selenium on your machine. Instead, Dusk uses a standalone ChromeDriver installation. However, you are free to utilize any other Selenium compatible driver you wish.

The Laravel Dusk installation is simple, after composer package installation just have to run:

php artisan dusk:install

The best thing is you can use all the power of Laravel framework for your browser tests as well. For instance, using Laravel Model Factories to seed data for your tests.

Using other browsers with Laravel Dusk

With laravel dusk you may start your own Selenium server and run your tests against any browser you wish. You just have to stop using ChromeDriver while preparing your instance in file tests/DuskTestCase.php

Environment handling in Laravel Dusk

We can create all together separate environment for dusk tests by just creating separate .env file for that dusk. To force Dusk to use its own environment file when running tests, create a .env.dusk.{environment} file in the root of your project. For example, if you will be initiating the dusk command from your local environment, you should create a .env.dusk.local file.

Vuejs Assertions with Laravel Dusk

Laravel Dusk even allows you to make assertions on the state of Vue component data(Isn't is awesome :)). For example, imagine your application contains the following Vue component:

// HTML...
<profile dusk="profile-component"></profile>
// Component Definition...
Vue.component('profile', {
    template: '<div>{{ user.name }}</div>',
    data: function () {
        return {
            user: {
              name: 'Taylor'
            }
        };
    }
});

You may assert on the state of the Vue component like so:

public function testVue()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/')
                ->assertVue('user.name', 'Taylor', '@profile-component');
    });
}


There are so many available assertions with proper documentation about them.


Similar Articles

Connect your laravel application to Sqlite DB

Connect your laravel application to Sqlite DB

Quickly connecting with sqlite database within laravel application
Read more...
Laravel v6 & Laravel Vapor is going to release soon 🚀

Laravel v6 & Laravel Vapor is going to release soon 🚀

Laravel v6 is is releasing soon. Serverless laravel is next big thing in laravel community!
Read more...
Compiling assets with Laravel Mix

Compiling assets with Laravel Mix

Laravel Mix provides a fluent API for defining Webpack build steps
Read more...