Codementor Events

Rest API Automation Framework

Published Jan 30, 2023

This post explain about the way how to design a Rest API Automation framework from scratch.

As a pre-requisite you should familiar with following listed items.

API To Automate

API Request:
curl --location --request POST 'https://petstore.swagger.io/v2/pet' \
--header 'Content-Type: application/json' \
--data-raw '{
    "id": 1,
    "category": {
        "id": 1,
        "name": "dog"
    },
    "name": "scoobydoo",
    "photoUrls": [
        "https://www.cdc.gov/healthypets/images/pets/cute-dog-headshot.jpg?_\u003d42445"
    ],
    "tags": [
        {
            "id": 1,
            "name": "scooby"
        }
    ],
    "status": "available"
}'
API Response:
{
    "id": 1,
    "category": {
        "id": 1,
        "name": "dog"
    },
    "name": "scoobydoo",
    "photoUrls": [
        "https://www.cdc.gov/healthypets/images/pets/cute-dog-headshot.jpg?_=42445"
    ],
    "tags": [
        {
            "id": 1,
            "name": "scooby"
        }
    ],
    "status": "available"
}
The Above request can be imported in POSTMAN. Copy the curl and import the data and fire the request. For the request sent a response will be received.

Necessary Item Required to Automate Rest API

To make a basic request the following items are required

  • HTTP Verb – (Post,Get etc…)
  • Base URL – (Eg:- https://api.x.com)
  • Resource Path – (Eg:- /v1/users)
  • Headers -> Required Header details and should add What content-type that we transit. Mostly it should be Application/json.
  • Body -> Json data

Once the Request is Fired, The server process the request based on business logic and return response. The basic response consists of following items.

  • Status Code – (200 – success etc .. )
  • Response Body -> Json data
  • Response Header

Automation Architecture

Components in Architecture:

Controllers
  • Controllers consists of common code to define control request flow.
  • Controller consists of following items
    • Authorization – Credential for different services can be added here
    • Constants – All static value required during flow will be added here
    • Test Group – Common place to define test groups
    • Route – Resource path
Dtos
  • Data transfer object consists of API contract details of request and response as class object.
Enums
  • Enums that required while test can be added
Exception
  • Exception that need to be handled inside test flow. This help on testing negative scenarios.
Impl
  • Implementation consists of API contract for each route related to a service.
Listeners
  • Listeners are type that helps to added extend functionality that help to improve the test automation process such as reporting, Retry mechanisms etc…
Services
  • Services consists of the Rest assured library implementation and that uses rest assured library to fire a rest api request and receive response.
Tests
  • Test consists of test data and test steps to verify and validate the api’s
Utils
  • Utils is a common package for adding utilities that required across test automation.
Code Walkthrough:
  • Request/Response DTO is added under the package of the service.
  • Common entity from request and response can be added to Base DTO.

Automation Framework Code:

https://github.com/premsvmm/API-TAF

Execution Workflow

Discover and read more posts from Prem Kumar
get started