Dusk: Problems with using the loginAs

Created on 6 Nov 2017  路  19Comments  路  Source: laravel/dusk

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?

All 19 comments

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?

@unglud I need to test it on Monday, cause the behavior is different on my machines. On laptop it works correctly.

I am having the same issue, and it started in the last couple weeks. These tests were running fine a few weeks ago, and all of a sudden $first->loginAs($user) stopped working.

My issue seems to stem from DatabaseMigrations stopped working. Tables not generating. Saw a note not to use :memory: with Dusk, so I tried it with a sqlite file, still not working.

<?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false"> <testsuites> <testsuite name="Browser Test Suite"> <directory suffix="Test.php">./tests/Browser</directory> </testsuite> </testsuites> <filter> <whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix=".php">./app</directory> </whitelist> </filter> <php> <env name="APP_ENV" value="testing" force="true" /> <env name="CACHE_DRIVER" value="array" force="true" /> <env name="SESSION_DRIVER" value="array" force="true" /> <env name="QUEUE_DRIVER" value="sync" force="true" /> <env name="DB_CONNECTION" value="sqlite" force="true" /> <env name="MONGO_CONNECTION" value="mongodb" force="true" /> <env name="MONGO_DATABASE" value="app_testing" force="true" /> </php> </phpunit>

database.php

'sqlite' => [ 'driver' => 'sqlite', 'database' => env('DB_DATABASE', database_path('database.sqlite')), 'prefix' => '', ],

I feel like this stems from using two DBs in my setup, but not sure why. It is saying:

Caused by InvalidArgumentException: Database (main_db) does not exist.

main_db is what is configured in my .env file, but I am forcing a different connection in phpunit.xml, so not sure how it's still picking up that DB name.

Yup, I'm one of those guys who finds their answer as soon as the question is posted online. Again, it seems my issue stemmed from having two different DBs in the same project. I solved my issue (at least on the first test) by updating tests/CreatesApplication.php. I will report back if I find any other issues.

````

namespace Tests;

use Illuminate\Contracts\Console\Kernel;

trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';

  $app->make(Kernel::class)->bootstrap();

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

  return $app;
}

}
````

I've run into a problem similar to this tonight - loginAs not working at all whilst using sqlite (not an in memory one though, just a normal one).

I read you last post and decided to just try it with an alternative mysql database and it worked.

I don't understand this!

Does nbyloff's workaround fix your SQLite problem?

I'm having the same issue. Seeding works, and from inside the test I can see that the database is created, and the user is created. However, during the actual test execution the table doesn't actually exist.

At the top of login (_vendor/laravel/dusk/src/Http/Controllers/UserController.php_) I put the following:

var_dump(DB::table('users')->exists());

Then I made another test to just go directly to the dusk url ('/_dusk/login/1') and take a screenshot. Sure enough - it's not there (file exists, but no tables exist inside of it. It's like it has no idea the sqlite database was prepped).

I am running inside docker containers, so maybe I'll check if that is part of the issue tomorrow.

Update

So I ended up dumping some information into the UserController from dusk to even see if the SQLITE3 extension was functional and it threw up an exception. I have no idea why, so i rebuilt my php-fpm container, relaunched the others, and it's fine now. No idea whats up with that, compared to yesterdays behavior. I guess it gracefully let itself build despite failing to get the sqlite extension? makes no sense.

I have had the same problem for months now, user exits in db, dusk UserController@login gets runs fine but user just does not get logged in.

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.

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

@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.

Even though I have a .env.dusk.local with a seperate db it is fething the user from the db defined in my main .env file.

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'
).

@RicardoRamirezR I'll need a little more info in order to reproduce that. Can you maybe first try a support channel? And if that fails please open up a new issue and provide a repo that reproduces the problem.

Randomly (maybe when errors in test syntax) i get a screenshot of the login screen (unauthenticated), running the same test again after some time or removing some assertions works.

Randomly (maybe when errors in test syntax) i get a screenshot of the login screen (unauthenticated), running the same test again after some time or removing some assertions works.

may have something to do with my misconfigured error handler, (rollbar) connects to the internet..

One thing I tried was to remove the port number from my env('APP_URL').
That apparently fixed it for me.
Using Laravel 5.5 with dusk 2.0 on MacOS Catalina.

I remembered that my test database always is empty before run tests, so Dusk have not nobody for login. I created a new user before to do loginAs.

public function testLogin()
    {
        $user = factory(User::class)->create();

        $this->browse(function (Browser $browser) {
            $browser->visit('/login')
                    ->assertSee('Forgot Your Password?')
                    ->loginAs(User::find(1))
                    ->visit('/home')
                    ->assertSee('Dashboard');
    });

That worked for me.

For me the issue was in an incorrectly specified sqlite file path. Needed to specify the full file path in "env.dusk.local":

DB_DATABASE=/home/vagrant/code/projectName/database/testing.sqlite

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dellow picture dellow  路  4Comments

hackel picture hackel  路  6Comments

pdbreen picture pdbreen  路  4Comments

bruno-barros picture bruno-barros  路  6Comments

subathanikaikumaran picture subathanikaikumaran  路  3Comments