Backend API Automation Framework - Notes By ShariqSP
Designing an API Backend Framework for the Given APIs
To design a backend API testing framework for the listed APIs, we will include all the necessary components mentioned. The framework is modular and follows the best practices to make testing efficient, scalable, and maintainable. Below is an explanation of the components, a high-level diagram, and the implementation structure.
Components in the Framework
- Generic Utility: Contains utility classes for repetitive operations like file handling, JSON parsing, REST client interactions, logging, and data encryption.
- PojoUtility: Contains Plain Old Java Objects (POJOs) to model API request/response objects for various entities like
User
,Event
,Inventory
, etc. - BaseAPIClass: Serves as the base class for API test cases. It initializes common configurations like Base URL, authentication tokens, headers, etc.
- EndPoints: Centralized constants for all API endpoint URLs to make them reusable across the framework.
- TestScripts: Contains test scripts written in TestNG or JUnit for each controller and its respective endpoints.
- Driver (TestNG.xml): Entry point for test execution, defining the test suite, test groups, and parallel execution strategies.
- Env Config Data: Stores environment-specific configurations like URLs, database credentials, and other properties in external files (e.g., YAML or JSON).
- Reports: Generates detailed test execution reports using tools like ExtentReports or Allure.
- Resources: Stores external files like data sets (e.g., CSV/JSON), test data, and schemas for validation.
- POM.xml (Build File): Defines dependencies like TestNG, REST-assured, and JSON libraries for the framework.
Framework Structure
src ├── main │ ├── java │ │ ├── base │ │ │ └── BaseAPIClass.java │ │ ├── endpoints │ │ │ └── EndPoints.java │ │ ├── pojo │ │ │ ├── UserPojo.java │ │ │ ├── EventPojo.java │ │ │ ├── InventoryPojo.java │ │ │ └── CardPojo.java │ │ ├── utils │ │ │ ├── FileUtility.java │ │ │ ├── JSONUtility.java │ │ │ ├── ExcelUtility.java │ │ │ ├── RestClient.java │ │ │ └── LoggerUtility.java │ │ └── resources │ │ └── config.yaml │ └── test │ ├── java │ │ ├── testscripts │ │ │ ├── UserTestScripts.java │ │ │ ├── EventTestScripts.java │ │ │ ├── InventoryTestScripts.java │ │ │ ├── CompanyTestScripts.java │ │ │ └── AdminTestScripts.java │ │ ├── testng.xml │ └── reports │ └── test-report.html pom.xml
Framework Diagram
Here is the high-level architecture of the framework:
Env Config Data
Configurations
BaseAPIClass
Initialize Base URL, headers
EndPoints
Centralized URLs
PojoUtility
Request/Response Objects
Generic Utility
Utility functions
Test Scripts
API Tests
Driver (TestNG)
Test Execution
Reports
Test Reports
Explanation of API Design
BaseAPIClass.java
Initializes the Base URL and common headers for APIs.
public class BaseAPIClass { protected String baseUrl = "http://apitesting.shariqsp.com:8080"; protected RequestSpecification request; @BeforeClass public void setup() { request = RestAssured.given().baseUri(baseUrl); } }
EndPoints.java
Centralized storage of API endpoints.
public class EndPoints { public static final String REGISTER_USER = "/api/users/register"; public static final String UPDATE_USER = "/api/users/update/{id}"; public static final String LOGIN_USER = "/api/users/login"; }
PojoUtility
Create POJOs for request/response.
public class UserPojo { private String username; private String password; // Getters and Setters }
Test Scripts
Create TestNG tests for endpoints.
public class UserTestScripts extends BaseAPIClass { @Test public void testRegisterUser() { UserPojo user = new UserPojo(); user.setUsername("testuser"); user.setPassword("password"); request.body(user) .when().post(EndPoints.REGISTER_USER) .then().statusCode(201); } }
Driver (TestNG.xml)
Defines test suites and groups.
<suite name="APITests">
<test name="UserAPITests">
<classes>
<class name="testscripts.UserTestScripts"/>
</classes>
</test>
</suite>
Final Note
This framework provides a structured and reusable approach to API testing for all the controllers and endpoints. Let me know if you'd like to dive deeper into any specific part!