• +91 9806724185,+91 9685733903
  • +91 9806724185

Laravel Dusk is a powerful browser automation tool for Laravel. With Dusk you can programmatically test your own applications or visit any website on the internet using a real Chrome browser.

Using Dusk you can automate repetitive tasks, scrape information from other sites or test to make sure your app always works in the browser. In this tutorial, we’ll go through how to create a job, log in to a mythical website and click around.

laravel Dusk

laravel Dusk

How Laravel Dusk Works?

Laravel Dusk charts a whole new path for developers in application testing. Application testing has been completely rewritten with ChromeDriver now the base of everything. The ChromeDriver works as a standalone diver that controls the workings of Chrome. Laravel Dusk works by sending your commands to ChromeDriver as you write your application tests. The ChromeDriver then lights up Chrome in which your tests run. While your tests run in the browser Laravel Dusk reports back to you the results.

Lavarel Dusk makes automated browser tests in your application possible. The developers of Laravel have put out a tool that has made app functionality testing and automation very easy.

So how can you take advantage of what Laravel Dusk offers?

How to Access feature of Laravel Dusk:

Get an up to date and a recent version of the Laravel Dusk via composer.

composer require –dev laravel/dusk

As a note though; never register Dusk in your production environment in the event that you carry out a manual registration of Laravel Dusk’s service provider. If you do so random users could get access to authentication via your application.

When you are done with the installation of the Dusk package run the dusk: install command to complete the process and then get ready to run some tests.

php artisan dusk:install

Upon the installation, there will be a browser directory created within a tests directory that will contain a test example. What come next is setting up the APP_URL environment variable in the .env file. The value of this APP_URL should be the same as the one you will be using while accessing your app in a browser.

You have the option of having Dusk use its own environment file. You can do this by creating a.env.dusk.{environment} file. An example of this in a local environment file would be .env.dusk.local. This comes in handy in case you don’t want to test your current database but instead want to have a test of a test database instead.

Running Tests

When running any of your tests, make use of the dusk Artisan command. The dusk command will accept any argument accepted by the PHPUnit command.

php artisan dusk

This command executes the ExampleTest within the tests\Browser\ExampleTest.php. This gives you the result as shown below:

public function testBasicExample()
{
   $this->browse(function (Browser $browser) 
{
     $browser->visit('/')
     assertSee(‘Laravel’);
});
}

The results show that the test was passed displaying the result OK.

Opening the file tests\Browser\ExampleTest.php will display for you the method above.

The browse method used accepts a callback that is an instance of the Browerclass. Within this callback, different methods and assertions that are unique to Dusk can be called.

Going to the visit() method allows you to run a test that visits the URL which matches the argument specified.

The assertSee() method inspects to find if the text matching the argument provided appears on the page.

In the example given above a visit on the homepage is done plus a check for the appearance of the text ‘Laravel’. A look at the documentation page on laravel.com will show you that there are other assertions available that you can look into.

How test fails look like:

Go to the argument assertSee() and replace the text ‘Laravel’ with text that does not appear on the visited page for example ‘whoa’. You will promptly get a failed test message. A screenshot of the failed test will also be available in the tests\Browser\screenshots directory.

Creating Browsers in Laravel Dusk:

To begin with, write a test that verifies the application login. Upon generating a test, make a modification to navigate to the login page. Key in the necessary credentials and then click the ‘Login’ button.

Call on the browse method to create browser instance.

namespace Tests Browser;
  use App \User;
  use Tests\DuskTestCase;
  use Laravel\Dusk\Chrome;
  use Illuminate\Foundation\Testing\Database\Migrations;

class ExampleTest extends DuskTestCase

{
   use DatabaseMigrations;
     // A basic browser test example
  @return void

public function testBasicExample()
{
   $user-factory(User::class)->create
{[
   ‘email’ => ‘taylor@laravel.com’,
]);

$this->browse (function (browser) use ($user) 

{
   $browser->visit(’/login’)
   type(‘email $user->email')
   type(“password”, “secret”)
   press ("Login")
   assertPathIs("/home");

});
}
}

From the example showcased above, it can be seen that the browse method does accept a callback.

This callback will automatically receive a browser instance from Dusk and through this instance interactions and assertions against the application will be made.

It is worth noting also that this test also can be used to test the make:auth  Artisan command generated login screen.

Create Multiple Browser for Testing:

It sometimes becomes necessary to have multiple browsers for proper testing to be done. An example of a chat screen that interacts with WebSockets may need testing on multiple browsers.

By going to the signature of the callback provided in the browse method and using “ask” for more than one browser you will be able to create multiple browsers.

$this->browse(function ($first, $second) 
{
$first->loginAs( User::find(1))

   visit{’/home’)
   waitForText (‘Message’);

$second->loginAs(User::find(2)) 

 visit('/home') 
 waitForText('Message')
 type('Message','Hey Taylor') 
 press('Send')

 $first->waitForText('Hey Taylor')
      assertSee('Jeffrey Way');
});

Final Words:

Laravel Dusk brings efficient and easy test writing to your fingertips. We have mentioned the key aspects of Laravel Dusk that you need to know about to get started. On the other side, Laravel Dusk still has plenty to it in relation to features.

Related Posts

Laravel Framework

Laravel v/s Ruby on Rails: In-depth Comparison of Frameworks

Laravel is a modern web application framework, that design web application easily and rapidly. It can perform the customization of the web application with equal ease. It is an open-source and highly used framework, written Read more…

Laravel Framework

Laravel Homestead Benefits as Your Development Environment

  Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine. No Read more…

Laravel Framework

Laravel Shift : Upgrade Laravel Versions Safely

  Laravel Shift is a service that scans your Laravel application and automatically creates a pull request for upgrading Laravel. All you have to do is just choose a repository and pay some money. Some Read more…