QA Automation

API Automation Testing Using Axios with JavaScript  

API testing  axios with js

Introduction:

Axios is a promise-based HTTP client for the browser as well as Node.js. Using Axios we can perform CRUD operations easily and, thus, it can be used in applications that use libraries such as React or Vue.

Pre-requisite: 

  • Latest version of Nodejs 
  • Latest version of npm  
  • Installation of Axios. 

Installation

$ node -v 
V17.8.0 

We see that we are using the node version v17.8.0.  

$ npm init –y 

To initiate a new node.js application.

$  npm i axios 

To install Axios

Syntax 

There are different ways by which we can create requests in Axios. Some of them are as given below. 

axios(config)  
axios(URL [, config]) 

These are other ways of generating requests in Axios. 

axios.request(config) 
axios.get(URL [, config]) 
axios.delete(URL[, config]) 
axios.head(URL [, config]) 
axios.options(URL [, config]) 
axios.post(URL [, data [, config]]) 
axios.put(URL [, data [, config]]) 
axios.patch(URL [, data [, config]]) 

Create [POST]:  

Step one would be to import libraries like Axios and chai for assertions. 

POST method can be used in order to send ‘post’ requests, which can be later used to trigger events like sending the request body to the concerned URL. In this case, the post request can be performed by calling axios.post(). 

By using this technique, we would be needing two parameters  

  1. URL for the service endpoint 
  1. Object which has the properties needed to be sent. 

Since passing data as a parameter to the post method is optional, we can conclude that it is very similar to the get method. 

Read [GET] 

Likewise, we can send a Get request too by using the request function and later calling the get function.  

Once the get request is sent, data received in the response and status code 200 is validated using the expect function from chai library.

Update [PUT] 

For update, we use put request while this request we need to send the full body in the request like “name”, “gender”,” email”, ”status”.  

Here we are updating user/2364 which we created previously. Once the Put request is sent successfully, assertions are made using the expect function from chai library, and values are compared using the expected property from data with actual values coming in response and status code 200. In this example, we would be changing the gender and status of the person, as we can see below. 

Delete [DELETE] 

Deleting can be done easily by just passing the id we want to delete, Here we check by deleting the user via a delete request. We put assertions to check the status code as the data would be deleted once this request is sent. For a successful request, we expect status code 204.  

Pros 

  • Axios can be easily installed therefore providing an easy-to-use API in a compact package for most of your HTTP communication needs. 
  • Due to the large volume of users, it provides high community support. 
  • Axios is lightweight and based on Promise API. 
  • It is isomorphic which means it can run on both, the browser and NodeJS with the same codebase. 
  • Error handling is quite easy which means that if there is a bad response like 404, the promise will get rejected and thus return an error. 

Cons 

  • It isn’t native javascript, In order to use Axios library, we have to install it and later import it to our project using npm/yarn/yarn. 
  • In the case of small Projects, with just a few simple API calls Axios is not a feasible/ideal solution, instead fetch can be used.