Perform Browser tests using Laravel dusk in Laravel 5.5

Perform Browser tests using Laravel dusk in Laravel 5.5 .

Laravel dusk package provide an easy and convenient way to perform automated testing of Laravel applications.
Laravel Dusk can perform browser-based testing so client-side features like JavaScript are also testable.
Laravel dusk is based on open source tools such as ChromeDriver and Facebook Php-webdriver so in order to perform tests using Laravel Dusk you does not need to install JDK or Selenium on your machine.
In this tutorial, we are going to install, configure and perform some basic testing tasks using Laravel dusk.

Installing Dusk on Laravel 5.5

To install dusk go into your projects directory and run below command.

composer require --dev laravel/dusk

In this tutorial we are using Laravel Latest version i.e 5.5 which comes with Package Auto-Discovery that mean you don’t need to register your packages in your service providers array , if you are still using Laravel 5.4 or below then you need to register LaravelDuskDuskServiceProvider service provider manually. As Laravel dusk exposes security holes like the ability to log in as other users so it recommended not to use Dusk on production server.To overcome this problem it suggested you to register the Dusk service provider class in a conditional that makes sure we are in a local environment in the appProvidersAppServiceProvider.php file.

Open your appProvidersAppServiceProvider.php and add below codes in it.

use LaravelDuskDuskServiceProvider;// Importing DuskServiceProvider class

public function register()
{
if ($this->app->environment('local', 'testing')) {
$this->app->register(DuskServiceProvider::class);
}
}

Next, Run below artisan command to install Laravel dusk scaffolding.

php artisan dusk:install

This command will create a browser directory in your application tests directory with an example test.

Next,set the APP_URL environment variable in your .env file.

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:uD/Nnu6P7oC/KY9m08WWh8pqHV2K7kU5WOctAPzs+qM=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost // Update this url to your URL, It should be match the URL that you use to access your application on browser.

Running Tests

Use below artisan command to run your test.

php artisan dusk

Read Full Post

One thought to “Perform Browser tests using Laravel dusk in Laravel 5.5”

Leave a Reply

Your email address will not be published. Required fields are marked *