paint-brush
How to run and automate E2E tests using Laravel Duskby@BuddyWorks
2,859 reads
2,859 reads

How to run and automate E2E tests using Laravel Dusk

by BuddyMarch 21st, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The <a href="https://buddy.works/guides/first-steps-with-laravel-and-continuous-delivery?utm_source=medium&amp;utm_medium=post&amp;utm_campaign=how-to-run-and-automate-e2e-tests-using-laravel-dusk&amp;utm_content=link" target="_blank">previous article</a> explained the process of creating unit and feature tests, and showed you how to automate them with Buddy. With this guide, you’ll learn how to run <a href="https://hackernoon.com/tagged/e2e" target="_blank">E2E</a> tests by using <a href="https://hackernoon.com/tagged/laravel" target="_blank">Laravel</a> Dusk.
featured image - How to run and automate E2E tests using Laravel Dusk
Buddy HackerNoon profile picture

Introduction

The previous article explained the process of creating unit and feature tests, and showed you how to automate them with Buddy. With this guide, you’ll learn how to run E2E tests by using Laravel Dusk.

Laravel Dusk is simply one of the kind of browser tests introduced in Laravel in v5.4. The tests use the ChromeDriver by default, but you can also configure them to use Selenium in a different browser.

Test project

If you have no idea how to add the tests, we’ll guide you through the process using the example of a simple calculator. In this guide, you can see how to create a calculator like that.

  1. At the beginning, make sure to fork and clone the project github.com/buddy-works/laravel-first-steps

  2. Then, you can check the application immediately by running:

    $ laravel-first-steps$ composer install$ php artisan serve

NOTE: If you have any trouble, check if you installed all required things.

Install Dusk

If you want to install Dusk, just follow the steps:

  • Install Laravel Dusk and add it to the project’s dependency:

    $ composer require laravel/dusk


  • Register DuskServiceProvider. Open app\Providers\AppServiceProvider.phpand add the namespace:

    use Laravel\Dusk\DuskServiceProvider;

  • Register the provider in the register method by adding:

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

The entire AppServiceProvider file should look like this:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;use Laravel\Dusk\DuskServiceProvider;

class AppServiceProvider extends ServiceProvider{    /**     * Bootstrap any application services.     *     * @return void     */    public function boot()    {        //    }

    /**     * Register any application services.     *     * @return void     */    public function register()    {        if ($this->app->environment('local', 'testing')) {            $this->app->register(DuskServiceProvider::class);        }    }}
  • Lastly, install Dusk by executing

    $ php artisan dusk:install

Writing first Dusk test

At first, the exemplary test should be removed or it will return errors:

$ rm tests/Browser/ExampleTest.php

Once done, make sure to add our first test by executing the artisancommand. Then…

Want to learn more? Follow the full article here.