REST API Testing with Cypress

REST API Testing with Cypress

Hello, everyone! Welcome to my blog section, Here we will see how to to do REST API Testing with cypress. REST API Testing with Cypress: Cypress automated

REST API Testing with Cypress:

Cypress automated everything that runs on the browser and many times we have a use case where we need to validate our UI behavior against the browser network calls, here cypress comes in the picture. So as per the cypress best practices we have created a REST-API-Testing.spec.js file and inside that spec.js file, we have defined our test cases for performing CRUD operations.

C: Create

R: Read

U: Update

D: Delete

We have also added some assertions on the response as we used to do while testing backend API(s) with the different rest clients.

GET Method:

 it('GET-list user',()=>{
        cy.request('GET','/users?page=2').then((response)=>{
            expect(response.status).equal(200)
            expect(response.body.data[0].first_name).equal('Michael')
            expect(response.body).to.not.be.null
            expect(response.body.data).to.have.length(6)
        })
    })

here we have defined a get method through which we are fetching the user’s detail.

POST Method:

it('POST-Create user',()=>{
        var user = {
            "name": "Vandana Yadav",
            "job": "QA"
        }

        cy.request('POST','/users',user).then((response)=>{
            expect(response.status).equal(201)
            expect(response.body.name).equal(user.name)
            expect(response.body.job).equal(user.job)

        })
        cy.request('POST','/users',user).its('body').should('include',{name:'Vandana Yadav'})
    })

here we have defined a POST method in which we are creating a new user.

PUT Method:

it('Ùpdate user',()=>{
        var user1 = {
            "name": "Samantha",
            "job": "DevOps"
        }

        cy.request('PUT','/users/2',user1  ).then((response)=>{
            expect(response.status).equal(200)
            expect(response.body.name).equal(user1.name)
            expect(response.body.job).equal(user1.job)
        })
    })

here we have defined a PUT method for updating the user’s detail.

Delete Method:

it('Delete user',()=>{
        var user1 = {
            "name": "Samantha",
            "job": "DevOps"
        }

        cy.request('DELETE','/users/2').then((response)=>{
            expect(response.status).equal(204)

        })
    })
})

here we have defined a Delete method for deleting a user.

Running Cypress Tests:

We can execute our cypress tests as per our requirement, like if we want to run our test cases on the browser then we need to pass an argument –headed along with our cypress run command and if you want to run your tests on console only then you can pass –headless along with cypress run command. 

The command for executing our tests in headed mode:

npx cypress run --spec "cypress/integration/REST-API-Testing.spec.js" --headed

The command for executing our tests in headless mode:

npx cypress run --spec "cypress/integration/REST-API-Testing.spec.js" --headless

Html Report:

scala cypress cypress.io rest api testing restapi ios

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

What is REST API? An Overview | Liquid Web

What is REST? The REST acronym is defined as a “REpresentational State Transfer” and is designed to take advantage of existing HTTP protocols when used

How to Do API Testing?

API endpoint when you send the get request to that URL it returns the JSON response. In this article, I am going to use postman assertions for all the examples since it is the most popular tool. But this article is not intended only for the postman tool.

Consume Web API Post method in ASP NET MVC | Calling Web API | Rest API Bangla Tutorial

LIKE | COMMENT | SHARE | SUBSCRIBE In this tutorial, I will discussed about how to consume Web API Get method and display records in the ASP.NET View. Here, ...

Consume Web API Get method in ASP NET MVC | Calling Web API | Rest API Bangla Tutorial

LIKE | COMMENT | SHARE | SUBSCRIBE In this tutorial, I will discussed about How to Consume Web API Get method in ASP NET MVC. Blog : http://aspdotnetexplorer...

Consume Web API Put method in ASP NET MVC | Calling Web API | Rest API Bangla Tutorial

LIKE | COMMENT | SHARE | SUBSCRIBE In this tutorial, we learned how to consume Web API Get and Post methods in the ASP.NET View. Here, we will see how to con...