TABLE OF CONTENTS
- Framework Building-blocks
- Postman Overview
- Naming Convention & Standard
- Collection, Environment & Global Variables
- Importing Collections in Postman
- Validations considered in API Automation
- Collection execution prerequisites
- Collections Execution & Report Analysis
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.
Znode API Automation Framework bis uilt on the following tools & technologies:
- Postman tool
- Collection & Environment variables json file editor (https://codebeautify.org/jsonviewer)
- Online Json schema generator (https://jsonschema.net/home https://jsonschema.net/home)
Postman is being used because of the following features:
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.
- 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.
Collections and environments can be imported or exported making it easy to share files. A direct link can also be used to share collections.
- 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.
- 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.
- Automation Testing:
Through the use of the Collection Runner or Newman, tests can be run in multiple iterations saving time for repetitive tests.
Postman console helps to check what data has been retrieved making it easy to debug tests.
- Continuous Integration:
With its ability to support continuous integration, development practices are maintained.
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 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:
Naming Convention & Standard
We are following the below-naming convention & coding standard for API Automation.
- Collection Name:
Should start with an upper camel case without space.
- Folder & Subfolder Name:
Should start with a lower camel case without space.
- API Name:
Should start with an upper camel case with space in between each word.
Eg. Get My Account
- Variable Name:
Should start with a lower camel case without space.
Eg. portalId, productName
- Schema Variable Name:
The schema variable should be in the below format:
Also while writing java scripts, you will see necessary comments as per function/method behavior & success/failure messages as below:
Collection, Environment & Global Variables
The 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 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 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 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 needs to add the 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
This function is used to validate the response status code. Where the input parameter is the expected response status code.
This function is used to validate the response schema. Where the input parameters are the 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 & the response body is not null. Once these two conditions are satisfied, we are validating the run time response body against the response schema.
This function is used to set the collection variable value. Where the input parameters are collection variable name & parameter value.
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, the ‘expire time’ environment variable value gets updated which will help to verify token expiration next time.
This function is used to replace all null response parameter values with blank values. Where the input parameter is the runtime response object.
This function is used to get an array of all values for the required response parameter key. Where the input parameters are the runtime response object & response parameter key against which the user needs to retrieve all values.
This function is used to verify custom response parameter values are null. Where the input parameter is the runtime response object.
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.
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.
This function is used to set static wait in seconds in case the API is taking a long to respond. Where an input parameter is the number of seconds.
Collection execution prerequisites
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 the 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, the user can execute the required Collection using the ‘Run’ button present adjacent to each Collection.
After clicking on the ‘Run’ button, a separate ‘Collection Runner’ window will get launched as below. To start Collection execution, the user needs to click on the ‘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's 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:
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, the HTML report gets generated in specified folder (in same directory)