cypress icon indicating copy to clipboard operation
cypress copied to clipboard

cy.reload() clears localStorage when it should not

Open Brestachan opened this issue 5 years ago • 28 comments

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.

Brestachan avatar Jun 05 '20 13:06 Brestachan

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.

jennifer-shehane avatar Jun 08 '20 08:06 jennifer-shehane

@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.

klausXR avatar Apr 01 '21 10:04 klausXR

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.

bilsak18 avatar Apr 20 '21 12:04 bilsak18

Same issue here.

CarolCiolaVV avatar May 05 '21 13:05 CarolCiolaVV

same

alexzavg avatar Jun 09 '21 06:06 alexzavg

Same

iashris avatar Aug 18 '21 00:08 iashris

same

katkarvaishali avatar Sep 02 '21 07:09 katkarvaishali

Me too. Weird thing is, I can run my other with cy.reload() without issues. Any solution?

ninogomez avatar Sep 08 '21 14:09 ninogomez

Same

OlgaShchekina avatar Sep 15 '21 18:09 OlgaShchekina

having the same issue, @cypress where yall at

kdp88 avatar Oct 19 '21 13:10 kdp88

It might be a related issue, whenever I call cy.reload it deletes the local storage 😢

myasul avatar Nov 04 '21 14:11 myasul

same here

karoukastro avatar Feb 21 '22 18:02 karoukastro

It might be a related issue, whenever I call cy.reload it 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();
});

ninogomez avatar Feb 22 '22 04:02 ninogomez

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.

TestSV2019 avatar May 23 '22 05:05 TestSV2019

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) Screenshot 2022-07-29 at 5 02 04 PM

Mufeez avatar Jul 29 '22 11:07 Mufeez

Same :(

darth-stewie avatar Oct 11 '22 12:10 darth-stewie

looks like they don't care at all ( ;

DeeZulu avatar Oct 15 '22 00:10 DeeZulu

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.

IhsenBen avatar Oct 24 '22 14:10 IhsenBen

same for me , to solve this I used :

afterEach(()=>{
      cy.saveLocalStorage();
    
    })
    beforeEach(()=>{
      cy.restoreLocalStorage();
    
    })

NiveenAlSmadi avatar Jan 08 '23 10:01 NiveenAlSmadi

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 avatar Feb 09 '24 14:02 frankhovin

@frankhovin There's a lot to keep up with in terms of comments here. We're doing our best!

jennifer-shehane avatar Feb 09 '24 16:02 jennifer-shehane

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.

danielSoellerAtBearP avatar Apr 02 '24 13:04 danielSoellerAtBearP

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.

cypress-app-bot avatar Sep 30 '24 02:09 cypress-app-bot

Still, active. Just tested it with [v13.15] https://github.com/cypress-io/cypress/releases/tag/v13.15.0

danielSoellerAtBearP avatar Sep 30 '24 05:09 danielSoellerAtBearP

As @danielSoellerAtBearP already said, issue is still present.

Brestachan avatar Sep 30 '24 06:09 Brestachan

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: @.***>

frankhovin avatar Oct 19 '24 07:10 frankhovin

Will there ever be something done for this issue?

Brestachan avatar Mar 14 '25 06:03 Brestachan