Laravel 5: Testing – Laravel Dusk testing – login shortcut

Cheatsheet: Laravel Dusk testing – login shortcut

Create default user and quickly use it to login. However, there is a problem with this approach.

If you look into /test/Browser folder, you will see another called: /Pages.
Here’s what Laravel docs (5.6) have to say about this.

Scroll slightly below and you will see this Artisan command:

When you run it, it will create class Login inside of /Pages.
This will be our default user login shortcut class.

But it is not ready yet.
We have to prep it a bit to serve its purpose.
We will add loginUser() method to it.
This will be a method, that will be used behind scenes to log in defined there default user.

Before I go to this, a word of caution: do not name your login method login(), but anything else – I used loginUser(), as login() name goes head on with another, Laravel native login() method and is being overridden, giving you error like this, when you attempt to run your test:

Login methods clash was not spotted by me. All credit due is here

Our Login class with changes is below.
A few tidbits in method comments to make things easier to understand – when you learn (or forgot something), nothing seems to be obvious.

And here is Login class with changes:

Now, how to use it.

There is a PROBLEM with this approach.

It is good, if you use Laravel out of the box single level authorization, which is good for blog, when supported by user roles.

Simply you have single single admin and allow access rights based on roles.

In most cases I worked on, I needed 3 separate site areas, with two, that needed authentication.

Take ecommerce: backend admin / customer admin.
Take any company website, that allows users to have accounts: backend admin / customer admin.

You could try roles, but good luck with that.

You need multi-level authentication capacity
Note: what you find at the end of the link listed above, was written for Laravel 5.2 and may need some touchups.

Laravel does not have multi-level auth out of the box (as of 5.6)

So, if you are using Laravel provided single level authentication, you are in luck.

You can use this method described above.

You can also use methods stored in here:


If you are trying to use these methods and getting error to the tune of this:

… you have to override protected user() method from vendor\laravel\dusk\src\TestCase.php in:

  • your TestClass that extends tests\DuskTestCase, or
  • in tests\DuskTestCase.php itself

Whichever serves you best.

Class tests\DuskTestCase seems best, as all your TestClasses will extend it, so you do it once and use it from now on.

Now, as I mentioned these methods are rigged to serve Laravel built in authentication.

Just look into: \vendor\laravel\dusk\src\DuskServiceProvider.php and you’ll see rigged routes, like:

Then these routes work with hardcoded methods in vendor\laravel\dusk\src\Concerns\InteractsWithAuthentication.php:

You could set this for your needs in DuskServiceProvider, but that would also require rewriting methods in vendor\laravel\dusk\src\Concerns\InteractsWithAuthentication.php, which is nothing short of messing with core files, that can be replaced in next framework update.

Not really recommended.

You could overload these methods, but then, why not writing your own 😉

Better use other means of checking login.

They are all described in Tests section.