Znode APIs Postman Collections

TABLE OF CONTENTS

Introduction

Znode has its API postman collection (Webstore specific APIs). It not only allows user to explore the API request/response model, but it also has “Tests” written to validate the API response with multiple test cases for a given API. It can be set up to automate the “Test” execution using the “Newman” tool and it will generate a visual report of the success/failure of the execution. This article gives an overview of the same.

Framework Building-blocks

Znode API Automation Framework bis uilt on the following tools & technologies:

  1. Postman tool

  2. Javascript as a test script language

  3. Collection & Environment variables json file editor (https://codebeautify.org/jsonviewer)

  4. Online Json schema generator (https://jsonschema.net/home https://jsonschema.net/home)

Postman

Postman is being used because of the following features:

  1. Accessibility:
    To use Postman, one would just need to log in to their own accounts making it easy to access files anytime, anywhere as long as a Postman application is installed on the computer.

  2. Use of Collections:
    Postman lets users create collections for their API calls. Each collection can create subfolders and multiple requests. This helps in organizing your test suites.

  3. Collaboration:
    Collections and environments can be imported or exported making it easy to share files. A direct link can also be used to share collections.

  4. Creating Environments:
    Having multiple environments aids in less repetition of tests as one can use the same collection but for a different environment. This is where parameterization will take place which we will discuss in further lessons.

  5. Creation of Tests:
    Test checkpoints such as verifying for successful HTTP response status can be added to each API call which helps ensure test coverage.

  6. Automation Testing:
    Through the use of the Collection Runner or Newman, tests can be run in multiple iterations saving time for repetitive tests.

  7. Debugging:
    Postman console helps to check what data has been retrieved making it easy to debug tests.

  8. Continuous Integration:
    With its ability to support continuous integration, development practices are maintained.

Javascript as a test script language

Postman Tests are JavaScript codes added to requests that help you to verify results such as successful or failed status, comparison of expected results, etc. It usually starts with pm. test. It can be compared to asserts, and verify commands available in other tools.

Collection & Environment variables json file editor

This is an online editor tool that helps to manipulate/merge collection & environment JSON files.

Online JSON schema generator

This is an online schema generator tool that helps to generate schema which needs to get validated against the response body. Also, it helps in validating mandatory, and optional parameters along with their data types.

Postman Overview

Postman is an API (Application Programming Interface) development tool that helps to build, test, and modify APIs.

Please refer to the below URL for a Postman overview & API Automation use:

https://www.guru99.com/postman-tutorial.html


Naming Convention & Standard

We are following the below-naming convention & coding standard for API Automation.

  1. Collection Name:
    Should start with an upper camel case without space.
    Eg. MyAPIAutomation

  2. Folder & Subfolder Name:
    Should start with a lower camel case without space.
    Eg. myAPIAutomation

  3. API Name:
    Should start with an upper camel case with space in between each word.
    Eg. Get My Account

  4. Variable Name:
    Should start with a lower camel case without space.
    Eg. portalId, productName

  5. Schema Variable Name:
    The schema variable should be in the below format:
    schema_Module_Functionality
    Eg. schema_PromotionType_CreatePromotion

Also while writing java scripts, you will see necessary comments as per function/method behavior & success/failure messages as below:

Collection, Environment & Global Variables

Variable is a very powerful feature of Postman. Not only does it allow you to save and manage some repeating information (like URLs, authentication tokens or keys, and so on) but by using scripts, you can pass data from one request to the next thus creating some very advanced workflows.

Global Variables

Global variables are general-purpose variables, ideal for quick results

and prototyping. You can make use of Global variables when passing data to other requests or sharing some data between multiple collections, passing data from the pre-request script to the request/tests.

As of now, we haven’t used Global variables in the API Automation framework.


Environment Variables

Environment variables will be useful if you want to use the same collection but against different environments. Variable values which need to get updated as per different environments (QA, Automation, etc.) we have included in environment variables.

Collection Variables

Collection variables try to provide a way of defining variables within a collection. This makes the collection independent of any environment.

Variables that get updated by test scripts are stored in Collection variables. 


Importing Collections in Postman

Step 1: Using TortoiseGit, the user can clone Collection & Environment JSON files from the required release branch and import them into the Postman workspace directly.

Step 2: The user needs to select the environment variable JSON file from the environment to drop down.

Step 3: The user needs to replace environment-specific variable values as per the execution requirements in the environment variable list.

Below are the variables which need to be replaced:

Step 4: The user need to add below Payment related variable values in the environment variable list.

Validations considered in API Automation

In API Automation, we have considered only happy path validation like API response code & positive response schema with response parameter data type, the reason being individual API test code length, readability & maintenance.


To validate API functionality with response parameters, the test script is written against individual API. We have created a function library environment variable functionLib, wherein all reusable test functions are created with required function parameters & return values.


functionLib reusable functions

validateResponseStatus()

This function is used to validate the response status code. Where the input parameter is the expected response status code.


validateResponseSchema()

This function is used to validate the response schema. Where the input parameters are expected response status code & response schema. In this function, using the if condition first we are checking whether the response status code is as expected & response body is not null. Once these two conditions are satisfied, we are validating the run time response body against the response schema.


setValue()

This function is used to set the collection variable value. Where the input parameters are collection variable name & parameter value.


getToken()

This function is used to get the latest access token. In this function, the first token expiration time gets verified based on the current token generation date & last token generation date. If the last generated token is found to expired then the ‘token/genaratetoken’ GET call will request for a new token with the application ‘authorization Base64 Code’ request parameter. The latest token value will get assigned to the ‘token’ environment variable & ‘token_Timestamp’ environment variable value gets updated as per the current date. Also, ‘expireTime’ environment variable value gets updated which will help to verify token expiration next time.


removeNull()

This function is used to replace all null response parameter values with blank values. Where the input parameter is the runtime response object.


getValues()

This function is used to get an array of all values for the required response parameter key. Where the input parameters are runtime response object & response parameter key against which the user needs to retrieve all values.


verifyCustomParameters()

This function is used to verify custom response parameter values are null. Where the input parameter is the runtime response object.


verifyKeyValue()

This function is used to verify an expected specific key-value response parameter exists in the runtime response body. Where the input parameters are the runtime response object, the required response parameter key & its expected value, and the valid message that needs to get logged in the test result window.


verifyKeyValueNotPresent()

This function is used to verify specific key-value response parameter does not exist in the runtime response body. Where the input parameters are the runtime response object, the required response parameter key & its expected value, and the valid message that needs to get logged in the test result window.


setTimeout()

This function is used to set static wait in seconds in case API is taking long to respond. Where an input parameter is the number of seconds.


Collection execution prerequisites

Database

APIs are automated with pre-configured test data. To execute API Collections in any environment other than mentioned in the environment variable ‘URL’, a user would require to configure the prerequisite database. The user needs to deploy a database backup copy (shared on Google drive) on the execution instance/environment.

Increase API Token expiration time

Under Znode source code, inside Znode.Engine.API - web.config file, set value for EnableTokenBasedAuthorization key as false or user can increase TokenExpirationTime from 10 to 60 minutes.


Collections Execution & Report Analysis

Collection execution using Postman

After importing all Collections & environment variables json files, user can execute required Collection using ‘Run’ button present adjacent to each Collection.

After clicking on ‘Run’ button, a separate ‘Collection Runner’ window will get launched as below. To start Collection execution, user need to click on ‘Run’ button highlighted in the screenshot below.

After Collection execution, based on number of validations written in individual API test script, user will get to see execution report with pass & fail validations as below:



Collection execution using Newman

(Steps for Newman Installation)


Step 1: Install NodeJS – https://nodejs.org/en/download/. Click on the 32-bit or 64-bit Windows Installer package, depending on your machine configuration


Step 2: 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


Step 3: Open the command prompt in Run As Administrator mode, and type “node”. The node environment should start. Press Ctrl-C to exit


Step 4: Type “npm install -g Newman”. It should take a few minutes to install


Step 5: Install Html Report, execute "npm install -g newman-reporter-html" command in cmd


Step 6: Install HtmlXtra, execute "npm install -g newman newman-reporter-htmlextra" command in cmd


Step 7: Execute the below command to increase heap memory:

set NODE_OPTIONS=--max_old_space_size=8192


Step 8: Once Newman setup completes, you can execute each collection individually using the below command:


newman run QuickOrder.postman_collection.json --timeout-script 300000 --environment API_Sanity_Environment_Variables.postman_environment.json -r cli,htmlextra --reporter-htmlextra-export Reports/QuickOrder.html


After collection execution, html report gets generated in specified folder (in same directory)




Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.