skip to Main Content

How can I save the bearer token and use it in another ‘IT’ blocks? please help I am new to cypress and JavaScript.

let Bearer_token
describe(‘get new API,function (){

it("get api request", function(){
    
    cy.request({

        url : 'whatever url',
        method : "POST",
        body : {
            userName : 'saurabh.sharma.com',
            password : 'Indigo@123'
        }

    }).its('body').then((body) => {
        expect(body.status).to.eq('success')
        let A_token = body.data.token
        // cy.wrap(A_token).as('Bearer_token')
        
        cy.log(body.data.token).as('Bearer_token')
        })
    cy.log(this.Bearer_token)
    
    
    
    cy.request({

        url: 'whatever url',
        method: "GET",
        headers: {
            'authorization': 'Bearer ' + this.Bearer_token,
            'client-id': '00'
        }
    }).then((res)=>{
        expect(res.status).to.eq(200)
        var count = res.body.data.totalCount
        cy.wrap(count).as('count')

        })
    cy.log(this.count)   
    })
    

3

Answers


  1. See the hooks section of the Writing tests page in the documentation.

    Use a before hook to fetch the bearer token and assign it to a variable in the top level scope of the test module.

    let bearer;
    
    before(() => {
        cy.then(async () => {
            // ...
            bearer = ...;
        })
    });
    
    Login or Signup to reply.
  2. You can use the Cypress.env variable to save and use the token globally –

    it('get api request', function () {
      cy.request({
        url: 'whatever url',
        method: 'POST',
        body: {
          userName: 'saurabh.sharma.com',
          password: 'Indigo@123',
        },
      })
        .its('body')
        .then((body) => {
          expect(body.status).to.eq('success')
          Cypress.env('Bearer_token', body.data.token)
        })
      cy.log(Cypress.env('Bearer_token'))
    
      cy.request({
        url: 'whatever url',
        method: 'GET',
        headers: {
          authorization: 'Bearer ' + Cypress.env('Bearer_token'),
          'client-id': '00',
        },
      }).then((res) => {
        expect(res.status).to.eq(200)
        var count = res.body.data.totalCount
        cy.wrap(count).as('count')
      })
      cy.log(this.count)
    })
    
    Login or Signup to reply.
  3. The alias method is correct, but don’t use it in conjunction with cy.log() as that command does not return the value logged.

    ...
    }).its('body').then((body) => {
      expect(body.status).to.eq('success')
      cy.wrap(body.data.token).as('Bearer_token')
      cy.log(this.Bearer_token)
    })
    cy.log(this.Bearer_token)
    

    To use the alias in further tests, use the same function() format

    it("get api request", function(){
      ...
      cy.wrap(A_token).as('Bearer_token')    
      cy.log(this.Bearer_token)
      ...
    })
    
    it("uses api result", function(){
      ...
      cy.log(this.Bearer_token)              
      ...
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search