Working with Authentication in Laravel Dusk

Laravel Dusk makes it very simple to Automate Authentication (Login / Logout ) of your application. Dusk has provided generic functions so that you don’t have to interact with the Login page and logout link for all of the tests.

Here is how you can use these methods.

Login and Logout in Laravel Dusk

We can make use of Laravel Dusk’s loginAs method to login into the application.

/** @test */
public function testAuthentication()
{
    $this->browse(function (Browser $browser) {
        $browser->loginAs(User::find(1))
                ->visit('/home')
                ->assertSee('Dashboard');
    });
}

Dusk will login you into the application without interacting with the Login page.

Livewire Component Library

loginAs method can accept an user instance or the user-id or the email-id of the user. So all of the below are acceptable

Host Laravel Application on DigitalOcean

Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.
$browser->loginAs(1);
$browser->loginAs('emailid@testemail.com');
$browser->loginAs($user);
loginAs method hits the ‘/_dusk/login/’.$userId.’/’.$guard, ‘/’ URL , which handles the Authentication by calling out Auth::guard($guard)->login($user); code. This all happens in the dusk source code.

Similarly to logout you can call the logout method

/** @test */
public function testAuthentication()
{
    $this->browse(function (Browser $browser) {
        $browser->loginAs(User::find(1))
                ->visit('/home')
                ->assertSee('Dashboard')
                ->logout()
                ->assertGuest();
    });
}

assertGuest is an additional helper function provided by Dusk to check if user is not authenticated.

There are more helper functions provided by dusk

Additional Helper Functions

There are additional helper function provided by Dusk to work with Authentication.

Check if Guest User

As we have seen previously to check if there is no current authenticated user. You can call assertGuest() method

Check if Authenticated User

To check if the user is authenticated and the session is active. You can make use of assertAuthenticated() method.

/** @test */
public function testAuthentication()
{
    $this->browse(function (Browser $browser) {
       $browser->loginAs(User::find(1))
                 ->visit('/home')
                ->assertSee('Dashboard')
                ->assertAuthenticated();
    });
}

Check If Authenticated As

You can also check if the session is Authenticated as a particular user.

/** @test */
public function testAuthentication()
{
    $this->browse(function (Browser $browser) {
       $browser->loginAs(1)
                 ->visit('/home')
                ->assertSee('Dashboard')
                ->assertAuthenticatedAs(User::find(1));
    });
}

Bonus : Using Factory Method to Generate a User and Login

If you are working with a fresh database and using DatabaseMigrations to generate the database structure, you might not have a user available in your database that you can login into the application.

We can make use of factory method provided in the Laravel framework to generate a new user for us.

<?php

namespace Tests\Browser;

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

class authenticationTest extends DuskTestCase
{

    use DatabaseMigrations;

    protected $user;

    public function setUp(){
        parent::setUp();
        $this->user = factory('App\User')->create();

    }

    /** @test */
    public function testAuthentication()
    {
        $user = $this->user;

        $this->browse(function (Browser $browser) use ($user) {
           $browser->loginAs($user)
                     ->visit('/home')
                    ->assertSee('Dashboard')
                    ->assertAuthenticatedAs($user)
                    ->logout()
                    ->assertGuest();
        });
    }
}

That’s All about working with Authentication in Laravel Dusk. Comment below if got more ideas Automating Authentication.

Next up checkout an example of authentication in dusk, where I Test all the user’s authentication using dusk in my application.

Udemy Laravel Dusk Course

Laravel Dusk Beginner to Advanced

Video Course on Automation Testing, Web Scraping and Creating Bots using Dusk

> 60% OFF

Limited Time Offer

Learn More

Site Footer