Skip to content
Steven Roland

Getting Started with Laravel Dusk for Browser Testing

Laravel Dusk provides an expressive and easy-to-use browser automation and testing API. It allows you to write tests that interact with your application just like a real user would in a browser. In this post, we'll explore how to set up Dusk and write some basic browser tests.

Setting Up Laravel Dusk

To get started with Dusk, first install it via Composer:

composer require --dev laravel/dusk

Then run the Dusk installer:

php artisan dusk:install

This will create a Browser directory in your tests folder and install ChromeDriver.

Writing Your First Test

Let's write a simple test to verify that the homepage loads correctly. Create a new test file in tests/Browser:

<?php

namespace Tests\Browser;

use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class ExampleTest extends DuskTestCase
{
    public function testHomepage()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/')
                    ->assertSee('Welcome to Laravel');
        });
    }
}

This test visits the homepage and asserts that it contains the text "Welcome to Laravel".

Interacting with Elements

Dusk provides methods to interact with page elements. Here's an example of filling out a login form:

public function testLogin()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/login')
                ->type('email', '[email protected]')
                ->type('password', 'password')
                ->press('Login')
                ->assertPathIs('/dashboard');
    });
}

Waiting for Elements

Sometimes you need to wait for elements to appear before interacting with them. Dusk provides wait methods:

$browser->waitFor('.selector')
        ->click('.selector');

$browser->waitForText('Loading complete')
        ->assertSee('Success');

Taking Screenshots

Dusk can capture screenshots, which is useful for debugging:

$browser->screenshot('filename');

Suggested Usages

  • Form submissions: Test complex forms by filling out fields and submitting.

  • User flows: Simulate entire user journeys through your application.

  • JavaScript interactions: Test dynamic content that relies on JavaScript.

  • Visual regression: Use screenshots to catch unexpected visual changes.

  • Cross-browser testing: Run tests on multiple browsers to ensure compatibility.

Best Practices

  • Use meaningful selectors and avoid relying on generated IDs or classes.

  • Keep tests focused and avoid testing too much in a single test.

  • Use page objects to encapsulate common interactions and improve test maintainability.

  • Run tests in headless mode for faster execution in CI/CD pipelines.

Laravel Dusk is a powerful tool for ensuring your application works correctly from a user's perspective. By incorporating browser tests into your testing strategy, you can catch issues that unit and integration tests might miss.

Remember to run your Dusk tests using the php artisan dusk command. Happy testing!

Support My Work

If you enjoy my content, consider supporting me through Buy Me a Coffee or GitHub Sponsors.

Buy Me A Coffee
or

More posts

Supercharge Your Laravel App Monitoring with Laravel Pulse

Laravel Pulse is a free, open-source performance monitoring tool for Laravel apps. It offers features like user activity tracking, server statistics, queue monitoring, and slow query detection. Use Pulse for performance optimization, user experience improvement, resource allocation, and error tracking. Best practices include securing the dashboard, considering dedicated databases for high-traffic apps, and regularly reviewing insights.

Why Tailwind CSS is a Game-Changer for Maintainable CSS

Tailwind CSS revolutionizes maintainable CSS with its utility-first approach. It offers a consistent design system, rapid development, reduced bloat, improved readability, flexibility, easy responsive design, and a strong community, making it ideal for modern web projects.