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

Revolutionizing Healthcare Data Management with Blockchain Technology

Explore how blockchain technology is revolutionizing healthcare data management. Learn about its applications in electronic health records, health information exchange, clinical trials, and supply chain management. Discover the benefits, challenges, and future trends of blockchain in healthcare.

Life as a Sonnet: Madeleine L'Engle's Poetic Wisdom

Madeleine L'Engle's quote from "A Wrinkle in Time" compares life to writing a sonnet, highlighting the balance between structure and creativity. It emphasizes personal responsibility in shaping our lives within given constraints.

Simplifying Routing with Laravel Folio: A Comprehensive Guide

Laravel Folio simplifies routing by using Blade templates to generate routes automatically. Install Folio, create pages in the 'pages' directory, and enjoy automatic routing. It supports dynamic routes, model binding, and middleware. Ideal for static pages, blogs, documentation sites, and admin panels.