Laravel, Dusk – Build a web application with Laravel and test it with Dusk

As I have talked with a friend creating a web application with Laravel. I have share with him few insights about how to perform testing on such environment and at the same time I discovered that in the last version of Laravel is shipped with a very practical tool for testing named Dusk.

So here is the the POC made with Laravel (5.6.26) on Mac, with a MySQL database and Dusk. It is a simple application that I have made up with the help of an article of Mukesh Chapagain.

Create the application

We are ready now to launch the building of the application through Laravel.

If you want to create from scratch your project, you can do so. Here is the first command to create the project, but you will need to grab the files from the repository git. The better is to clone directly from Github.

$ cd/path-to-your-directory/

$ cd /path-to-your-directory/

$ laravel new laravel-dusk-testing-people-app

$ laravel new laravel-dusk-testing-people-app

$ cd laravel-dusk-testing-people-app

$ cd laravel-dusk-testing-people-app

2 things you need to know before diving into the application.

The APP_URL : The first thing to do is to change your APP_URL. In order to Dusk access your application and test it, you have to change your local development URL editing the .env file. By default, the APP_URL is http://localhost. If you are running your server in another port, for example 8000, change it to http://localhost:8000. If you keep http://localhost, Dusk will not be able to test your app.In my example, the APP_URL=http://127.0.0.1:8000 in .env

The DB: The second thing is to create a database named for instance db_laravel_people and change the .env by adding the correct databasename (db_laravel_people), username (root) and password (root)

Creating the Migration files

Migrations in Laravel are easy, here is the 2 commands to create a migration for your new application.

$ php artisan migrate:install

$ php artisan migrate:install

$ php artisan make:migration create_people_table --create=people

$ php artisan make:migration create_people_table --create=people

In the file created, add the code to create table ‘people’ in /database/migrations/2018_07_01_101838_create_people_table.php

In order test also the login system that come with Laravel. I will pass this command.

Add the login sytem in the application by the following command

$ php artisan make:auth

$ php artisan make:auth

So far I have created a simple web application based on Laravel. You can add record to the database. Sure that making a CRUD application with the help of Laravel is damn easy but that is the purpose of this post. My concern is to test Dusk inside Laravel. You can check the result with the command.

$ php artisan serve

$ php artisan serve

Tests for the login and the app

We have now a very basic application with some records and 2 screens. So why don’t we test it with Dusk? In order to ensure that for each new feature, there will be no regression in our previous screens.

In order to install Dusk, you can add the following line in your the file: composer.json

$ composer require laravel/dusk

$ composer require laravel/dusk

Then, type this command and it will install Dusk for good.

$ php artisan dusk:install

$ php artisan dusk:install

You have to add the following code inside the file app/Providers/AppServiceProvider.php. It will declare Dusk as a provider in all environment exception made of the production one.

To execute our Dusk tests in a more verbose way, just type in the console.

$ php artisan dusk --debug

$ php artisan dusk --debug

Conclusion: Dusk is using a headless browser of Chrome to perform the tests on your application. Every time the test fails, you will get a nice capture of the screen where it has failed in /tests/Browser/screenshots/.