REST API testing with Cypress
Tags: Cypress, Post, Rest Api, Rest Api Testing, Get

When it comes to using Cypress, many testers think about using it only for front-end testing. But we can use Cypress when writing e2e API tests. If there is, e.g. REST API created in our application, automated API tests should be an integral part of the application’s e2e tests.

In this post, we are going to focus mainly on GET, POST, PUT, PATCH, and DELETE methods.

Before writing tests, make sure that you have the cypress environment all set up. If not, you can follow the steps to do so in this post.

Now, for testing purposes, we will be using an online fake API provided by https://jsonplaceholder.typicode.com/. Let's start with writing tests now.

let's create a new test file (api-tests.spec.js) at path cypress\integration\examples\api-tests.spec.js and let's start writing the tests into the test file.

GET request

it(`GET`, function () {

cy.request({

method: `GET`,

url: `https://jsonplaceholder.typicode.com/posts/1`,

}).then(function (response) {

expect(response.body).have.property(`title`);

});

});

In the above code, we have made a request using cy.request to our endpoint with method GET. For validation of the returned response object, we are checking for the response body to have a property of title in it.

POST request

it(`POST`, function () {

cy.request({

method: `POST`,

url: `https://jsonplaceholder.typicode.com/posts`,

body: {

userId: 10,

title: 'random title',

content: 'random content',

},

headers: {'Content-Type': 'application/json'}

}).then(function (response) {

expect(response.body).have.property(`title`);

expect(response.body).to.contain({

userId: 10,

title: 'random title',

content: 'random content',

});

});

});

In the above code, we have made a POST request to our endpoint. we are providing the body of the content that needs to be added and also the headers for our response. We are then expecting our response to have a property of title to it and also expecting the entire response object to be equal to the one that we added in the post request.

PUT request

it(`PUT`, function () {

cy.request({

method: `PUT`,

url: `https://jsonplaceholder.typicode.com/posts/1`,

body: {

userId: 10,

title: 'random PUT title',

content: 'random PUT content',

},

headers: {'Content-Type': 'application/json'},

}).then(function (response) {

expect(response.body).to.contain({

userId: 10,

title: 'random PUT title',

content: 'random PUT content',

});

});

});

Similar to the POST request, we have written the PUT request. But here, the method inside cy.request is going to be PUT.

PATCH request

it(`PATCH`, function () {

cy.request({

method: `PATCH`,

url: `https://jsonplaceholder.typicode.com/posts/1`,

body: {

userId: 10,

title: 'random patch title',

},

headers: {'Content-Type': 'application/json'},

}).then(function (response) {

expect(response.body).to.contain({

userId: 10,

id: 1,

title: 'random patch title',

body: 'quia et suscipit

suscipit recusandae consequuntur expedita et cum

reprehenderit molestiae ut ut quas totam

nostrum rerum est autem sunt rem eveniet architecto',

});

});

});

Similar to the PUT request, we have written the PATCH request. But here, the method inside cy.request is going to be PATCH.

DELETE request

it(`DELETE`, function () {

cy.request({

method: `DELETE`,

url: `https://jsonplaceholder.typicode.com/posts/1`,

}).then(function (response) {

expect(response.body).to.contain({});

});

});

In the DELETE method, we are just adding the method: `DELETE` inside our cy.request and expecting the returned response object to be an empty object.

You can even verify these endpoints on the postman tool to check their exact response object.

Add these tests into your code, run them, and voila✨. You have successfully tested your REST APIs.

Hope this article has made your task of testing the REST API easiler 😎