question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cy.session: Caching across spec files

See original GitHub issue

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 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:closed
  • Created 2 years ago
  • Reactions:20
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
romankhomitskyicommented, Dec 3, 2021

@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. Plus, it’s already an optional flag so if you don’t like it you can just not enable. However, it doesn’t make sense to cache cookies if you’re not clearing them.

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(

2reactions
DanJFletchercommented, Feb 1, 2022

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:

{
  "<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 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found