cy.reload() clears localStorage when it should not
I have a cloud app that loads some elements AFTER the page itself is already loaded completely. So, in order to get that element and to make it visible, I need some kind of soft refresh, UI render or something like that. So, I hoped that cy.reload() will work perfectly, however, I get logged out every time I send cy.reload() command. Any solution/fix for this? Is there any alternative or workaround that I could try out? I'm using Cypress 4.5.0 and running tests in Chrome 83.
cy.reload() is the equivalent of hitting the 'refresh' button in your browser. So, it may be that some state is not persisting through this as you are expecting.
Issues in our GitHub repo are reserved for potential bugs or feature requests. This issue will be closed since it appears to be neither a bug nor a feature request.
We recommend questions relating to how to use Cypress be asked in our community chat. Also try searching our existing GitHub issues, reading through our documentation, or searching Stack Overflow for relevant answers.
@Brestachan We encountered similar issue in our application.
Depending on the way you are handling authentication, this might be your problem as well.
If the Cypress browser has an invalid certificate, any cookies with the secure flag will not work.
So, lets say you hit an endpoint /login in order to log in, and the server that handles that endpoint sends back a Set-Cookie Response Header, which writes the authentication cookie to the browser.
If the Set-Cookie header includes the secure flag and you are not using https or possibly have invalid certificates, the cookie will not be written to the browser, so its almost as if you never logged in at all ( you can inspect the browser cookies and you will see that the authentication one is not there ).
This becomes prominent when the page is refreshed - you will not be logged in, because the browser does not have the cookie in it. However, once you log in, if you don't refresh, you are actually browsing as a logged in user.
I suspect that axios ( which is what I am using ) reads the Set-Cookie Response Header and manually attaches it to further requests, which is why it would work until you have reloaded the page.
At least half of this is theory, so don't get too attached to it. The root cause, I'm assuming, would be the invalid SSL certificate.
I am encountering this issue too.
I use Cognito to authenticate, if during my test I run cy.reload() or cy.visit('/any-url') I get logged out.
Same issue here.
same
Same
same
Me too. Weird thing is, I can run my other with cy.reload() without issues. Any solution?
Same
having the same issue, @cypress where yall at
It might be a related issue, whenever I call cy.reload it deletes the local storage 😢
same here
It might be a related issue, whenever I call
cy.reloadit deletes the local storage 😢
Yes it removes the local storage. So what you can do is add this in your commands.ts file. (I found this somewhere online)
const LOCAL_STORAGE_MEMORY = {};
function localStorageRestore() {
Object.keys(LOCAL_STORAGE_MEMORY).forEach((key) => {
localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
});
}
Cypress.Commands.add('saveLocalStorage', () => {
Object.keys(localStorage).forEach((key) => {
LOCAL_STORAGE_MEMORY[key] = localStorage[key];
});
});
Cypress.Commands.add('restoreLocalStorage', () => {
Object.keys(LOCAL_STORAGE_MEMORY).forEach((key) => {
localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
});
});
Cypress.on('window:before:load', () => {
localStorageRestore();
});
Cypress.on('url:changed', () => {
localStorageRestore();
});
Cypress.on('before:url:changed', () => {
localStorageRestore();
});
I have the same problem. executes cy.reload() in home page (after logging in) and loads the login page again. Hi @ninogomez didnt work for me, Ive added what you said in my local commands.js file and does exactly the same.
Hi @ninogomez , I have the same problem; I added your code in the commands.js, and after using cy. reload() no change in the behaviour. i tried to use cy.restoreLocalStorage() after cy.reload(). Could you please explain how to call cy.restoreLoacalStorage()
same issue with our Application @jennifer-shehane @klausXR @Brestachan
Certificate Does Shows that its invalid and normal Refresh from Browser does not cause this issue
This is specific to Cypress ,Please Try to Address (cy.reload(false) or cy.reload(true) same issue)

Same :(
looks like they don't care at all ( ;
It is working fine for me. The below methods I have kept it in the helpers.js file
/**
* Local storage to run cy.reload() dependency custom commands added in Commands file and e2e.js file.
*/
localStorageRestore() {
const LOCAL_STORAGE_MEMORY = {};
return Object.keys(LOCAL_STORAGE_MEMORY).forEach((key) => {
localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
});
}
/**
* The below custom commands are used to store the localStorage for help to
* retain the same page when the page is reloaded.
*/
saveLocalStorage() {
Object.keys(localStorage).forEach((key) => {
LOCAL_STORAGE_MEMORY[key] = localStorage[key];
});
}
restoreLocalStorage() {
Object.keys(LOCAL_STORAGE_MEMORY).forEach((key) => {
localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
});
}
e2e.js
beforeEach(() => {
helpers.restoreLocalStorage();
})
fterEach(function onAfterEach() {
helpers.saveLocalStorage();
})
In your code, you can call cy.reload(); It should expect to be on the same page when the page gets reloaded.
same here, you can at least add in your documentation that cy. reload() delete the local storage instead of Reload the page as if the user clicked 'Refresh' and let us blindly guess what's going on.
same for me , to solve this I used :
afterEach(()=>{
cy.saveLocalStorage();
})
beforeEach(()=>{
cy.restoreLocalStorage();
})
I'm usually able to get help from Cypress when contacting support (we're paying a LOT of money to them). But in GitHub they seem to have a near limitless arrogance, and they don't seem to use a single calory on even answering questions or acknowledging things they're doing horribly wrong. So complaining in here is most likely a complete waste of time.
@frankhovin There's a lot to keep up with in terms of comments here. We're doing our best!
The use of cy.reload() clears localStorage, regardless if testIsolation is aktiv or not. I would prefer if cy.reload() had an option, for clearing or not clearing localStorage.
This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.
Still, active. Just tested it with [v13.15] https://github.com/cypress-io/cypress/releases/tag/v13.15.0
As @danielSoellerAtBearP already said, issue is still present.
We have given up on Cypress too, and gonr back to Selenium. Every meeting we've had with then, where we've tried taking up issues, they just try to sell us a more expensive plan.
On Sat, 19 Oct 2024 at 05:09, Marco Scabbiolo @.***> wrote:
4 year old very basic issue. There is a company supposed to be behind this project attempting to charge businesses 3.2k anually.
If you are no longer supporting cypress please archive it and stop misleading people trying to grab as much money as possible in the process.
— Reply to this email directly, view it on GitHub https://github.com/cypress-io/cypress/issues/7598#issuecomment-2423530581, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF2ZBVZBMLRK47YYUHCM2Q3Z4HENXAVCNFSM4NTRMMUKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TENBSGM2TGMBVHAYQ . You are receiving this because you were mentioned.Message ID: @.***>
Will there ever be something done for this issue?