Cypress reutrns 302 instead of 200

1.7k views Asked by At

I was trying to test sign-in page of our app. I am using cypress to test Vuejs frontend works with AspNet Api. When I click on the signin button on chrome it makes following requests and visits the homepage "localhost:44389"

first request from Chrome

second request from Chrome

if I want to simulate it on cypress it sends same request but get 302 from second request instead of 200.

first request from Cypress

second request from Cypress

Can someone help me to find out the problem?

Cypress.Commands.add('IdentityServerAPILogin', (email, password) => {
  
  console.log("SERVER_URL is called. Server: " + Cypress.env('SERVER_URL'));
  
  cy.request({
    method: 'GET',
    url: Cypress.env('SERVER_URL'),
    failOnStatusCode: false,
    headers: {
      'Cookie': '...coookies...'
    }
    })
    .then(response => {
      if (response.status == 401){
        console.log ("Check for NOnce");
        console.dir(response, { depth: null });
        const requestURL = response.allRequestResponses[1]["Request URL"]
        console.dir(requestURL, { depth: null })
        //const signInPage = (response.redirects[0]).replace('302: ', '');
        const signInPage = (response.redirects[1]).replace('302: ', '');
        console.log("signInPage:" + signInPage);
        const nOnceObj = response.allRequestResponses[0]["Response Headers"];
        console.dir(nOnceObj, { depth: null });
        const nOnce = nOnceObj["set-cookie"][0];
        console.log("Nonce:" + nOnce);
        cy.visit({
          method: 'GET',
          url: signInPage,
          failOnStatusCode: false,
          headers: {
            //'Cookie': nOnce
          }
        })
        // TODO: Create all needed tests later to test sign in
        cy.get('#username').type(email)
        cy.get('#password').type(password)
        // TODO: Waiting for to click signIn button. When I call the click() method I get infinite loop!!
        cy.get('.ping-button')
        // .click()
        //  .then((response_login) => {
        //     console.log("Status REsponse_Login: "+ response_login);
        //     console.dir(response_login, { depth: null });
        //     if (response_login.status == 401){
        //     cy.visit(Cypress.env('SERVER_URL'))  
        //     }     
        //   })
      }else
        cy.visit(Cypress.env('SERVER_URL'))             
    })  
  console.log("vorbei");
});

Just figured out Cypress is not able to get Cookies from .../signin-oidc, because there is an error as in the photo below

SameSite=Lax Error

Asking kindly for a solution. I am not allowed to make changes on authorization service. Looking for a possibility around cypress.

1

There are 1 answers

0
cd3k On

If you add the option followRedirect to cy.request it'll ignore the first 302 response and pick up on the final resolution instead.

cy.request({
    method: 'GET',
    url: Cypress.env('SERVER_URL'),
    failOnStatusCode: false,
    headers: {
      'Cookie': '...coookies...'
    },
    followRedirect: true, // <-- here
});