API testing with cypress

Introduction: 

There are lot of tools to perform API Testing in the current market such as Postman, SoapUI, REST Assured  etc. There are other similar types of tools available. When we are currently switching from mainframe tools to JS based tools even if UI based or API based, there are lots of JS based libraries also available for us such as Axios, Request, Supertest and fetch. Cypress is an emerging tool for E2E UI testing. There is also one major advantage that it provides API testing without installing any new library from npm package. Yes! You heard it right!  

We can use Cypress for API testing directly after installing cypress main package. 

Why Cypress for API? 

It is an open-source, front-end testing tool usually used to automate the test cases of web-based browser applications. It is important and necessary in our project to test API’s before testing an entire regression suite to ensure that the application is stable and to prepare data or to interact with 3rd party servers. 

Pre-requisites: 

Installation: 

  • We will need cypress to be ready.
  • Create or open project directory and navigate into its cypress-API-test. 
  • Setup a new npm package by using command in terminal:  $ npm init 
  • To install cypress, use command in terminal: npm i cypress 
  • Let’s add the following lines to the package.Json for scripts to run our tests.
API tEST
  • To start with cypress run cypress:open command in terminal.

Syntax: 

Syntax

Arguments: 

  1. url (String): The URL is used to make HTTP requests to server.  
  2. body (String, Object): A request body is used as a payload to our request. Cypress sets the ‘Accepts’ request header and serializes the response body by ‘encoding’ option. 
  3. method (String): It is an actual HTTP method. We used to make a request using specific methods. If no method is defined, Cypress uses the ‘GET’ method by default if we didn’t provide any method. There are other main methods available such as: 
  • POST 
  • PUT 
  • DELETE 
  • UPDATE 
  • PATCH  
  • GET 

4. options (Object): It is used to change the usual behavior of cy.request() by passing options as Object data type.  

Some basic options we can send such as:  

  • log 
  • url 
  • method 
  • body 
  • headers 

5. Yields: cy.request() provides the response object after getting response body which gives properties such as: 

  • status
  • body
  • headers
  • duration

Examples:

Let’s start with some code samples with each HTTP method. 

  • POST: Let’s create users with specific request payload using GoREST, through which userId will be retrieved for future purposes. Insert authorization token with mandatory data of user.
Post
  • GET: The GET method is used for reading code from API responses, along with passing parameters for getting specific data. In this example GET method is used to read a specific user by using user-id.
Get
  • PUT: The put method is used to update specific data. It uses some request payload along with bearer token for authorization. 
Put
  • DELETE: The delete method is used for deleting the created record. It needs only one id in base-url along with authorization token as header object. 
Delete

Ok, so as we have tried basic CRUD operations using Cypress. Let’s talk about the Pros and cons. Can we use Cypress for API testing along with UI-E2E tests or not? 

PROS: 

  1. Easy setup and installation.
  2. Live reloads (whenever you make changes to your code, the test will automatically run so you can keep the Cypress Test Runner open, no need to restart or rerun your test manually). 
  3. Great plugins of choice that you can easily install Plugins | Cypress Documentation
  4. Using Cypress, you can verify server response, even manipulate the method implementation as per your need, and can add the required timeout to the response. 
  5. Great documentation available for Cypress. They documented very clearly and explained from basics to advanced level. Community support is also good. 
  6. Cypress provides a customized dashboard feature, where it will show different types of reports and charts, although it is a paid product and there are certain packages available for enterprise level. 
  7. Specifically for API, it will open the browser window and run the entire test case headed, due to this it reduces some execution time which is needed faster in case of API. 

CONS: 

  1. Cypress is currently only supported for Chrome, Firefox, Edge, Brave, and Electron browsers. As a result, Cypress is a less favored option for cross-browser testing. 
  2. Except JavaScript, it doesn’t support other global languages like java, python, C# etc. 
  3. Cypress doesn’t support remote execution. 
  4. Doesn’t support other browsers such as IE11 or Safari.
  5. You can’t test cross domains so sometimes 2FA can be an issue if the login provider is on a separate domain. There are workarounds but it’s trickier to implement.
  6. It doesn’t support multiple tabs but again there are some workarounds.   
  7. It has limited support for iframe/popup handling. 
  8. It doesn’t allow to run tests on two browsers at the same time, and restricts multi-threading execution.