How to get session data in Laravel Dusk?
Asked Answered
B

2

7

I was wondering why we cannot get the session data when testing with Laravel Dusk ?

In any controller in Laravel we can get session data with Session::get('data');

When we are running a Laravel Dusk test, we take a screenshot with the phpdebugbarr opened (very usefull). We can see on the screenshot that the session is correctly filled with our data, but if we call Session::all(); juste after browser->screenshot() then it return an empty array !

Is it even possible to get the session with Dusk ? I don't find any mention of session managing with Dusk on Google.

Birnbaum answered 12/10, 2021 at 9:6 Comment(2)
Did you ever find an answer to this?Godiva
Laravel Dusk operates on a separate browser instance, meaning it doesn't share the same session as your Laravel application. This isolation is intentional to ensure test independence and prevent unintended interactions between different test cases.Ragtime
W
1

First and foremost it's better to say that sessions are serverside concepts and are different than cookies. As Laravel defined Dusk:

Laravel Dusk provides an expressive, easy-to-use browser automation and testing API.

It means that in the Dusk browser instance, you are reaching the end level of seeing results just by acting as an automation system or as a user. It means that in the rendered part you can access only front-side parts but can't access backend things.

Worthwhile answered 25/12, 2023 at 23:33 Comment(0)
D
1

Accessing session data directly during tests is challenging due to the architecture of Dusk because its browser-based testing. Dusk operates separately from your Laravel application's server-side processes, including session management.

It works as if how an external browser would, ie doesn't have direct access to server-side data like the session. To test specific functionalities that involve session data, you may need to check the changes on a browser leve, like changes in the displayed data or UI , rather than trying to access the session data directly. For more complex actions, indirect methods like API calls or database queries might be necessary to inspect the session state.

Delacruz answered 26/12, 2023 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.