Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MatejKucera's avatar

Dusk is not creating new session after each test

Hello,

I have a trouble resetting session after each test. I use cookies as my session driver (not working with file driver either).

This is my test teardown:

public function tearDown()
{
    parent::tearDown();

    foreach(static::$browsers as $browser) {
        $browser->driver()->manage()->->deleteAllCookies();
    }
}

It clears the cookies successfully, but my users in my application are still logged in through all of the tests.

I've been looking for a lot of answers here and at stackoverflow, but no solution is working for me...

Thanks a lot for any response and have a nice day!

Matej

0 likes
1 reply
myasirhashmi's avatar

I am not sure if you have got it fixed or not but can you check for the typo in your deleteAllCookies method, use

$browser->driver()->manage()->deleteAllCookies(); 

You can also use the following tearDown method in your DuskTestCase.php file

    public function tearDown()
    {
        parent::tearDown();

        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
        });
    }

It is working for me.

1 like

Please or to participate in this conversation.