I'm new to cypress.io and I'm trying to check if item in sessionStorage is being set.
How do I check if item in sessionStorage exists?
I've tried it by calling: cy.wrap(sessionStorage.getItem('someItem')).should('exist');
but it looks like the value is being set on initialization because at the first test it says it expected null
and in another test it expects old sessionStorage value (from previous test).
it('can set sessionItem', function() {
cy.visit('/handlerUrl')
// ... some code
cy.get('[type=submit]').click()
cy.url().should('include', '/anotherUrl')
cy.wrap(sessionStorage.getItem('someItem')).should('exist');
})
You can also use
cy.window()
.its("sessionStorage")
.invoke("getItem", "token")
.should("exist");
I had to use cy.window()
which can get the window object of the page that is currently active.
cy.window().then(win=> {
const someItem = win.sessionStorage.getItem('someItem')
cy.wrap(someItem).should('exist') // or 'not.empty'
});
More information about cy.window()
can be found in cypress.io documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With