June 5, 2016

API Automation

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.

{
“status”: 1,
“message”: “Login successfully”,
“data”: {
                                “access_token”: “rcd676up552kit3oqfhkajjf18akd93a”,
                                “email”: “kkashyap1707@gmail.com“,
                                “userId”: “1”,
                                “timeZoneName”: “GMT +05:30 – New Delhi”,
                                “timeZoneId”: 8,
                                “gameName”: “ikeshav”,
                },
“code”: 10001
}

                                               JSON Representation

Difference Between JSON and XML :-

JSON

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

API Workflow

Fig. :- API Automation Workflow

Sample API Test Case:-

https://gist.github.com/kkashyap1707/d2ec20c30a13f883bb58e59551775cdb

API Method POST:-

https://gist.github.com/kkashyap1707/2468ddc36b65b6c187fbfa729e3f4d54

API Method GET:-

https://gist.github.com/kkashyap1707/1dd4c032e226c17fe0d6b3ef10ccb89a

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

Tool Name

Websites

Jsonschema2pojo http://www.jsonschema2pojo.org/
JSON to POJO http://boldijarpaul.github.io/jsontopojo/
Convert XML or JSON to Java Pojo Classes http://pojo.sodhanalibrary.com/
Code Beautify http://codebeautify.org/jsonviewer#
Json to Java http://jsontojava.sinaapp.com/

Steps to Validate the JSON Request / Response output :-

Validate JSON

  • 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 :-

JSON TO POJO

  • 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 :-

GSON Maven Dependency

GSON Parsing

To convert an object to or from JSON, we have to use the GSON class and its 2 methods :-

  1. toJson() : It converts an object provided to JSON string, takes object to be converted as argument and returns the json representation string
  2. 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

https://gist.github.com/kkashyap1707/c7a1f7c7b415236df988f75db6193ba4

How to Validate the API Responses ?

Compare the expected values with the actual response output.

https://gist.github.com/kkashyap1707/b68b18e2f44e1aa24996eb1d26cd8362

 

Click Here for API Automation Framework using REST ASSURED

 

 

 

 

12 thoughts on “API Automation

  • Thanks Keshav for sharing wonderful information my question is how you will determine that a site can be automated with API or not or its by default in-built and exposed with every website without interacting with the Dev team and secondly as per my understanding how you will make sure that application behaviour is same when you are performing the same functionality by GUI and API ….for example It might be that login API is working fine but in GUI there is some connection leakege/discontinuation due to some intermittent issue in this scenario we can’t make sure that application functionality is working fine by just checking the respective API so the point to asking second question is that we can’t 100% rely on API testing.Please clarify

  • Hi Mr. Automator
    First of all thanks for your valuable comment.
    Below are the reply of your questions.
    Q. : How you will determine that a site can be automated with API or not?
    A. : Such information is provided by the business stack-holder or your project manager that whether your backend is using API’s or not.
    API is not exposed for every website.

    Q. : How you will make sure that application behavior is same when you are performing the same functionality by GUI and API ….for example It might be that login API is working fine but in GUI there is some connection leakage/discontinuation due to some intermittent issue?
    A. : It is not mandatory that if your API is working fine then Your GUI is also working properly.Both are independent with their behavior. API testing only check that the functionality of your application is working fine or not. While in GUI testing we test UI as well as the functionality of the application.

  • Hey very useful for the beginners. Thanks for the great info and keep update. I have a query in your scripts, where are the Login_InputGson,Login_Response and Login classes. Could you plz update in site if possible.

Leave a Reply

Your email address will not be published. Required fields are marked *