Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
describe ("accessing local storage", function() {
function logLocalStorage() {
Object.keys(localStorage).forEach(key => {
cy.log(key + ": " + localStorage[key])
})
}
// if local storage exists it will be logged out here because afterEach happens after each test completes
afterEach(() => {
cy.log("Logging local storage from _afterEach_:")
logLocalStorage()
})
it ("visits a web page and clicks a link", function() {
cy.visit("https://docs.cypress.io")
.get("a").first().click()
.then(() => {
cy.log("Logging local storage from inside _then_:")
logLocalStorage()
})
cy.log("Logging local storage from body of test:")
logLocalStorage() // Won't log anything as is async
})
it ("visits the web page that interactive user was redirected to", function() {
cy.visit("https://docs.cypress.io/guides/overview/why-cypress.html#In-a-nutshell").then(() => {
cy.log("Logging local storage from inside _then_:")
logLocalStorage()
})
cy.log("Logging local storage from body of test:")
logLocalStorage() // Won't log anything as is async
})
it ("visits the original web page but does not interact with page", function() {
cy.visit("https://docs.cypress.io").then(() => {
/* Won't log anything as there is nothing set. This
was initially unexpected behaviour as it looked like
the user did have local storage set, but it looks like this is
because interactive user gets redirected to a
different page that does set local storage
*/
cy.log("Logging local storage from inside _then_:")
logLocalStorage()
})
cy.log("Logging local storage from main body of test:")
logLocalStorage() // Won't log anything
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment