Introduction Laravel Dusk is one of the tool for browser automation testing for your laravel project. Laravel dusk is introduced in version of Laravel 5.4. Using the laravel dusk is a easy way to test you application or web. It does not required to install JDK or Selenium on your machine. Using Dusk we can automate repetitive tasks, test to make sure your app always works in the real browser.
Installation: Install laravel dusk using following command in your command line.
NOTE: Make sure that your web/application is not in production mode.
Step 1: Download the package using composer
require –dev laravel/dusk
Laravel Automation, Browse Testing using Laravel Dusk
Step 2: Install the package in your project.
php artisan dusk: install The effect of this command, Browser directory is created inside of test directory. Next step to set your APP_URL environment variable in .env file.
Step 3: Run dusk using following command
php artisan dusk
Browser Configuration & Examples After step 3, It will generate DustTestCase.php file in your test folder. There is a one method startChromeDriver in your prepare function. By default it gets the from browse. If you want to run any other browser you can also remove this method.
Example:
public static function prepare() { static::startChromeDriver(); }
You can modify in driver function in same file.
Example:
protected function driver() { return RemoteWebDriver::create( 'http://localhost:4444/wd/hub', DesiredCapabilities::phantomjs() ); }
Login test example:
<?php namespace Tests\Browser; use App\User; use Tests\DuskTestCase; use Laravel\Dusk\Chrome; use Illuminate\Foundation\Testing\DatabaseMigrations; class ExampleTest extends DuskTestCase { use DatabaseMigrations; /** * A basic browser test example. * * @return void */ public function testBasicExample() { $user = factory(User::class)->create([ 'email' => '[email protected]', ]); $this->browse(function ($browser) use ($user) { $browser->visit('/login') ->type('email', $user->email) ->type('password', 'secret') ->press('Login') ->assertPathIs('/home'); }); } }
Conclusion:
Using the Laravel dusk you can develop your project with more accuracy and quality. It reduces the work for QA, and easy way for developers to check errors and fix the bugs. It’s an amazing tool which provided by laravel Development for developing and unit testing.
For any queries !! Just fill up the contact form and our experts will get back to you soon.
About Author
Tarun Bansal - Technical Head
Tarun is a technology enthusiast with a flair for solving complex challenges. His technical expertise and deep knowledge of emerging trends have made him a go-to person for strategic tech initiatives. Passionate about innovation, Tarun continuously explores new ways to drive efficiency and performance in every project he undertakes.