API Automation Framework using RestAssured

API Automation Framework using RestAssured

To create an API Automation Framework using RestAssured, you can structure your framework into logical components, including request builders, utilities, configurations, test cases, and reporting mechanisms. Below is a guide to building such a framework in Java:


1. Project Setup

  • Dependencies: Use Maven or Gradle to manage dependencies. Add the following to your pom.xml for Maven:
    <dependencies>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.9</version>
        </dependency>
    </dependencies>
    

2. Framework Structure

a. Directory Structure

src/
├── main/
│   ├── java/
│   │   ├── config/
│   │   ├── utils/
│   │   ├── request/
│   │   └── models/
├── test/
│   ├── java/
│   │   ├── tests/
│   │   ├── base/
│   │   └── data/

b. Components

  1. Configuration (config/)

    • Store environment-specific configurations like base URI, API keys, etc.
    • Example:
      public class Config {
          public static final String BASE_URI = "https://api.example.com";
          public static final int TIMEOUT = 5000;
      }
      
  2. Request Builders (request/)

    • Encapsulate API endpoints and methods.
    • Example:
      import io.restassured.RestAssured;
      import io.restassured.response.Response;
      
      public class ApiRequest {
          public static Response getUserById(String userId) {
              return RestAssured
                      .given()
                      .baseUri(Config.BASE_URI)
                      .when()
                      .get("/users/" + userId);
          }
      }
      
  3. Utility Classes (utils/)

    • Helper methods for logging, JSON serialization/deserialization, etc.
    • Example:
      import com.fasterxml.jackson.databind.ObjectMapper;
      
      public class JsonUtils {
          public static <T> T fromJson(String json, Class<T> clazz) throws Exception {
              ObjectMapper mapper = new ObjectMapper();
              return mapper.readValue(json, clazz);
          }
      
          public static String toJson(Object object) throws Exception {
              ObjectMapper mapper = new ObjectMapper();
              return mapper.writeValueAsString(object);
          }
      }
      
  4. Data Models (models/)

    • Represent API request and response payloads as Java objects.
    • Example:
      public class User {
          private String id;
          private String name;
          private String email;
      
          // Getters and Setters
      }
      
  5. Base Test Class (base/)

    • Setup and teardown logic for tests.
    • Example:
      import org.testng.annotations.BeforeSuite;
      
      public class BaseTest {
          @BeforeSuite
          public void setup() {
              RestAssured.baseURI = Config.BASE_URI;
          }
      }
      
  6. Test Cases (tests/)

    • Write test cases using TestNG.
    • Example:
      import org.testng.Assert;
      import org.testng.annotations.Test;
      
      public class UserTests extends BaseTest {
          @Test
          public void testGetUserById() {
              Response response = ApiRequest.getUserById("123");
              Assert.assertEquals(response.getStatusCode(), 200);
              User user = JsonUtils.fromJson(response.getBody().asString(), User.class);
              Assert.assertNotNull(user);
          }
      }
      

3. Reporting

  • Integrate reporting libraries like ExtentReports or Allure for detailed test results. Add dependencies:
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>5.0.9</version>
    </dependency>
    

4. Running Tests

  • Use TestNG or Maven to execute tests:
    mvn clean test
    

5. Enhancements

  • Environment Management: Use profiles for different environments (e.g., dev, staging).
  • Retry Logic: Add retry mechanisms for flaky tests.
  • Parallel Execution: Configure TestNG for parallel test execution.
  • CI/CD Integration: Integrate with Jenkins or GitHub Actions.

Would you like a complete code example or additional details about a specific section?

Prakash Bojja

I have a personality with all the positives, which makes me a dynamic personality with charm. I am a software professional with capabilities far beyond those of anyone who claims to be excellent.

Post a Comment

Previous Post Next Post

Contact Form