Home  >  Article  >  PHP Framework  >  Laravel development: How to use Laravel Dusk for automated UI testing?

Laravel development: How to use Laravel Dusk for automated UI testing?

PHPz
PHPzOriginal
2023-06-13 17:21:27750browse

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?

  • Fast and reliable testing: Laravel Dusk is a fast and reliable testing tool designed to shorten the testing cycle while simplifying the testing infrastructure.
  • Better test coverage: Use Laravel Dusk to test many aspects of your application, such as form validation, user actions, authentication, etc.
  • Simulate actual user interaction: Laravel Dusk tests your application by simulating actual user interaction, so you can make sure your application is actually working the way it's supposed to.
  • Situational testing: You can perform tests through different scenarios. This means you can test many different scenarios, such as testing specific web pages, testing different user roles and permissions, etc.

Next, let’s take a look at how to use Laravel Dusk for automated UI testing in Laravel.

Prerequisites:

  • Laravel 5.4 or higher
  • PHP 7.0 or higher

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn