How to run and automate E2E tests using Laravel Dusk

Written by BuddyWorks | Published 2017/03/21
Tech Story Tags: laravel | php | selenium | selenium-test-automation | phpunit

TLDRvia the TL;DR App

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.


Published by HackerNoon on 2017/03/21