https://cypress.io logo
How to validate a session via local storage?
# i-need-help
e
I am using
cy.session
to login a user and I need to validate the token has been set in local storage, however when using the
validate()
method the result from
cy.getAllLocalStorage()
does not contain the token item - the login is succesful though as the user is redirected as expected and that test passes. Here is my code. Cypress.Commands.add("loginViaUi" as any, (user: any) => { cy.session( user, () => { cy.visit("/public/login"); cy.get("#username").type(user.username, { log: false }); cy.get("#password").type(user.password, { log: false }); cy.get(".ant-btn").click(); }, { cacheAcrossSpecs: true, validate: () => { cy.getAllLocalStorage().then((result) => { console.log("result", result); }); }, } ); });
e
You probably need to check that you’re logged in after the button click. I think it might be trying to validate before the login is actually finished
Usually by asserting on the url or an element that only appears after logging in
e
@echoing-kangaroo-83836 Thanks, yep, that was it 🙂