api-automation-framework

api-automation-framework

Introduction

This API testing framework, developed as a mini-project for practicing API testing in Java, provides a comprehensive suite of features to automate API testing, improve test coverage, and ensure the reliability and quality of APIs.

API testing is an integral part of software development and testing, ensuring that APIs function as intended and meet business requirements. This framework encompasses a robust set of features to streamline the API testing process, encompassing core functionalities, application-specific modules, and test execution capabilities.

This framework has 3 layers

  • Core Layer

  • App Layer

  • Test Layer

Jenkins integration empowers the framework to seamlessly integrate with the popular continuous integration (CI) server.


Architecture


Core Layer

The core layer of the framework provides the essential functionality for automating APIs. It consists of the following modules:

ModuleDescription
RestClientThis module provides a high-level abstraction for making HTTP requests to APIs. It handles all the low-level details of HTTP communication, such as sending and receiving requests, parsing responses, and handling errors.
ReportThis module provides a mechanism for generating reports on the results of API tests. The reports can be generated in a variety of formats, such as HTML, TEXT.
AssertsThis module provides a collection of assertion methods for verifying the results of API tests. The assertion methods can be used to check the status codes of responses, the values of response headers, and the contents of response bodies.
UtilsThis module provides a collection of utility methods that are commonly used when automating APIs. The utility methods can be used to perform tasks such as generating random email ids, handling config files.
ExceptionsThis module provides a collection of custom exceptions that are used to handle errors that occur when automating APIs. The custom exceptions provide more specific information about the errors than the standard Java exceptions, which makes them easier to debug.

App Layer

The application layer of the framework provides a layer of abstraction between the core layer and the test layer. It consists of the following modules:

ModuleDescription
POJOThis module contains the Plain Old Java Objects (POJO) that represent the data models of the application. The POJO classes are designed to be simple and easy to use, and they can be used to serialize and deserialize data to and from JSON format.
Business FlowsThis module contains the business logic of the application. The business flow classes are responsible for orchestrating the execution of API tests and common flow related to application which are used across different test as part of re-requisite like creating user.

Test Layer

The test layer of the framework is responsible for executing API tests. It consists of the following components:

ModuleDescription
Test CasesTest cases are individual tests that verify a specific piece of functionality.
Test RunnersTest runners are used to execute test suites and test cases using testng.xml. CI test runners can be used to run tests on a CI server, such as Jenkins

Sample code to write tests

@Test(groups = {"smoke","regression"} )
    public void listAllUsersTest(){
        Response response = request.createRequest().get();

        response.then().spec(GoRestResponseSpec.getResponseSpec());

        //Validate headers
        Asserts.assertEquals(response.getHeader("x-pagination-page"),"1","Validate Response Header - x-pagination-page");
        Asserts.assertTrue(response.headers().hasHeaderWithName("x-pagination-page"),"Validate header x-pagination-page exist");
        Asserts.assertTrue(response.headers().hasHeaderWithName("x-pagination-limit"),"Validate header x-pagination-limit exist");
        Asserts.assertTrue(response.headers().hasHeaderWithName("x-pagination-total"),"Validate header x-pagination-total exist");
        Asserts.assertTrue(response.headers().hasHeaderWithName("x-pagination-pages"),"Validate header x-pagination-total exist");

        ResponseBodyParser responseBodyParser = new ResponseBodyParser(response);

        //Validate number of users on a result page
        int paginationLimit = Integer.parseInt(response.getHeader("x-pagination-limit"));
        int paginationTotal = Integer.parseInt(response.getHeader("x-pagination-total"));
        //Get the count of users in response
        int userCount = responseBodyParser.getList("$").size();

        if(paginationTotal > 10){
            Asserts.assertEquals(userCount,paginationLimit,"Validate userCount is same as paginationLimit when total user count is greater than 10");
        }
        else{
            Asserts.assertEquals(userCount,paginationTotal,"Validate userCount is same as paginationTotal when total user count is less than 10");
        }

        //Validate user has fields
        responseBodyParser.setRootPath("[0].");
        Asserts.assertTrue(responseBodyParser.get("id").toString().length() > 0,"Validate response body has id field");
        Asserts.assertTrue(responseBodyParser.get("name").toString().length() > 0,"Validate response body has name field");
        Asserts.assertTrue(responseBodyParser.get("email").toString().length() > 0,"Validate response body has email field");
        Asserts.assertTrue(responseBodyParser.get("gender").toString().length() > 0,"Validate response body has gender field");
        Asserts.assertTrue(responseBodyParser.get("status").toString().length() > 0,"Validate response body has status");
    }

GitHub Repo - https://github.com/sksingh329/api-automation-framework

Did you find this article valuable?

Support subodh's blog by becoming a sponsor. Any amount is appreciated!