Laravel Dusk Sessions on

710 views Asked by At

I have a laravel 5.4 project set up on a vagrant machine and I'm trying to create Laravel Dusk tests on it. It was a bit tricky and I followed the tutorial here : http://www.jesusamieiro.com/using-laravel-dusk-with-vagrant-homestead/

It runs fine, but the session variables don't seem to be saving. I have this code in a post route

if (!session('user_name')) {
        $request->session()->flash("message", "Your session expired. Click "Please Confirm" to approve your monthly donation.");
        return redirect()->back();
    }

and it is set on the get request when the page is first fetched. I have this test in dusk:

$browser->visit("http://support.welcome.dev/?layout_id={$layout}&purl={$supporter->purl}&user_name={$supporter->name_first}&amount=10&ct={$hash}")
                ->press("Please Confirm")
                ->assertPathIs("/confirm");

This test fails and is redirected with the message session has expired. However, it works just fine when when the code in the post route checking for the user_name variable is commented out. The original code works when I test it manually on a browser.

Any help is appreciated.

1

There are 1 answers

1
xuan hung Nguyen On
$user = factory(User::class)->create([
            'email' => '[email protected]',
        ]);

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

This code is write on https://laravel.com/docs/5.4/dusk. i think this can help you. =)