My Request Body is so long. How do you handle it while doing API testing via Cypress

Viewed 48

My request body for an endpoint is so long:

1st question: I read that we can use some request.body.js file for storing our request body and then call it where ever we need it. But unfortunately, I could not find any sample framework/tutorial to learn it.

2nd question: in my project the properties of the request body (especially names of properties) are not exactly matching with the response body that is gaven in the Swagger document. What can be the reason? What would be your approach?

I would appreciate it if you could help me to ridd off the question in the best possible way. Thank you!

1 Answers

It's quite straight forward, take a look at this login example:

cy.fixture('users.json').then((userdata) => {
    cy.request({
        method: 'POST',
        url: <auth_url>,
        form: true,
        body: userdata
    });
});

You can export this as cypress function and then have it available in all your test spec files. users.json file in fixtures folder looks like this:

{
  "username": "...",
  "password": "..."
}

Hope that answers the first question at least.

Related