API Test Automation Using Javascript – Devstringx

Back to Blog
Feature image for API Automation Using JS

API Test Automation Using Javascript – Devstringx

API Testing

API testing is basically testing the application programming interfaces (APIs), this is the process of testing the product in an early phase so that there can be low chances of breaking the application, it mainly focuses on testing the business logic or of the application without interacting with the user interface of the application.


Now, We will have to first install Node.js in our system, then we will install all the dependencies below. We can write API Automation scripts using JavaScript.

  • SuperTest: SuperTest builds on top of SuperAgent, it is an HTTP request library. SuperTest adds an abstraction on top of the SuperAgent to easily test the API requests.
  • Mocha: Mocha is a javascript framework for testing purposes that runs on Node.js.
  • Chai: Chai is a library used for assertion in API testing, for browser and node.js that could be paired with any JavaScript framework.

Note:  we are using Fake Api to explain to you, you can get these APIs from the websites

For Setting up the tests

Create a new test.js file in your root folder to write your API tests. For now, we will be using the following web URL to test the APIs.


Payroll Raw provides fake REST APIs for testing purposes
Now, let’s start with importing SuperTest in our test file and passing in the Payroll Raw URL for testing the application –

Setup Your Project

Before installing dependencies, initialize a new NPM project –

mkdir api-test-js && cd api-test-js

npm init -y

Install Given Dependencies:

npm install supertest mocha chai –save-dev

Writing API Tests:

GET Method

We will start with a writing test for getting requests. GET is used for getting the data from the server

Make a block to group the tests and name it ‘Users API’ as we will be working with the User’s API

Within the block, we will create an ‘it’ block to give a name to the test. Inside the ’it’ block we will start to write our Automation script

This is what a basic test will look like–

describe('students API', () => {

it(''GET /students', () => {

// Make a GET request to the users route

return request.get('/users').expect(200);


Chai Assertion

Now we can verify 200 OK responses, response time, response size, and much more verification by using Chai assertions. Let’s take a look below:

const assert = require('chai').assert;

First, we will import the Chai library. Here, We are using the assert interface from the Chai library.

it('GET /users', () =>{
  // Make a GET request
  return request
  .then((response) => {
 // Below data is verifying the response should be be empty   assert.isNotEmpty(response.body);

Post Method

Using the POST Method, we can send data to the server to create a new resource. To create a new resource, we usually use the POST method which sends payload data to the server.

it('POST /registration', () => {

   constant data = {

       name:Diksha Gupta,

       email:[email protected]',


   return request

    .post ('/registration)

    .send(data) // send payload data

    .then((response) => {

      assert.hasAnyKeys(response.body, 'id');

      assert.equal(response.body.name, data.name);

      assert.equal(response.body.email, data.email);




Related Blog:-

Share this post

Back to Blog