Why API Automation?
Below are few points which invokes us to start API Automation:-
- Testing of manual tests takes far longer than just executing them via automation.
- The UI is one of the least stable interfaces of rapid changing Web/App Ecosystem, so we can start automating API’s to test the backend functionality.
- Maintenance of the UI tests takes a significant amount of time.
- Execution of UI test is slow, and sometimes cumbersome.
- Tests become flaky.
- Tests break for the wrong reasons at UI end.
Prerequisites to start API Automation
Setup below mentioned tools on your development machine:-
- Your favorite IDE (Eclipse Recommended)
- Java, Maven should be installed
- Java, Maven, TestNG knowledge
- HTTP Library: A Java based library for HTTP communications
- GSON to handle or manipulate JSON response.
What is JSON?
- JSON stands for Java Script Object Notation.
- It provides readable format for structuring data into a syntax for storing and exchanging.
- There are two primary parts of JSON format i.e.: keys and values. Key is always a string while value can be String, Array, Boolean Expression or an object.
“message”: “Login successfully”,
“timeZoneName”: “GMT +05:30 – New Delhi”,
Difference Between JSON and XML :-
|Java Script Object Notation||Extensible Markup Language|
|Developed by Douglas Crockford||Developed by W3C|
|Faster to write||Difficult to write|
|Light Weighted||Not so lighter as compared to JSON as it takes more space to represent same data.|
|Doesn’t supports comments||Supports Comments|
|Supports data types including Integer , string and Arrays.||No direct support of Array|
|Data oriented and can be mapped easily||Document oriented & need more efforts for mapping|
API Automation Workflow :-
Fig. :- API Automation Workflow
Sample API Test Case:-
API Method POST :-
API Method GET :-
What are Models ?
- When an application will have to access several back ends data from multiple classes then a “Domain Model” typically termed as ”Data Model”, which represents the basic structure for an enterprise and maintains relationships to different back end systems.
- When designing Web/App API’s, you must design the data around “How the user see’s the data” and it is referred as Model.
- Models are the endpoint response into an object class using any object-oriented programming language of your choice.
Why we use Models ?
- It helps in mapping JSON response.
- It helps in faster development of software as well as API Test Cases.
- It is quite easy to maintain. Once it is created it will be used throughout the next update or changes in the module. Let us suppose, a module develops an error due to some changes in the requirements, then a programmer can fix that particular module.
- It enables reuse of objects, designs, and functions.
- It reduces the development risks especially in the integration of complex systems.
- It implies mapping the response to a Java class called POJO stands for Plain Old Java Object, with the class members corresponding to the JSON fields and using this object in assertion, assertEquals(expectedResponseObject , actualObject), given that assertion is customized to compare objects.
Online POJO Converter :-
|JSON to POJO||http://boldijarpaul.github.io/jsontopojo/|
|Convert XML or JSON to Java Pojo Classes||http://pojo.sodhanalibrary.com/|
|Json to Java||http://jsontojava.sinaapp.com/|
Steps to Validate the JSON Request / Response output :-
- Copy the Request / Response.
- Open http://jsonlint.com/
- Paste the response into the provided box.
- Click on validate.
- It will return the tree structure of JSON request or response
Steps to Convert the response into Model Classes :-
Steps to Convert the response into Model Classes :-
- Copy the Response received from the request.
- Open http://www.jsonschema2pojo.org/
- Validate and Paste the response.
- Select source Type as JSON.
- Select Annotation style as GSON.
- Click on Preview.
What is GSON ?
- GSON is an open source Java library developed by Google in 2008.
- It is an API for converting a Java object to and from JSON representation.
- It can also be used to convert a JSON string to an equivalent Java object.
- GSON can work with arbitrary Java objects including pre existing objects that you do not have source-code of.
Why we use GSON ?
- GSON is used to perform automatic JSON parsing.
- It converts any Java object i.e. new object or any existing legacy object to JSON and from JSON.
- It provides finest support for generic objects.
- It is simple and convenient methods for conversions.
- There is no need of any annotation for fields for conversions.
- All the fields by default are included in conversions even private fields.
GSON Maven Dependency URL :-
To convert an object to or from JSON, we have to use the GSON class and its 2 methods :-
- toJson(): It converts an object provided to JSON string, takes object to be converted as argument and returns the json representation string
- fromJson(): It converts the JSON string to object, takes first parameter as JSON string as object and class literal of destination object and returns the destination object
GSON Parser Code
How to Validate the API Responses ?
Compare the expected values with the actual response output.