0
0
Laravelframework~5 mins

Browser testing with Dusk in Laravel

Choose your learning style9 modes available
Introduction

Browser testing with Dusk helps you check if your website works correctly by simulating real user actions in a browser.

You want to make sure buttons and links on your website work as expected.
You need to test user login and registration flows automatically.
You want to check if forms submit and show correct results.
You want to catch bugs before users find them by testing in a real browser.
You want to automate repetitive manual testing tasks.
Syntax
Laravel
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

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

Tests extend DuskTestCase to use browser features.

The browse method runs browser actions inside a closure.

Examples
This test fills in login form and checks if redirected to home page.
Laravel
public function testLogin()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/login')
                ->type('email', 'user@example.com')
                ->type('password', 'secret')
                ->press('Login')
                ->assertPathIs('/home');
    });
}
This test clicks a link and checks if the new page shows expected text.
Laravel
public function testClickLink()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/')
                ->clickLink('About Us')
                ->assertSee('Our Story');
    });
}
Sample Program

This simple Dusk test opens the home page and checks if the text "Welcome to Laravel" is visible.

Laravel
<?php

namespace Tests\Browser;

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

class HomePageTest extends DuskTestCase
{
    public function testHomePageLoads()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/')
                    ->assertSee('Welcome to Laravel');
        });
    }
}
OutputSuccess
Important Notes

Make sure to run php artisan dusk:install once to set up Dusk.

Run tests with php artisan dusk to see browser actions live.

Dusk uses ChromeDriver, so Chrome must be installed on your machine.

Summary

Dusk lets you test your website by controlling a real browser automatically.

Write tests to visit pages, fill forms, click buttons, and check results.

Run tests to catch problems early and keep your site working well.