Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

emabusi's avatar

Laravel Dusk example Auth test not workin

I the Creating Browsers example from https://laravel.com/docs/5.4/dusk and the tests keeps failing. I can see the browser open typing but on trying to authenticate its unable to authenticate credentials.

I have the .env.dusk.local setup with two different DBs but still not working


<?php

namespace Tests\Browser;

use emad\User;
use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class AuthBroswerTest extends DuskTestCase
{

    use DatabaseMigrations;

    /** @test **/
    public function user_can_login()
    {
        $user = factory(User::class)->create([
            'email' => '[email protected]'
        ]);

        $this->browse(function ($browser) use ($user) {
            $browser->visit('/login')
                ->type('email', $user->email)
                ->type('password', '1qazwsx')
                ->press('login')
                ->assertPathIs('/');
        });

    }
}

@JeffreyWay I have seen a lot of people with this problem here and stackoverflow, kindly assist... like http://stackoverflow.com/questions/42643509/laravel-dusk-phpunit-failing-to-test-login

0 likes
5 replies
impbob36's avatar

You create a user but you didn't set a password.

$user = factory(User::class)->create([
    'email' => '[email protected]',
  'password' => bcrypt('1qazwsx')
        ]);

The password in bcrypt should obviously be the same as the one you use in the test

emabusi's avatar

@impbob I had already set the password in the model factory..,, I have tried to set it up too as suggested and test the login, it still types but fails to authenticate

tsaicharlie93's avatar

+1. I have the user account and password already in database too. But it just doesn't redirect after login is pressed

impbob36's avatar

Heres step by step what I did to get it working on a new fresh install.

Install fresh copy of Laravel -> laravel new dusk-login

Add test

Run test: error -> Command not found

FIX: Install dusk -> composer require laravel/dusk

Run test: error - .../Browser/screenshots" directory does not exist

FIX: Setup dusk -> php artisan dusk:install

Run test: error -> SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

FIX: Setup database: edit .env

CHECK: Confirm database is working -> php artisan migrate

Run test: error -> Unable to locate factory with name [default] [emad\User].

FIX: Rename User class for default setup: use emad\User; -> use App\User

Run test: error -> no such element: Unable to locate element: {"method":"css selector","selector":"body textarea[name='email']"}

FIX: Use default template to quickly generate stuff php artisan make:auth

Run test: error -> no such element: Unable to locate element: {"method":"css selector","selector":"body textarea[name='email']"}

CHECK: Visit page in browser: php artisan serve" -> Visit http://127.0.0.1:8000/login

Error: No application encryption key has been specified.

FIX: Set key -> php artisan key:generate -> composer dumpautoload -> restart server

  • Can see page in browser

Run test: error -> no such element: Unable to locate element: {"method":"css selector","selector":"body textarea[name='email']"}

FIX: Change .env server correct url APP_URL=http://127.0.0.1:8000

Run test: error -> Unable to locate button [login]

  • Default example test now passes FIX: Update test for case-sensitive button label: ->press('Login') Run test: Passes ... but ...

Test doesn't actually check for login user so ...

FIX: Update test -> ->assertSee('You are logged in!')->assertSee($user->name)

Run test: Did not see expected text [You are logged in!] within element [body].

FIX: Screenshot directory shows a password mismatch error. Ensure user email / password match

        $password = '1qazwsx'; // Our password

        $user = factory(User::class)->create([
            'email' => '[email protected]',
            'password' => bcrypt($password) // Save the encrypted
        ]);

        $this->browse(function ($browser) use ($user, $password) {
            $browser->visit('/login')
                ->type('email', $user->email)
                ->type('password', $password) // Enter plain password
                ->press('Login')
                ->assertPathIs('/')
                ->assertSee('You are logged in!')
                ->assertSee($user->name);
        });

Run test: pass

1 like

Please or to participate in this conversation.