Cypress: Can't access to localStorage items when testing an app on HTTPS

Created on 6 Nov 2018  路  1Comment  路  Source: cypress-io/cypress

I am currently trying to read an item from localStorage (saved by the tested app on HTTPS) for several hours inside the code of a test (the reason is to set the same item on the beginning of the next test) but without any success.

If I do the same by the same test against the same app but on just HTTP protocol, everything works as expected. I am using the standard syntax:

JSON.parse(localStorage.getItem(name))
localStorage.setItem(name, JSON.stringify(obj))

I am not sure if it's a bug or just the principal limitation. It's clear that code from HTTP can't access localStorage items saved by code under HTTPS, but as I know Cypress uses techniques to run tests under same port (here it's HTTPS) as the tested site, so strictly technically it should be possible (it's just the necessity if you need to set items in localStorage for the next tests because of the clearing up cookies and localStorage by Cypress before of each test).

Environment:
Cypress: 3.1.1
Chrome: 70
Windows: 10
NodeJS: 10.2.1

Most helpful comment

... the problem was using the sync access to localStorage instead of an async way. With the following code it works as expected:

cy.window().then(
   window => console.log(window.localStorage.getItem('...'))
);

>All comments

... the problem was using the sync access to localStorage instead of an async way. With the following code it works as expected:

cy.window().then(
   window => console.log(window.localStorage.getItem('...'))
);
Was this page helpful?
0 / 5 - 0 ratings