Token Set Gets Removed While Running The Cypress Test
Any idea why the 'token' set in localStorage is removed during cypress test run? I have 'loadToken()' function written in commands.js file, while running the test i could see the t
Solution 1:
This Cypess example recipe shows a slightly different pattern for using tokens in local storage and cy.visit().
// but set the user before visiting the page// so the app thinks it is already authenticatedbeforeEach(functionsetUser () {
cy.visit('/', {
onBeforeLoad (win) {
// and before the page finishes loading// set the user object in local storage
win.localStorage.setItem('user', JSON.stringify(user))
},
})
// the page should be opened and the user should be logged in
})
I presume the onBeforeLoad()
callback is used to overcome the problem you are experiencing where cy.visit() clears down local storage.
So, your code would be something like this
beforeEach(functionsetTokensAndVisit () {
cy.fixture('tokenData.json').then(tokenData => {
cy.visit('/dashboard', {
onBeforeLoad (win) {
// Set cookie or load localStorage with tokenData here,// depending on how your app checks that user is authorized.// In a SPA app, this check is likely to done in the router
},
})
})
})
Post a Comment for "Token Set Gets Removed While Running The Cypress Test"