4

I am trying to pass the result of an API request using the following function:

Add(someName) {
        cy.request ({
            method: 'POST',
            url: someURL,
            body: {
                name: someName
            }
        }).then(function(response){
            return response
        })
    }

When I try to call this function however, it does not give me the content of the response (it gives me Undefined). I thought this probably has something to do with either asynchronosity (if that is a word),or the scope of the object and therefore tried aliasing the response or defining an object outside of the function (then assigning the response to that object), without any luck.

SSpall
  • 139
  • 1
  • 8
JelleSch
  • 41
  • 1
  • 4

4 Answers4

7

You just need a return on the cy.request() call.

Add(someName) {
  return cy.request ({...})
    .then(function(response) {
      return response.body      // maps the response to it's body
    })                         // so return value of function is response.body 
}

The return value type is a Chainer (the same type as all Cypress commands) so you must use a .then() on it

myPO.Add('myName').then(body => ...

You don't need .then() after cy.request()

If you want the full response,

Add(someName) {
  return cy.request ({...})    // don't need a .then() after this 
                              // to return full response
}

How to await the result

If you want to await the result, use a Cypress.Promise as shown here

Add(someName) {
  return new Cypress.Promise((resolve, reject) => {
    cy.request ({...})
      .then(response => resolve(response))
  })
}

Awaiting

const response = await myPO.Add('myName')
user16695029
  • 3,365
  • 5
  • 21
2

You should try returning something in your function:

Add(someName)
{
    return cy.request ({
        method: 'POST',
        url: someURL,
        body: {
            name: someName
        }
    }).then(function(response){
        return response
    })
}

And then get the value returned:

Add('val').then((data) => {console.log(data)})
0

This can be done using cy.wrap!

In the below example you can do anything before the return:

export const add = (someName) => {
  cy.request({
    method: 'POST',
    url: someURL,
    body: {
        name: someName
    }
}).then((resp) => {
    cy.wrap(resp)
      .as('requestResp')
  })

   // Here you can do anything

  return cy.get('@requestResp')
}
  • 1
    Why so complicated? Just return the `cy.request()` as shown above. – Yolandi Nov 06 '22 at 21:36
  • Using this way, you can add cypress commands between the `cy.request` and the `return` e.g. you can add `cy.wait` – Omar salameh Nov 07 '22 at 12:54
  • Wrapping in a promise is a better way to wait, rather than a hard-coded time period. If you add to much other (unspecified) stuff, you are introducing side effects which is bad practice. – user16695029 Nov 08 '22 at 03:43
0

Let's try the following: Return the response from this function,

function getApiResponse(): Cypress.ObjectLike {

     return cy.request ({
        method: 'POST',
        url: someURL,
        body: {
            name: someName
        }
    })
}

And then call it in the required places:

getApiResponse().then(response => {
  cy.log(response);
});
Kanapriya
  • 44
  • 1
  • 6