Test All User Authentication Using Laravel Dusk

I have been experimenting with Laravel Dusk and it’s powerful package for the Laravel application Browser testing.

Recently I have been involved in a Laravel Application Migration to a new version, and after migration I had to make sure that all the current user’s of the application are able to Login to the application and they see the home page.

This task can be done very conveniently using Laravel Dusk and within minutes.

Here is how it goes

Make sure you have Laravel Dusk Package installed and is configured to run properly.

Create a new dusk test using following command

php artisan dusk:make LoginTest

Here is how you can test User Login for all the user’s that exist in your database

<?php

namespace Tests\Browser;

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

class UserLoginTest extends DuskTestCase
{
    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function testAllUserLogin()
    {
        $users = User::all();
        foreach($users as $user) {
            $this->browse(function ($browser) use ($user) {
                echo $user->license_id;
                $browser->loginAs($user)
                    ->visit('/home')
                    ->assertSee('Welcome')
                    ->clickLink('Logout');
            });
        }
    }
}

Sweet ! That’s how you can Test All User Authentication Using Laravel Dusk !

Next up, Dig into and Learn about Different Selectors in Laravel Dusk

Web Stuff Enthusiast.

Leave a reply:

Your email address will not be published.

Site Footer