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

REST API Testing with Cypress
38.55 GEEK