The Keyword-Driven Test Automation Framework With JSON Format for REST API Testing

First, I’d like to get back a little bit of this framework’s architecture overview.

The Keyword-Driven Test Automation Framework With JSON Format for REST API Testing
Test Execution Progress

As the above image and the previous story mentioned, the test suites/cases/steps are formed in a JSON file, and the framework will load and map these JSON files to the list of test suites/cases objects. Then, action steps are executed based on the class and method (keyword) that are specified in each test step (use the reflection technique to invoke the keyword — method). Our responsibility is to write the autotest script inside each keyword.

I coded the core class and keywords for REST API Testing named RestAPIAction.

What you need to use this framework is to define test suites/cases and test steps in the JSON format file.

The source code is located on GitHub here and here. Download or clone it to research more.

The automation libraries used in this framework are as follows:

Next, I’d like to show you the test parameters of the RestAPIAction keyword that will be defined in each REST API test step (please refer to the previous article linked in the introduction to get more detail on the test suite/test case format).

1. GET Method

{
  "name": "Search repository",
  "class": "RestAPIAction",
  "method": "GET",
  "parameters": {
    "request": {
      "url": "https://api.github.com/search/repositories",
      "headers": {
        "Authorization": "@var->authorization@" },
      "queryParams": {
        "q": "automationtestingframework",
        "sort": "stars",
        "order": "desc" }
    },
    "response": {
      "statusCode": 200,
      "body": {
        "total_count": {
          "greaterThan": 1
        },
        "items.any{it.full_name = 'automationtester304/automationtestingframework'}": {
          "is": true }
      }
    }
  }
}


2. POST Method

{
  "name": "Send POST request to create Hello-World repository",
  "class": "RestAPIAction",
  "method": "POST",
  "parameters": {
     "request": {
       "url": "https://api.github.com/user/repos",
       "headers": {
         "Authorization": "@var->authorization@" },
       "body": {
         "name": "Hello-World",
         "description": "This is your first repository",
         "homepage": "https://github.com",
         "private": false,
         "has_issues": true,
         "has_projects": true,
         "has_wiki": true }
     },
     "response": {
       "statusCode": 201,
       "body": {
         "full_name": {
           "is": "automationtester304/Hello-World" }
       }
     }
   }
}


3. PUT/PATCH and DELETE methods

Please refer to CRUDRepository.json.

As you can see, there are two “Request and Response” sections in each REST Action method.

Request Section

This is where you define the parameters’ values for sending a REST Request.

Response Section

This is where you define the expected value in the response.

Example:

"body": {
  "total_count": {
    "greaterThan": 1
  },
  "items.any{it.name = 'automationtestingframework'}": {
    "is": true }
}


Inside the field name or the query, some Hamcrest matchers are defined to perform its assertion since REST-assured takes advantage of the power of this one.

Please refer to the Hamcrest Tutorial for more information.

4. StoreResponseValue Method

This keyword method is located after the REST method step to store the specified value from the response of the REST request method step right before.

The next step can use the value stored from this method by the string @var->[var name]@ to verify something. 

Example:

{
  "name": "Store owner and repoName values of the above response",
  "class": "RestAPIAction",
  "method": "storeResponseValue",
  "parameters": {
    "variables": [
     {
      "varName": "owner",
      "key": "items.find{it.name = 'AutomationTesting'}.owner.login" },
     {
      "varName": "repoName",
      "key": "items.find{it.name = 'AutomationTesting'}.name" }
    ]
  } 
}
{
  "name": "Send GET request to get branches",
  "class": "RestAPIAction",
  "method": "GET",
  "parameters": {
    "request": {
      "url": "@var->githubapi@/repos/@var->owner@/@var->repoName@/branches",
      "headers": {
        "Authorization": "@var->authorization@" }
    },
    "response": {
      "statusCode": 200,
      "body": {
        "any{it.name == 'master'}": {
          "is": true }
      }
    }
  }
}


5. ValidateReponse Method

As the method name suggests, you can use this method to validate the response of the REST request method.

The situation of using this method is when you want to add some steps to handle the value from the response of the previous REST Request step, and then you’ll use this method to verify the responses’ values.

So the parameters of this method are defined as well as the response section in the REST request step. 

Example:

{
  "name": "Validate Response",
  "class": "RestAPIAction",
  "method": "validateResponse",
  "parameters": {
    "statusCode": 404,
    "body": {
      "message": {
        "is": "Not Found" }
    }
  }
}


Next Vision

 

 

 

 

Top