I'm testing a website that returns token in body from login request. Then all the requests made after we logged in use this token value in header.
I made a custom cypress command that logs in by sending a POST request, but i can't get how to make it push the token to all requests inside my app during test run.
This is a command:
Cypress.Commands.add('login', () => {
cy.request({
method: 'POST',
url:'site/login',
form: true,
body:{
"username":creds.login,
"password":creds.password
}
}).then((resp) => {
const token = resp.body.token
cy.intercept('*', (req) => {
req.headers['token'] = token
})
})
})
In the part after .then((resp) => {...
i'm trying to intercept all requests and add token to their headers. I can see that requests are intercepted but i'm still logged out from my app.
Tried this solution, but also didn't work.
How can i get token from response body and add it to all requests in cypress?