Dusk loginAs methods opens Login page

Published 2 weeks ago by mstdmstd

Hello , In my laravel 5.6.20 application I created new testing file

tests/Browser/documentCategoriesCRUD.php:

<?php

namespace Tests\Browser;

use Auth;
use DB;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use PhpUnit\Framework; // vendor/phpunit/phpunit/src/Framework/Assert.php
use Tests\TestCase;
use App\User;
use App\Settings;

use App\DocumentCategory;


class documentCategoriesCRUDTest_prior extends DuskTestCase
{
    public function testExample()   // php artisan dusk  tests/Browser/documentCategoriesCRUD.php
    {
        $logged_user_id = 5;   // There is user in userrs table in db
        $site_name = Settings::getValue('site_name', '');

        try {


            $document_category_id= '';
            $this->browse(function (Browser $browser) use ($site_name, $document_category_id, $logged_user_id) {
                $browser->resize(1920, 1080); // Width and Height

                $new_document_category_name = 'new document category created  at ' . time();
                $browser->loginAs(User::find($logged_user_id))
                        ->visit('/admin/dashboard#/admin/document_categories/edit/new')
                        ->assertTitle('Document Categories of ' . $site_name)
                ;

            });

            echo ' New document category id::'.print_r($document_category_id,true);

        } catch (Exception $e) {

            throw $e;
        }

        return;

    }
}

But I got error that page if not found and in printscreen I see login(auth model) page.

I have default config/auth.php and in config/database.php:

<?php

return [

    'default' => env('DB_CONNECTION', 'mysql'),

    'connections' => [

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),  // THAT IS MY WORKING DB
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => 'tsk_',
            'strict' => true,
            'engine' => null,
        ],

        'testing' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DUSK_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => 'tsk_',
            'strict' => true,
            'engine' => null,
        ],

As that are first steps with dusk I do not need any special envirement here... What did I miss in dusk config ?

Thanks!

bobbybouwmann

Are you sure the user exists? Dusk won't migrate the database for you as far as I know. So you need to have a full setup database up and running!

mstdmstd

I read user by $logged_user_id, like:

                $loggedUser= User::find($logged_user_id);
                echo '$loggedUser->name::'.print_r($loggedUser->name,true);
                $browser->loginAs(User::find($logged_user_id))

and in the console name of the user with id == $logged_user_id, so I suppose, that:

  1. my testing script works under db I work with
  2. I have user with id == $logged_user_id
  3. Is it so? Any idea ?

Please sign in or create an account to participate in this conversation.