-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cy.session: Caching across spec files #17710
Comments
This would be incredibly useful for my auth flow, which involves a magic link being sent to an email box and two-factor authentication. The auth takes quite a while to complete, and it would be awesome to cache the result across specs. |
You could do this using cypress-data-session plugin https://github.com/bahmutov/cypress-data-session#videos |
I created a PR to advance discussion on a possible implementation of this. I'd also advocate for possibly removing the cache clearing behavior from run mode as it can be exactly replicated by invoking I looked at the closed roadmap for the session feature, but it differs a fair bit from what we currently have. Is there a planning document for where the team is expecting to take the API of sessions in the future? |
Just curious, wouldn't it be better just add option not to clear local storage, cookies rather than creating huge API that is difficult to pick up? |
@romankhomitskyi You'll probably addressing this question at the Cypress team, but I'd like to answer as someone who's excited about this development. It brings local storage and cookies in line with the Cypress principle of setting up state before each test. This ensures greater test isolation for less flake. |
I am just wondering what kind of flake could be in my flow, where I created bunch of it() to check only one page, and after first it() I get logged out |
Those are questions that are really only answerable by you and your unique application. However, a "bunch of it() to check only one page" is probably not following best practices. But again, Cypress is flexible enough for you to follow the workflow you described. I think a larger discussion about the merits of the API would be more appropriate in another forum. |
In future versions the behavior of Cypress in open mode will match that of run mode.. It seems this desired behavior is out of line with where the API is going. For those interested in pursuing such behavior I can confirm that @bahmutov's plugin provides a solution. However, it seems inadvisable if you may want parallelization at some point for your project, which can deliver large speedups. When parallelized, spec files are routed nondeterministically to different virtual machines. It's not possible (at least as far as I can see) to share state between these machines, therefore each spec file should be written as if it could run in any combination of other spec files running. For our part, we have decided to look back at our architecture to find a different solution that works within this limitation. |
I understand the benefits of parallelization and test isolation, but shouldn't there be some kind of way to set up and store auth state? Especially for complex auth flows that involve sending and checking emails? |
The response on the PR:
It seems a state sharing solution will have to work in parallel mode for it to make it into the Cypress project. Bahmutov's plugin seems like a good solution if that's not a concern for you. |
Ideally, there would be a way to run a |
You could do that in your plugin file and then access the information from the environmental variables. Getting that to work in a parallelized setup would be another matter however. |
I wish Cypress handled this too. There's a couple of options to roll this yourself in a way that works in parallel too. Ultimately you need a file that can be used to persist session data across spec files. With a cypress task you can read/write to files from within a node process. In my case I made a {
"<email + password>": {<session data>}
} Where email and password is concatenated to make a unique key and the value is the session data I need. You can use this data to restore any required state in localstorage or use it as a way to mock the login request. I updated my login command to behave similar to It also saves the session to the This works across multiple spec files while still working if they run in parallel. The caveat is that each runner will maintain its own |
The code for this is done in cypress-io/cypress#23904, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
What would you like?
The new cy.session is amazing and just what we wanted - the behaviour with
cypress open
where this feature caches across spec files is something we'd like to have as an option forcypress run
as well if possible.Why is this needed?
In many cases authentication sessions are reliably the same between spec files, and starting with a blank cy.session cache for each file isn't necessary. I understand that the reason may be more technical, since the runner may be opening each spec file in a brand new context where it's hard to carry session data around.
Other
Just further thanks for cy.session! 🎉
The text was updated successfully, but these errors were encountered: