0

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 } })
        );
      })
  • 1
    Can you please share the code you used to produce that result? It could be a cookie issue – cypher_null Nov 05 '21 at 08:00
  • I solved my issue with below code (your opinion was very helpfull for me, thanks for that.) – Adrian Góra Nov 07 '21 at 14:43
  • The answer OP is referring to has been deleted. It refers to a different StackOverflow Q/A: https://stackoverflow.com/questions/50471047/preserve-cookies-localstorage-session-across-tests-in-cypress as a similar problem, without explaining the differences. (Probably that is why the answer was deleted, for being a link-only non-answer.) – Yunnosch Feb 16 '22 at 11:17

0 Answers0