简体   繁体   中英

Cypress : How can we write GET request in with bearer token in cypress?

I have two requests: one is POST request and other is get. In first i get user access token by post and in other i used this accessToken to get login. My code does not work.

I am using window 7 and cypress 3.3.5

my code:

var value;
describe("Login operation", () => {
  it("Login Request with post method", () => {
    cy.request({
      method:'POST', 
      url:'https://odms.baitussalam.org:8445/api/v1/auth/login',
      body: {
        "userName": "faizanj",
        "password": "abc"
      }
    })
      .then(function(response){
        this.value = response.body.accessToken;
        console.log("Value "+this.value);

        expect(response.body.name).to.equal('Faizan');
        expect(response.status).to.equal(200);
      });
  });

  it('Second test case', function() {

    var authHeader='bearer ${'+this.value+'}';
    const options = {
      method: 'GET',
      url: `https://odms.baitussalam.org:8445/api/v1/qurbani-representative`,
      headers:{
        authorization:authHeader,
      }};

    cy.request(options)
      .then((response)=>{
        expect(response.status).to.equal(200);6+9
      });
  });
});

The problem is that you are trying using variable set between test cases when it's already reset to store token you need either use global variable (not advised), or create some login command that will be called before you need access to token. For example:

Cypress.Commands.Add('login', (userName, password) => {
  cy.request({
      method:'POST', 
      url:'https://odms.baitussalam.org:8445/api/v1/auth/login',
      body: {
        userName,
        password,
      }
    })
    .as('loginResponse')
    .then((response) => {
      Cypress.env('token', response.body.accessToken); // either this or some global var but remember that this will only work in one test case
      return response;
    })
    .its('status')
    .should('eq', 200);
})

Then whenever you need you login user before using Cypress.env('token') .

For example:

describe('testing token', () => {
  beforeEach(() => {
    cy.login();
  });

  it('test request', () => {
    const token = Cypress.env('token');
    const authorization = `bearer ${ token }';
    const options = {
      method: 'GET',
      url: `https://odms.baitussalam.org:8445/api/v1/qurbani-representative`,
      headers: {
        authorization,
      }};

    cy.request(options)
      .its('status')
      .should('eq', 200);
  })
});

You can go further and override all request to add token to them like this:

Cypress.Commands.overwrite('request', (originalFn, ...options) => {
  const optionsObject = options[0];
  const token = Cypress.env('token');

  if (!!token && optionsObject === Object(optionsObject)) {
    optionsObject.headers = {
      authorization: 'Bearer ' + token,
      ...optionsObject.headers,
    };

    return originalFn(optionsObject);
  }

  return originalFn(...options);
});

then the above example would look like this:

describe('testing token', () => {
  beforeEach(() => {
    cy.login();
  });

  it('test request', () => {
    cy.request(options)
      .its('status')
      .should('eq', 200);
  })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM