abhay singh
abhay singh

Reputation: 3

Want to use result of one request in a second request, for API testing with Cypress

I want to test API endpoint using cypress and want do mocking for AUTH token because it's coming from another API

cy.intercept({
method:'get',
url:'/first endpoint'
},response).as('mocktoken');

cy.request({
method:'get',
url:'/firstendpoint'}).then(response=>{
 cy.request({
method:'get',
url:'/secondendpoint'}).then(response=>{ // assertion statement});

Thhis is not working what can be done to implement this scenario

First, i want auth token from first which I am, trying to mock then using this token I will get the response from second endpoint

Upvotes: 0

Views: 495

Answers (1)

Constance
Constance

Reputation: 537

Don't use cy.intercpet() in this case, it's for intercepting web page requests not test requests.

This is how you can do it most simply

cy.request('/firstendpoint')
  .then(response1 => {
    cy.request({
      method:'get',
      url:'/secondendpoint',
      body: {
        token: response1.token    // check where the token is in the response
      }
    })
    .then(response2 => { 
      ...
    })
  })

There is no mocking, you are using the real thing!

Upvotes: 1

Related Questions