What you’ll learn?

  • What is Cypress?
  • How to test APIs using Cypress.
  • Sample code to test APIs for the basic API methods.
  • Complete execution flow.

Cypress is a Java-Script automation framework that is built on top of Mocha which is a feature-rich java-script test framework.

  1. Install Node.js & NPM (By default, installing Node.js installs NPM too)
  2. Install Cypress
  3. Install Mocha
  4. Install Visual Studio Code
  5. Install the Chai assertion library.
    Click on the respective links above to install all the prerequisites. When you’re done with the installations come back here to write some code.

If you’re coming from the Java & Selenium world then on a high level we can compare as follows:
1.  Node.js is like JRE. How JRE provides us a run time environment for Java, similarly, Node.js provides us a run time environment for JavaScript.
2. NPM is like Maven. How Maven is a build and dependency resolution tool for Java, similarly, NPM is for JavaScript.
3. Mocha is like TestNG. How TestNG is a test framework for Java, similarly, Mocha is for JavaScript.
4. Cypress is like Selenium. How Selenium is an automation framework for web app testing using Java, similarly, Cypress is for JavaScript.

When we talk about API testing, the basic methods we use are GET, POST, PUT, and DELETE. Here, I am considering the reader is having a sample API request in hand for our practice.

Below is my current project structure in Visual Studio Code. Here, I have created a folder called ‘API_Test’ and created a file as ‘role.js’.

The API I will be using here is the ‘Role’ API which includes Create a role (POST method), Update role details (PUT method), Get role details(GET method) and Delete role (DELETE method)

Let’s see a sample POST request to create a new role.

  • describe: One or more related tests can be written within the describe block.
  • it: it is actually a test within a describe block
  • expect: Use to construct assertions.
  • cy.request: Make a HTTP(S) request via cypress.

1. The below command is used to open the Cypress GUI.

2. When the GUI opens, double click on the role.js file.

3. On clicking, it will open a chrome browser and will execute our POST request.

CYPRESS GUI output for POST request:

A new role is created and the server will return the roleID as part of the API response. As we have added a couple of API validations in our code those will be executed and shown in the Cypress GUI as below.

(Sensitive information is blurred in the above screenshot)

Note: Once the Cypress GUI is launched and is active, from the next save onwards in your visual studio code editor, Cypress will automatically execute the saved scripts. 

Now let us see similar execution flow for PUT, GET, and DELETE requests.

PUT: Here we will add a new permission to the role created in the previous step.

CYPRESS GUI output for PUT request:

On successful, the API returns the ‘SUCCESS’ message with the roleID in the response. We will get to see in the response of the GET request, whether the permission we added above is successfully added or not.

(Sensitive information is blurred in the above screenshot)

GET: Here we will verify all the details of the role created as part of POST request including the newly added permission as part of PUT request.

CYPRESS GUI output for GET request:

(Sensitive information is blurred in the above screenshot)

DELETE: Here we will delete the role we have created earlier and validate the API response.

CYPRESS GUI output for DELETE request:

(Sensitive information is blurred in the above screenshot)

This is how we can validate our APIs for all the basic API methods. We can always enhance our code and add more assertions to our APIs to make sure APIs are working as expected.

This is just a short guide to kick start the API testing using Cypress and I hope the reader will explore more about Cypress from here.

In my next blog, we will learn how to automate APIs using Cypress. Till then, happy learning. 🙂