What you’ll learn?

  • How to automate APIs using Cypress?
  • How to handle dynamic data and request chaining in Cypress using Fixtures?

Note to reader: If you’re new to Cypress then, I highly recommend you to refer my previous blog  “Test APIs Using Cypress”   which helps you better, in understanding what is Cypress, requirements needed for the setup, how to use Cypress effectively to test APIs manually and the execution steps in detail.

When we talk about API automation, one of the road blocks we face is handling dynamic data and request chaining.

What is “Dynamic Data” and “Request Chaining”?

When we work with APIs, the flow normally goes like this:
1. Send a request to the server.
2. Server provides a response with the data.
3. Store the data in some variables/files.
4. Reuse the data stored in variables/files for the subsequent requests.

In layman’s terms, output of the one request, is the input for other request and the way we chain the two or more requests is called “Request Chaining”.

When we talk about API responses, response will be either a static message such as server sending the response as “SUCCESS” or, sometimes server sends dynamic data such as IDs. For every request we send to the server, if the response data changes dynamically then this type of data we call “Dynamic data”.

One of the key area to be focused on API automation is, how to handle “Dynamic Data” and “Request Chaining”. By end of the blog I promise you will understand all the topics we discussed till now.

What is a fixture and how fixtures helps us in handling dynamic data and request chaining?

A fixture is nothing but a sample data file which is used to store data and make use of this sample data file where-ever required. In our case, we use this fixture file to store the response data we get from the server. Know more about fixtures here.

For easy understanding and keeping the readers in mind who has gone through my previous blog “Test APIs Using Cypress” , let us use the same API end point and same API methods here too.

API we will be using here will be ‘role’ API.

API methods we will be chaining will be Create a role (POST method), Update role details (PUT method), Get role details(GET method) and Delete role (DELETE method).

Below is my current project structure for your quick reference and the file I have created to perform CRUD operations on role API is ‘role.js’ and also created a fixture file under ‘cypress/fixtures’ called ‘post_response.json’ to store the response of the POST request we get from the server.

Let me quickly explain what the below code does in step wise and the API execution flow.

  1. POST Request: Create a role using POST method by sending all the required details in the request body and store the response in a fixture file i.e., ‘post_response.json’ and validate the basic API validations.
  2. PUT Request: Extract the roleID from the above fixture file and make use of the roleID data as part of PUT request to update/add the permissions for the role we created using POST request. Validate the roleID data in the fixture against the server response along with the basic API validations.
  3. GET Request: Extract the roleID from the above fixture file and make use of the roleID data as part of the GET request to fetch the details of the role from the server. Validate the roleID data in the fixture against the server response along with the basic API validations.
  4. DELETE Request: Extract the roleID from the above fixture file and make use of the roleID data as part of the DELETE request to delete the role created using POST request and validate the basic API validations.

Code goes as follows:

Most of the above code is simple and self explanatory. Couple of key points to be discussed here are:

  1. The below line written in POST request will write the response of the POST request to a file in our case ‘post_response.json’. Screenshot of the fixture file is shown below

(Sensitive information is blurred in the above screenshot)

2. Before we send our PUT/GET/DELETE request to the server we need to load the fixture to extract the roleID. Below is the line we used to load the fixture.

3. Request URL of PUT, GET & DELETE request is using dynamically created roleID from the fixture file i.e., ‘post_response.json’ as below

Note to the reader: Here we have used $ symbol to append dynamic data to the URL and the URL is written within backquotes(`) not single quotes(‘)

Lets see the Cypress Output:

image-1: Overall output(assertions details collapsed)

image-1.1: POST request output in detail.

image-1.2: PUT request output in detail.

image-1.3: GET request output in detail.

image-1.4: Delete request output in detail.

(Sensitive information is blurred in the above screenshot)

Congratulations!!! Now you know the basics of Cypress API automation.
You can always improve the above code by adding more data in the fixtures or by adding more API validations to make sure the APIs are working as expected.

Thanks for reading till here and I would love to hear your questions and suggestions in the below comment section.

Happy learning & happy blogging 🙂