Api Testing Using Cypress : A Comprehensive Guide | Devstringx

Back to Blog
Feature image for cypress API testing blog

Api Testing Using Cypress : A Comprehensive Guide | Devstringx

API Testing Using Cypress

Cypress is a user-friendly testing framework for E2E testing of web applications. It can easily install using the Node Package Manager and does not require additional setup. It sends HTTP requests on behalf of the web applications.

Setting Up the Environment

To Start with Cypress, we have to follow the below-mentioned steps:-

  • Install Node.js
  • Install npm
  • Install Visual Studio code
  • Now Create a folder using the command – mkdir folderName.
  • Open the terminal and set up the node project with the below-mentioned command
  • npm init -y, which will create a package.json file.
  • Execute the command, npx cypress install 

Now the installation will be complete

After Setup, Let’s write our first Cypress API test.

How to Write Cypress API Test?

To make an API request, Cypress provides an inbuilt command called cy. request(). By using this command, Cypress can make an HTTP request on behalf of the web apps and get the response from the API server.

Get Request

Let’s try a simple GET request with the cy.request() command.

Example of cy.request

In this example, you will see ///<reference types=’Cypress’/> at first, This code is used to get the cypress suggestions.

Describe and it is from the mocha library and expects is the assertion used from chai assertions.

So in the cy.request, we need to pass the method, URL, and headers. Methods can be GET, POST, PUT, or DELETE accordingly and responses can be stored in any variable using then(). after that, we can assert our response like status code, etc.

Good to Read:- Introduction of Cypress A Test Automation Tool

Post Request Tests

Now we will talk about post-API automation.

For Post APIs, we need a request body(Payloads) to pass our requests to the server. So the same thing we will simulate through the code.

Here we can pass, Request Body (Payloads) by two types-

  1. Hard quoted data (Directly on test class)
  2. Get test data from a JSON file using fixtures.

1) Hard quoted data (Directly on test class)

image of random string value

3) Get Test Data From JSON File Using Fixtures

Here firstly we need to create a .json file under the fixture folder then we can use this fixture file under our tests by calling them before the describe keyword or using the cy. fixture() method.

const testData=require("../../fixtures/postrequest.json")

image of test data

 

image of request json

Put Request Tests

PUT request uses to update the resource from the server.

For Put Request, we need to use method:’PUT

image of put request 

Delete Request Tests

Delete API is used to delete the resource from the server.

To delete API we need to use method:’DELETE

image of gorest.co.in

Good to Read:- Learn About API testing with REST Assured

How to Run Cypress Tests?

We need to use the Npx cypress open command to run the cypress test.

To run all specs we need to choose our specific spec file or click the Run all specs button

Spec button

cypress framework image

image of cypress integration


Test Your Product to Enhance User Experienced & Productivity of Your Product. Contact Us to Hire India’s Top-Rated Software Test Engineers!

Share this post

Back to Blog