GOQAGO

API Automation Using REST Assured

Case Study - API automation using REST Assured

Posted By - Avinash Shitole Date: 05/08/2019

Project Challenge:

One of the recent projects had only Selenium automation script in place to test the UI (Presentation layer). In addition to the UI, there was a need to automate the API’s testing as well. The challenge was to finalize a tool which could fulfill the below business requirements for API’s automation.


Selection of API automation tool.

Earlier we had good experience in API automation using Postman. But Postman does not support customized reports, Integration with Selenium. Also postman does not allow creating customizable http requests so it’s difficult to automate all combinations of requests. So after a thorough analysis we decided to go with REST-assured (with Maven/ TestNG) for script development. We used Maven as a build tool and Log4j for error logging.

Why Rest Assured?

Rest assured is an open-source java library which can be used to test HTTP Rest services.As we have used Postman in the past, so I would like to provide a small comparison between Postman & REST-assured.


REST assured framework walkthrough
Main Packages:
  • entities package
    • The content type of request & response of the endpoints used in the application was in JSON format. So for creating payload & for parsing the json response we have used serialization & de-serialization technique which is supported by REST assured. Using this technique we create json body/payload from Object & also use to save the json response in Object. The classes used to create objects are defined in entities package. Once the json data was converted to object then it becomes very convenient to get & set values for attribute, validate the data & to create payload for another endpoint. For creating the classes from json we used POJO library.
      Below is the code snippet of one of the classes inside the entities package.

      									
      public class Journal {
          @SerializedName("id")
          @Expose
          private String id;
          @SerializedName("value")
          @Expose
          private String value;
          public String getId() {
              return id;
          }
          public void setId(String id) {
              this.id = id;
          }
          public String getValue() {
              return value;
          }
          public void setValue(String value) {
              this.value = value;
          }
      }
      
      
  • test package
    • This package contains test scenarios & each test scenario contains multiple test cases defined using testNg. Test cases contains single/multiple endpoint calls. To confirm that endpoint is working expected we should add some assertions/verifications. There are 3 types of verifications which are listed below.
      1. Status code verification: For some of the endpoints we may require to just verify the status code returned and based upon the code returned, it is determined if it passed or failed.
      2. Content-Type verification: Sometimes we need to make sure that content type returned by the endpoint is matching the expected result. Depending upon the content then we mark test cases as fail or pass. Below is the code from our suite which call the GET method with path parameter & content type as JSON. Once the API call is successful then assertion is done on response to check if content type is HTML as expected.
        									
        public void Abstract() {
        given(requestSpecification)
        .basePath("/rendered_articles/v2")
        .log().everything()
        .contentType(ContentType.JSON)
        .pathParam("articleId", articleId)
        .get("{articleId}/abstract")
        .then().assertThat().contentType(ContentType.HTML);
        }
        
        
      3. Schema validation: JSON Schema is a contract for your JSON document that defines the expected data types and format of each field in the response. That schema can then be used to validate that an actual JSON response from the API is compliant with the definition. io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath is a rest assured package which is used To validate the Json schema.

        Below is a Json file 'Articlelist.json' with a schema definition.
        
        {
          "$schema": "http://json-schema.org/draft-04/schema#",
          "type": "object",
          "properties": {
            "items": {
                  "type": "object",
                  "properties": {
                    "article": {
                      "type": "object",
                      "properties": {
        .
        .
        .
         "required": [
            "items",
            "nextPageId",
            "totalCount"
          ]
        }
        
            
        

        Below is the code which shows that the file 'Articlelist.json' is parsed by the rest assured package io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath to validate.
        
        public void article() {
        Response response = given(requestSpecification)
        	.param("token", user.getToken())
        	.get("/trending/new");
        ArticlesList alist = response.then().body(matchesJsonSchemaInClasspath("articlelist.json"))
        	.assertThat()
        	.statusCode(HttpStatus.SC_OK)
        	.extract()
        	.body().as(ArticlesList.class);
        	   }    
        
        
Supported Packages:
  • helpers package:
    • This package contains classes/methods that are very often used in the framework. Some of them are like data provider helper which provides data to testNG method, property file reader, CSV file reader etc.
  • utility package:
    • This package contains small utilities which are used in a framework like - GenerateReport (generate the test report and sent it to all team members) etc.
  • customListener package:
    • TestNG provides different type of listeners that can be customized as per our requirements in the framework. In customListener package we have defined some customise functions by overwriting the existing TestNG listeners interfaces. Here is the list of interfaces which we have customized. One of the listeners inside this package is ‘TestListener’.
      1. TestListener: Here methods implemented from ITestListner are used to write the test result(Pass/Fail/Skip) to external file. Later with the help of email report utility it’s used to generate the custom emailable HTML report.
  • TestNG:
    • TestNG is used for test case execution and test report generation.
  • Log4j:
    • Log4j is an open source logging framework. We have used it to capture the automation logs only in case the test cases fails.
Other supported automation tools:
  • Maven:
    • Maven as the auto-build and dependency management tool.
  • Bitbukcet:
    • Source code management tool for version control.
  • Jenkins:
    • Our Jenkins automation job used to pull the latest code from the Bitbucket repository and executes it in headless mode. We also configured the Jenkins job so that any team member can execute the script module wise as well. Obviously we made some changes in our script as well to support it.
Conclusion:
  1. More flexibility with writing tests, as it is based on Java. We were able to write more complex scenarios which helped us to increase the API test coverage upto 90%.
  2. It’s an open source tool so free to use without any license.
  3. Easy to integrate with JUnit and TestNG frameworks.
  4. Support for customizable HTML email report at the end of the script.
  5. Easy integration with CI tools like Jenkins etc .