I carry out a login test and email change that requires authorization, to change the email I need data from the login process. Log in via the UI (enters the input data + click button), then goes to the input and changes the email. I press the button and a 401 error pops up, can the earlier data about the session be saved to avoid 401?.
SOLUTION:
To save data between cypress session, use cy.preserveAllCookiesOnce() inside beforeEach hook.
maybe you need also provide API_KEY in beforeEach hook. You can provide that by cy.intercept. Below some example:
e.g
cy.intercept(/e.g: regex to match url /, req => {
req.headers['your header'] = true;
});
to intercept all request:
cy.intercept(/**, req => {
req.headers['your header'] = true;
});
cy.request({
method: 'GET',
url: '',
headers: { Authorization: `apikey-v1 ${apiKey}` }
}).then(async response => {
cy.task('your task').then(task => {
cy.get('input[placeholder="Enter code..."]').type(task);
});
});
modify request triggered by button:
cy.contains('Confirm')
.click()
.then(() => {
cy.url().then(urlValue =>
cy.visit({ url: urlValue, qs: { add params to match condition for regex } })
);
})