Consent

This site uses third party services that need your consent.

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!

More posts

Embracing Optimism: The Power of Seeing the Good

Inspired by John Green's quote, this post explores the power of maintaining an optimistic outlook. It challenges readers to embrace the possibility of overwhelming good and provides practical tips for cultivating positivity in daily life.

Laravel Cashier: Stripe vs Paddle - Choosing the Right Payment Solution

This post compares Laravel Cashier for Stripe and Paddle, covering installation, key features, pricing, availability, and developer experience. Stripe offers more control and payment methods, while Paddle provides automatic tax handling and global digital product sales support.