Key phrase-Pushed Take a look at Automation Framework: REST API – DZone – Uplaza

First, I’d wish to get again somewhat little bit of this framework’s structure overview.


Take a look at Execution Progress

Because the above picture and the earlier story talked about, the take a look at suites/instances/steps are shaped in a JSON file, and the framework will load and map these JSON recordsdata to the listing of take a look at suites/instances objects. Then, motion steps are executed primarily based on the category and technique (key phrase) which might be laid out in every take a look at step (use the reflection approach to invoke the key phrase — technique). Our duty is to jot down the autotest script inside every key phrase.

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

What that you must use this framework is to outline take a look at suites/instances and take a look at steps within the JSON format file.

The supply code is situated on GitHub right here and right here. Obtain or clone it to analysis extra.

The automation libraries used on this framework are as follows:

  • TestNG: A testing framework
  • REST Assured: A library you should use to check HTTP-based REST companies
  • Extent Report 4.0: A framework for making a take a look at report

Subsequent, I’d like to indicate you the take a look at parameters of the RestAPIAction key phrase that will likely be outlined in every REST API take a look at step (please check with the earlier article linked within the introduction to get extra element on the take a look at suite/take a look at case format).

1. GET Technique

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

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

Please check with CRUDRepository.json.

As you’ll be able to see, there are two “Request and Response” sections in every REST Motion technique.

Request Part

That is the place you outline the parameters’ values for sending a REST Request.

  • url: Outline the URL you wish to ship for a request
  • headers: The place you outline the header parameters akin to Authorization, ContentType, and so on.
  • queryParams: Outline the question parameters for the request
  • physique: Outline the request physique for POST/ PATCH strategies

Response Part

That is the place you outline the anticipated worth within the response.

  • statusCode: Outline the anticipated worth of the response standing code as 200,201,204,400, or 501
  • schemaPath: Outline the trail file that comprises the JSON schema format of the response
  • physique: Outline the anticipated worth of the fields, and parameters within the response; it comprises the JSON objects the place we are able to outline the sector title and the question of figuring out a specified worth from the response

Instance:

"body": {
  "total_count": {
    "greaterThan": 1
  },
  "objects.any{it.title="automationtestingframework"}": {
    "is": true
  }
}

Inside the sector title or the question, some Hamcrest matchers are outlined to carry out its assertion since REST-assured takes benefit of the ability of this one.

Please check with the Hamcrest Tutorial for extra data.

4. StoreResponseValue Technique

This key phrase technique is situated after the REST technique step to retailer the desired worth from the response of the REST request technique step proper earlier than.

The subsequent step can use the worth saved from this technique by the string @var->[var name]@ to confirm one thing. 

Instance:

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

As the strategy title suggests, you should use this technique to validate the response of the REST request technique.

The state of affairs of utilizing this technique is once you wish to add some steps to deal with the worth from the response of the earlier REST Request step, and then you definitely’ll use this technique to confirm the responses’ values.

So the parameters of this technique are outlined in addition to the response part within the REST request step. 

Instance:

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

Subsequent Imaginative and prescient

  • I’m going to let you know how I apply the mixture with out repetition of the algorithm to generate a collection of take a look at instances with the intention to enhance the take a look at protection for REST API testing.
  • I’m growing the take a look at device UI to handle the visually generated take a look at suite/take a look at case JSON file.
Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version