dolorexxx
dolorexxx

Reputation: 37

How to wait for the cookie value to be updated in Cypress

I'm writing an e2e test and Cypress doesn't wait for the cookie to refresh. When I open the item on the page there is a request for an api, but because the current value of the token in the cookie is out of date, and the new one has not yet come - Cypress crashes due to 401 error.

How can I wait for cookie value to change in Cypress? I tried this, but it didn't work.

 cy.waitUntil(() => cy.getCookie(TOKEN_KEY)
.then(old_cookie =>  cy.getCookie(TOKEN_KEY)
.then(cookie =>  old_cookie.value !== cookie.value))); 

Upvotes: 1

Views: 1672

Answers (1)

dolorexxx
dolorexxx

Reputation: 37

So i solved problem with

let old_cookie='';
cy.getCookie(TOKEN_KEY).then((cookie) => {old_cookie = cookie})
cy.waitUntil(() => cy.getCookie(TOKEN_KEY).then(cookie =>  old_cookie.value !== cookie.value));  

Upvotes: 1

Related Questions