Skip to main content

Step by step guide to set up API Automation framework in JavaScript for beginners.

API Automation made simpler and Easier !


Wondering if API Automation has to be only written using JAVA, .NET ?
Thinking if reusable Request objects can only be maintained effectively using .NET programming ?
Imagine if you can Automate huge test data for API testing with light weight JavaScript framework and spin up the test environment with just a single command ?

Yes, You can !

Below Ill take you through step by step procedure to set up a simple JavaScript API Automation framework written using mocha.js, chai.js and few other npm libraries which allows you to test tonnes of test scenarios with very simple and manageable reusable objects.

Lets get this started...

You need the below key libraries from node package manager, so just start your package.json with these simple libraries.

  • 'mocha' for test framework
  • 'chai' for assertions
  • 'supertest' for sending api requests
  • 'chai-factories' for building reusable request objects
  • 'csv2json' or any other equivalent to covert bulk data from csv format to framework understandable JSON format.
  • 'mochawesome' or any other equivalent to generate html reports

There are many ways you can structure your framework but I advise a very basic structure like below to start with.



Explaining the folder structure
  • 'helpers' : Any reusable objects like login details, test data json etc can be located here.
  • 'requestObjects': Central location to store all reusable request objects. This also helps to define and maintain request contracts. If there is any change in request contract you can maintain it from here.
  • 'test': Single location for all your test files
  • 'utils': Any utilities like multiple test data csv's etc can be stored here 
  • 'test.js': Logic implementation for integrating with CI/CD pipeline

Make sure to load your package.json file with dependencies and also very importantly 'scripts' , 'scripts' allows you define how you want to run the tests. Sample given below
Am explaining a very simple test scenario here to fire a login request and then post something which needs to be done with 1000 different test data.
So I create a login request and other post request object in 'requestObjects' folder.
I create a test data csv in 'utils' folder
I save my test data json in the runtime in 'helpers' folder
I write the test in 'test' folder

Login request builder looks like

Within chai-factories we can overwrite the factory and also extend the factory, which makes it very easy to define request contract and update it dynamically in the run time.
Chai factories usage:
Saving test data csv into 'helper' run time looks like below in the _test.js file (Inside 'test' folder)
Typical test file can look like
Now that test framework is set up. All you will do is open command prompt, navigate to your repository. Run 'npm install' and 'npm start'



Hope this helps. Thanks !

Comments