In the recent years, we have seen a huge surge in the development and use of Application Programming Interface (API). Today, Web applications are fueled by REST-based Web Services. APIs compress critical business logic with high SLAs. Hence it is becoming extremely crucial to test APIs as part of the continuous integration (CI) or continuous delivery (CD) process to identify bugs, reduce errors, and improve predictability.
What is an Application Programming Interface (API)?
An interface with a set of protocols, routines, and tools that helps in building software application and makes available to programmers to interact with other software is known as an Application Programming Interface (API), and it differs from other interfaces like User interface as below
User interface communicates with a collection of windows, dialog boxes, buttons and menus whereas API consists of a set of direct calls or sofware links to lower-level functions and operations.
Today, we have been watching an exponential increase in the development and use of APIs where the entire application is exposed via REST APIs which encapsulate the critical business logic of the software architecture. Hence it is vital to test APIs to determine whether it returns the correct response (in the expected format) for the given input request and also to identify the response time in an acceptable amount of time.
How to Install Postman on any machine
Postman can be installed either as a Chrome extension or Native application
Installing as a Native application:
Go to apps page->Click download for Mac/Windows/Linux depending on platform->Run the installer
Installing as a Chrome extension:
Go to chrome webstore->Search for Postman->Click on Add to chrome->Launch the postman
Postman comes with a command line (CLI) collection runner tool called Newman. This collection runner engine sends API requests, receives the response and then runs your tests against the response. Using the Newman, the response can be achieved instantly, and collections can be exported/imported as JSON files.
Postman collections is a group of saved requests that can be organized into folders. It is mostly a collection of related tests, which can be sequence based or scenario-based tests.
There’s a Collections tab on the top left of Postman, with an example Service swagger collection (or) to add a new collection. Click on the (+) icon below to collection tab.
API requests can be selected from the saved collections and assertions can be inserted in the Tests using the Java script from the code snippets present on the right pane. Postman stores the response in a global object called response Body, and we can use this to access response and assert values in tests as required.
tests [“Response time:” +responseTime] = responseTime;
tests [“Status of Response”] = responseCode.code === 200;
var jsonData = JSON.parse(response Body);
tests[“Verify IsAutoscheduling :”+ jsonData.data.isAutoSchedule] = jsonData.data.isAutoSchedule;
Newman Command Line (CLI) tool:
Once collections are saved and written tests for them, it may be monotonous to go through by each line and click send to see if a given collection test passed or not. This is where Newman comes in. Newman is a command-line collection runner for Postman.
The next step is to export your environment variables and collection and use Newman to run tests from your terminal.
Installation of Newman:
- Download and install NodeJS using the link –https://nodejs.org/download/ and click on the 32-bit or 64-bit Windows installer package, depending on the machine configuration.
- Add the Node executable to your system path. Go to the Control Panel > System and Security > System > Advanced System Settings > Environment variables. Append this to the end of the PATH variable: ;C:\Program Files\Nodejs
- Open a command prompt, and type “node”. The node environment should start.
- Type “npm install -g newman” on command prompt. It should take a few minutes to install.
Below is the process followed to run the API Test scripts:
- Import the Test scripts(collections) from the Postman by clicking on the Import button on Top
2. Update the input requests in API test scripts if required
3. Saved to the same collection and ensure Get method should follow Put method
4. Export the Test scripts by clicking on Export to the physical location and save
5. Once you are in the directory, run newman run <collection_name. Json>, replacing the collection_name with the name used to save the collection.
Example: newman run CalendarWorkHours.postman_collection.json
Batch file can be created by saving all the json collections in the file and saving with .bat extension and run them to execute all the collections
start newman run CalendarWorkHours.postman_collection.json
start newman run CalendarDetails_8088.postman_collection.json
start newman run CalendarWeekDaySlots_8088.postman_collection.json
start newman run CalendarSlotsByDate_8088.postman_collection.json
start newman run GetOpenSlots.postman_collection.json
Newman and Postman comes handy for variety of test cases including suites, create usage scenarios, packs for API test cases and most importantly they can be integrated with CI/CD tools such as Travis, Jenkins and others.