DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • API Testing With Cypress
  • REST Assured: CRUD Framework for API Testing
  • Get Some Rest! A Full API Stack
  • Cypress API Testing: A Detailed Guide

Trending

  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Handling “Element Is Not Clickable at Point” Exception in Selenium
  • Microservices Design Patterns for Highly Resilient Architecture
  • Test Smells: Cleaning up Unit Tests
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. The Keyword-Driven Test Automation Framework With JSON Format for REST API Testing

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

In this article, learn how to use the keyword-driven test automation framework to implement and execute REST API testing.

By 
Su Gia user avatar
Su Gia
·
Jun. 14, 24 · Tutorial
Like (3)
Save
Tweet
Share
2.6K Views

Join the DZone community and get the full member experience.

Join For Free

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

Test execution progress
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:

  • TestNG: A testing framework
  • REST Assured: A library you can use to test HTTP-based REST services
  • Extent Report 4.0: A framework for creating a test report

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.

  • url: Define the URL you want to send for a request
  • headers: Where you define the header parameters such as Authorization, ContentType, etc.
  • queryParams: Define the query parameters for the request
  • body: Define the request body for POST/ PATCH methods

Response Section

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

  • statusCode: Define the expected value of the response status code as 200,201,204,400, or 501
  • schemaPath: Define the path file that contains the JSON schema format of the response
  • body: Define the expected value of the fields, and parameters in the response; it contains the JSON objects where we can define the field name and the query of identifying a specified value from 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

  • I’m going to tell you how I apply the combination without repetition of the 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 the visually generated test suite/test case JSON file.
API testing JSON REST Test case Framework

Published at DZone with permission of Su Gia. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • API Testing With Cypress
  • REST Assured: CRUD Framework for API Testing
  • Get Some Rest! A Full API Stack
  • Cypress API Testing: A Detailed Guide

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: