With the development of web applications, test automation has become an indispensable element. In this article, we will explore Laravel Dusk, a powerful tool of the Laravel framework for automated UI testing. Laravel Dusk provides a simple API to run a headless browser to verify that your web application is working as expected by simulating user interaction.
What are the benefits of using Laravel Dusk for automated UI testing?
Next, let’s take a look at how to use Laravel Dusk for automated UI testing in Laravel.
Prerequisites:
Step 1: Install Laravel Dusk
Laravel Dusk is part of the Laravel framework, so we need to install the Laravel framework first. To install Laravel framework, run the following command:
$ composer create-project --prefer-dist laravel/laravel project-name
Next, we need to install Laravel Dusk by running the following command:
$ composer require --dev laravel/dusk
Step 2: Set up Dusk
Once Once you have Laravel Dusk installed, you need to perform a few setup steps before you can start testing.
First, Dusk requires a .env.dusk.local file, which is an extension of a .env file and contains environment variables for testing. You can create a .env.dusk.local file by:
$ cp .env .env.dusk.local
Change the contents in the .env.dusk.local file to suit your testing needs.
Laravel Dusk also requires a SQLite database to store data used during testing. You can create the database by executing the following command:
$ touch database/database.sqlite
Finally, register an accessor in your AppServiceProvider that instructs Dusk to use PHPUnit's ChromeDriver.
public function register() { if ($this->app->environment('local', 'testing')) { $this->app->register(DuskServiceProvider::class); } }
Step 3: Write the test
Now, you can start writing your first Dusk test. To write tests, create a tests/Browser directory and create a new browser test class in it.
Use Artisan Maker to easily create this file, run the following command:
$ php artisan dusk:make LoginTest
Running this command will create a new test class file named LoginTest.php, please update according to the following sample code This file:
<?php namespace TestsBrowser; use LaravelDuskBrowser; use TestsDuskTestCase; class LoginTest extends DuskTestCase { /** * A Dusk test example. * * @return void */ public function testLogin() { $this->browse(function (Browser $browser) { $browser->visit('/login') ->type('email', 'example@domain.com') ->type('password', 'password') ->press('Login') ->assertPathIs('/home'); }); } }
This example tests opening a local application in Chrome, then entering sample data on the login page, clicking the login button, and then verifying that the redirect path is "/home".
Finally, run the following command to run the test:
php artisan dusk
Congratulations! You have now completed the getting started tutorial with Laravel Dusk. Using Laravel Dusk, you can easily write automated UI tests to ensure your application works as expected.
The above is the detailed content of Laravel development: How to use Laravel Dusk for automated UI testing?. For more information, please follow other related articles on the PHP Chinese website!