Hello Readers,
In this blog, we will learn how to perform CRUD Operations with cypress. Let’s start.
What is Cypress?
Cypress is an end-to-end testing framework for web test automation. It enables developers to write automated web tests in Javascript. It is easy to use and learn which makes it perfect for end-to-end testing. Many big companies like Alibaba Travels, CircleCI uses cypress.
CRUD Operations with Cypress:
As we know cypress can automate everything that runs on the browser. Cypress also comes into the picture, when we have a use case where we need to validate our UI behavior against the browser network calls. Here, we have created an API.js file and inside that file, we have defined our test cases for performing CRUD operations.
CRUD stands for Create, Read, Update, and Delete. We will be performing these operations with cypress and also added some assertions to check status and response.
GET Method:
it("get user request", function(){
cy.request({
method : 'GET',
url : 'https://gorest.co.in/public/v2/users',
headers : {'Authorization' : 'Bearer ' + accesstaken
}
}).then((res)=>{
expect(res.status).to.eql(200)
})
})
Here we have used a dummy API “https://gorest.co.in” and defined a GET method through which we are fetching the user’s detail and also added assertion for response status.
POST Method:
let accesstaken = 'b4bd7ed693f7d1a7963c1542004fd1411a6feaddb7cf9c105dfc2511f5eff2db'
let randomtext = ""
let testemail = ""
it("create user request", function(){
var pattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for (var i=0; i<10; i++){
randomtext+=pattern.charAt(Math.floor(Math.random()*pattern.length))
testemail = randomtext + '@gmail.com'
}
cy.request({
method : 'POST',
url : 'https://gorest.co.in/public/v2/users',
headers : { 'Authorization' : 'Bearer ' + accesstaken
},
body: {
"name":"korea12345",
"email":testemail,
"gender":"male",
"status":"Active"
}
}).then((res)=>{
expect(res.status).to.eql(201)
expect(res.body).has.property('email', testemail)
})
})
Here we have defined a POST method in which we are creating a new user and for this, we are generating randomtext for email so that a new user is added into the database every time we run our request as shown in the above code.
PUT Method:
cy.request({
method : 'PUT',
url : 'https://gorest.co.in/public/v2/users/'+userId,
headers : {'Authorization' : 'Bearer ' + accesstaken
},
body: {
"name":"korea12345789",
"email":testemail,
"gender":"male",
"status":"Active"
}
}).then((res)=>{
expect(res.status).to.eql(200)
})
Here we have defined a PUT method for updating the user details.
DELETE Method:
cy.request({
method : 'DELETE',
url : 'https://gorest.co.in/public/v2/users/'+userId,
headers : {'Authorization' : 'Bearer ' + accesstaken
}
}).then((res)=>{
expect(res.status).to.eql(204)
})
Here we have defined a DELETE method for deleting a user.
Here is the full code that contains all the CRUD operations.


How to Run Cypress Tests:
The command for executing our tests in headed and headless mode:
npx cypress run --spec "cypress/integration/API.js" --headed
npx cypress run --spec "cypress/integration/API.js" --headless
The command for executing all our tests available in cypress:
npx cypress open
When running this command on the terminal of Visual Code Editor, a screen will open that contains all the tests and we have to click on the specific test which we want to run like API.js as shown below.

After running the API.js, a browser will open which contains the result and show whether the result gets passed or failed. In the below image, all the tests and assertions get passed.

References:
https://docs.cypress.io/guides/overview/why-cypress
It was very insightful Himanshu. Thanks for sharing.
It was really insightful Himanshu. Keep up the good work 👍!