cy.session: Caching across spec files
See original GitHub issueWhat 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 for cypress 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! 🎉
Issue Analytics
- State:
- Created 2 years ago
- Reactions:20
- Comments:15 (4 by maintainers)
Top Results From Across the Web
session - Cypress Documentation
Caching session when logging in via page visit cy.session(name, ... Use this option for a session that will be used multiple times, across...
Read more >Share Data Across Specs Using cypress-data-session Plugin
This video shows how to use the "shareAcrossSpecs" parameter to cache the computed data not just in the current browser spec, ...
Read more >Is there a way to keep session Storage between tests in Cypress
The cy.session() command is a cache - anything inside does not get called multiple times. You need to call it beforeEach() test not...
Read more >Cache session using cy.session() - Medium
session command in cypress cache and restore cookies, local and session storage in order to reduce test ... Running Cypress specs across multiple...
Read more >Flexible Cypress Data Setup And Validation - Gleb Bahmutov
Setting up, caching, and re-creating the test data using cypress-data-session plugin. · Introduction # · Creating the user # · Separate creation ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 As it was said before, most of the time you do not want to clear local storage There might be a flake in cy.session down the road, that might not be fixed asap, so we can’t rely on it(
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
credentials.json
file with the structure: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
cy.session
in that it does a real login if there’s no cached session but if there is a session, it restores that instead.It also saves the session to the
credentials.json
file after a successful login attempt (creates it if it doesn’t exist already).This works across multiple spec files while still working if they run in parallel. The caveat is that each runner will maintain its own
credentials.json
file but I don’t personally think that’s a big deal. If you thought it was a big deal you’d have to write to a central cache that all your runners have access to.