dusk: Problems with using the loginAs

Environment: Homestead Laravel version: 5.5 Dusk version: 2.0

I have a problem that when running Laravel Dusk I cant use the loginAs method to log in as a user. When I do authentication using

->visit('/login')
->type('email', $user->email)
->type('password', 'password')
->press('Login')

it logs in the user correctly. But when I use ->loginAs($firstUser) that does not work.

I have checked that dusk goes to internal Dusk url when triggering loginAs

$this->visit(rtrim('/_dusk/login/'.$userId.'/'.$guard, '/'));

And that maps to a controller method

    Route::get('/_dusk/login/{userId}/{guard?}', [
        'middleware' => 'web',
        'uses' => 'Laravel\Dusk\Http\Controllers\UserController@login',
    ]);

but method login is never actually run. How can I solve that?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 25 (3 by maintainers)

Most upvoted comments

Closing this issue because it’s already solved, old or not relevant anymore. Feel free to reply if you’re still experiencing this issue.

Hi @driesvints, is there any config expected for dusk?

The first one works as it should be:

    $browser
        ->visit('/login')
        ->type('email', $user->email)
        ->type('password', 'password')
        ->press('Login')
        ->assertAuthenticatedAs($user)

With this one:

    $browser
        ->loginAs($user)
        ->assertAuthenticatedAs($user)

I got:

There was 1 failure:

1) Tests\Browser\CreateOwnerTest::a_owner_is_created
The currently authenticated user is not who was expected.
Failed asserting that null is identical to Array &0 (
    'id' => 1
    'className' => 'App\User'
).

@driesvints I just install Dusk 5.5.5 in an old laravelphp app updated to 6.0.3. loginAs does not log in the given user.

Do you see this route in your route list? php artisan route:list Did you tried go to this address in your browser /_dusk/login/email/? Does it work? Can you put breakpoint to Route::get('/_dusk/login/{userId}/{guard?}'.... and go and see what is going on?

TLDR: Add APP_ENV=dusk to your .env.dusk.local or .env.dusk.{environment} file since the DuskServiceProvider only registers authentication/loginAs routes if the APP_ENV != production. The default APP_ENV if not defined is production. See code link: https://github.com/laravel/dusk/blob/2eaa14a67ef9931fa13dab175f00a6bd7aad8236/src/DuskServiceProvider.php#L17

You can probably set this through phpunit.dusk.xml as well, but if you don’t have APP_ENV set, you’ll get a 404 on those dusk routes:

  • _dusk/login/{userId}/{guard?}
  • _dusk/logout/{guard?}
  • _dusk/user/{guard?}

How I found out: I realized while running my tests in Laravel Sail, the Dusk runner will swap around your .env files. In that deep dive, I enabled video mode for my tests:

sail dusk --browse

And added a pause in my tests:

class CoreSanityCheckPagesTest extends DuskTestCase
{
    public function testExpensesPage(): void
    {
        $this->browse(function (Browser $browser): void {
            $browser->pause(10_000);
            $browser->loginAs($this->user);
            $browser->pause(10_000);

            $browser->visit(new ExpensesPage())->createNewExpense($this->user);
        });
    }

And visited the video URL: http://192.168.214.5:4444/ui#/sessions and refreshed that screen when I ran the sail dusk --browse command.

The session will pop up, there will be a video icon, click on that, and you can enter the password: secret and then view. what’s going on in your tests. I was getting a 404 on the login route: laravel.test/_dusk/login/1 was returning a 404. Dove into source code to understand why those routes weren’t being registered and figured it was the APP_ENV value.

Hope this helps!

Seems to have to do with the fact that I am using cartalyst/sentinel for user authentication considering dusk uses laravel’s auth.

UPDATE

Dusk’s UserController always uses database set in .env and not .env.dusk.local but test cases do, using mysql.

This is what i have tried:

phpunit.xml

      <env name="DB_CONNECTION" value="mysql_testing" force="true"/>
      <env name="DB_DATABASE" value="fastlms_testing" force="true"/>

createApplication()

    $app->loadEnvironmentFrom('.env.dusk.local');

    $app['config']->set('database.default','mysql_testing');
    $app['config']->set('database.connections.mysql_testing.database', 'fastlms_testing');

UPDATE 2

Managed to use my testing db by overriding route to use my own controller with \Sentinel::login($user), which works, but when a try $browser->visit any page I still get redirected to login.