Develop an E-Commerce Website With Laravel 5.4 - Part 4

Published Jun 15, 2017
Develop an E-Commerce Website With Laravel 5.4 - Part 4

All posts in this series:

Alright so we have integrated the theme and inner page for home page. Before I move ahead and talk about Database related thing, it's better to introduce testing with Laravel Dusk

What is Laravel Dusk?

Laravel Dusk provides you a platform to do Integration Testing of your different sections of your page by providing successful and failed scanerio to make sure your application work as it should be. It's built on top of PHPUnit so if you are already familiar with Unit testing and Integration testing then this should not be unknown to you.

Setting up Dusk

Like other sections of Laravel Docs Dusk also have been explained quite well. Dusk is not shipped with Laravel so you need to install it first.

composer require laravel/dusk

Once done open app/Providers/AppServiceProvider.php and register Dusk for local mainly since we don't want it to run for production environment:

<?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', 'staging' ) ) {
      $this->app->register( DuskServiceProvider::class );
    }
  }
}

Then install it:

php artisan dusk:install

It will create necessary folder structure under tests directory.

Also, make sure that APP_URL in your .env is set to application url. In my case it is http://golmarket.dev

Writing first test

Test I am going to write is very simple. I name it as HomePageTest since this will deal All the tests related to our Homepage. The test I am going to write is testHomePageTitle, it will check whether it has desired Page Title or not. Laravel provides an artisan command to create Testsuit:

php artisan dusk:make HomePageTest

It will create a file with sample test method. I rename it as testHomePageTitle.
The thing I want to test that when a user visits home page, it finds the Page title as Welcome to Golmarket. So my method would be like this:

public function testHomePageTitle() {
    $this->browse( function ( Browser $browser ) {
      $browser->visit( '/' )
              ->assertTitle( 'Welcome to Golmarket' );
    

A browser instance is created, it then visits the home page that is /. I am using assertTitle function to test Title value. If it finds the required one, it would be declared as successful.
Now coding part is done, go to your project root directory and run the following command:

php artisan dusk

It will open up Chrome browser, visit the page and then exit. If it is successful then it will show something like:

Screen Shot 2017-06-15 at 1.18.32 PM.png

Let's make the change in title, just to test failed case:

$this->browse( function ( Browser $browser ) {
      $browser->visit( '/' )
              ->assertTitle( 'Welcome to Me' );

It results:
Screen Shot 2017-06-15 at 1.20.05 PM.png

As you see red bar. It also tells why it failed.

Alright so that's it for now. You got the gilmpse of Laravel Dusk. I will be writing tests for the home page and other pages in upcoming posts as well.

The updated code has been pushed to Github.

Discover and read more posts from Adnan Siddiqi
get started
Enjoy this post?

Leave a like and comment for Adnan

Be the first to share your opinion

Get curated posts in your inbox

Read more posts to become a better developer