Welcome to CodeCrew Infotech

shape shape
Shape Shape Shape Shape
Blog

Laravel 11: Introducing Dusk, a Browser Automation and Testing Tool

Laravel 11 brings forth Dusk, a robust browser automation and testing tool designed to streamline the testing process for Laravel applications. With Dusk, developers can effortlessly simulate user interactions, perform end-to-end testing, and ensure the reliability and functionality of their web applications. This powerful addition to the Laravel ecosystem empowers developers to create comprehensive test suites, enhancing the quality and stability of their projects.

1. Create a Project:-

Now, open terminal run this command:

composer create-project laravel/laravel dusk-automation

After Setup Project:

cd dusk-automation

2. Database Configure:-

In the second step, configuring database credentials for the Laravel Livewire CRUD app is essential. Simply update the .env file located in the root directory of the application.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=your_db_username
DB_PASSWORD=your_db_password

3. Install Laravel Jetstream:-

Laravel Jetstream is a powerful authentication scaffolding library for Laravel applications, providing pre-built authentication components and features such as two-factor authentication and API support.
Now, open terminal

composer require laravel/jetstream

 

4. Publish Jetstream Configuration:-

php artisan jetstream:install livewire
npm install
npm run build
php artisan migrate

5. Install Laravel Dusk:-

This allows you to use Dusk features for browser automation and testing during development but excludes it from production builds.

composer require laravel/dusk --dev

After installing the Dusk package, run the 'dusk:install' Artisan command. This command sets up a 'tests/Browser' directory, provides a sample Dusk test, and installs the Chrome Driver binary suitable for your operating system.

php artisan dusk:install

6. Manage Chrome Driver:-

To manage ChromeDriver installations, utilize the 'dusk:chrome-driver' command. This allows installing a custom version of ChromeDriver distinct from the one provided by Laravel Dusk through the dusk:install command.

php artisan dusk:chrome-driver

4. Migrate the tables to the database:-

After creating and updating the blog migration, proceed to migrate the tables into the database by running the following command.

php artisan migrate

5. Install Livewire Package:-

In this step, we'll install Livewire in our Laravel application by executing the following command.

composer require livewire/livewire

6. Modify DuskFile:-

To begin, open your 'tests/DuskTestCase.php' file and make the necessary modifications to the existing code.

<?php

namespace Tests;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\TestCase as BaseTestCase;

abstract class DuskTestCase extends BaseTestCase
{
    use CreatesApplication;

    /**
     * Prepare for Dusk test execution.
     *
     * @beforeClass
     * @return void
     */
    public static function prepare()
    {
        // if (! static::runningInSail()) {
        //     static::startChromeDriver();
        // }
    }

    /**
     * Create the RemoteWebDriver instance.
     *
     * @return \Facebook\WebDriver\Remote\RemoteWebDriver
     */
    protected function driver()
    {
        return RemoteWebDriver::create(
            'http://127.0.0.1:9515',
            DesiredCapabilities::chrome()
        );
    }
}

7. Generating Test:-

Generate tests easily with 'php artisan dusk:make LoginTest' command. This creates a new test file named 'LoginTest' within your Dusk test directory, allowing you to efficiently write and execute tests for login functionality.

php artisan dusk:make LoginTest

Now, open the LoginTest file located at tests\Browser\LoginTest.php and update the following code within it.

<?php

namespace Tests\Browser;

use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTruncation;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class LoginTest extends DuskTestCase
{
    use DatabaseTruncation;
    /**
     * A basic browser test example.
     */
    public function test_basic_example(): void
    {
        $user = User::factory()->create([
            "email" => "taylor@laravel.com",
        ]);

        $this->browse(function (Browser $browser) use ($user) {
            $browser
                ->visit("/login")
                ->type("email", $user->email)
                ->type("password", "password")
                ->press("Login")
                ->assertPathIs("/dashboard");
        });
    }
}

8. Run Laravel Project:-

run the following command:

php artisan serve

 

To run your browser tests, execute the dusk Artisan command:

php artisan dusk

 

Conclusion:-
Laravel 11's integration of Dusk marks a significant advancement in testing capabilities, enabling developers to conduct thorough browser automation tests seamlessly. With Dusk's intuitive features, developers can confidently ensure the reliability and performance of their Laravel applications, ultimately delivering higher-quality software to users.

Thank you...