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.

Laravel Packages Laravel Laravel News

Similar Articles

PHP develop your own package with local composer environment

PHP develop your own package with local composer environment

Developing your own PHP composer package locally & integrating local file symlink within your application can speeds up development immensely.
Read more...
Laravel Zero: A Micro-framework for console applications

Laravel Zero: A Micro-framework for console applications

Get leverage of laravel magic to create console applications within a micro framework.
Read more...
Convert webpage to PDF or image in laravel

Convert webpage to PDF or image in laravel

Browsershot is package to convert webpage to image or PDF in laravel.
Read more...