The Keyword-Driven Test Automation framework with JSON Format for Rest API Testing

Hien Su
4 min readApr 23, 2020

--

Continuing the story “New Approach, A Complete Keyword-Driven Test Automation Framework with JSON Format”, I’d like to tell you guys more how do I use this framework for implementing and executing Rest API Testing.

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

Test Execution Progress

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

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

So what you need to use this framework is to define Test Suites/ Test cases and test steps in the JSON format file.

The source code is located on GitHub https://github.com/sugia279/AutomationTestingFramework

https://github.com/sugia279/AutomationTestingFramework/tree/master/src/test/java/githubsample_webapitesting

you guys can download or clone it to research more.

The automation libraries are used in this framework.

Next, I’d like to show you the test parameters of RestAPIAction keyword will be defined in each Rest API test step (Please refer the previous story to get more detail of test suite/ test case format).

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
}
}
}
}
}

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"
}
}
}
}
}

PUT/PATCH & DELETE method
Please refer to CRUDRepository.json

As you see, there are 2 sections “Request and Response” in each Rest Action method.

Request section: where you define the parameters’ values for sending a Rest Request

“url”: Define the URL you want to send for a request.

“headers”: where you define the header parameters such as Authorization, ContentType …

“queryParams”: define the query parameters for the request.

“body”: define the request body for POST/ PATCH methods.

Response section: where you define the expected value in the response.

“statusCode”: define the expected value of the response status code is 200/ 201/ 204/ 400 or 501.

“schemaPath”: define the path file where contains the JSON schema format of the response.

“body”: define the expected value of the fields, parameters in the response. It contains the JSON objects where we can define the field name, the query of identifying a specified value from the response. Ex:

"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 http://hamcrest.org/JavaHamcrest/tutorial to get more.

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.

And the next step can use the value is stored from this method by the string ‘@var->[var name]@’ to verify something. Ex:

{
"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
}
}
}
}
}

ValidateReponse method

As the method name, you can use this method to validate the response of the Rest request method before.

The situation of using this method is when you want to add some step 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. Ex:

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

References

  1. https://github.com/sugia279/AutomationTestingFramework
  2. http://rest-assured.io/
  3. http://hamcrest.org/JavaHamcrest/tutorial
  4. http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html

Next vision

  • I’m going to tell you how do I apply the combination without repetition algorithm to generate a suite of test cases in order to increase the test coverage for Rest API Testing.
  • I’m developing the test tool UI to manage and generate the test suite/ test case JSON file in visually.

If you enjoyed reading the article, feel free to give some claps and share it with others. And if you have any concern, you can leave the message in the below comment or contact me at hiensu304@gmail.com

--

--