Site icon QATechTools

How to do Schema validation in Karate

When it comes to API testing schema validation is one the most important part. Using karate you can easily perform your scheme validation.
In this post I will validate the GitHub api which will fetch an requested API from the authenticated User and will validate the schema of the same.

First I will call the getRepo feature and store the response in a variable.

Now we will verify the schema of the response. You can do it in multiple ways. You need to first create your schema from your response.In my example response below I have a Owner Json object for which I have created a separate schema json file

 {
  "stargazers_count": 0,
  "pushed_at": "2020-05-19T18:12:06Z",
  "owner": {
    "gists_url": "https://api.github.com/users/QAtechtools/gists{/gist_id}",
    "repos_url": "https://api.github.com/users/QAtechtools/repos",
    "following_url": "https://api.github.com/users/QAtechtools/following{/other_user}",
    "starred_url": "https://api.github.com/users/QAtechtools/starred{/owner}{/repo}",
    "login": "QAtechtools",
    "followers_url": "https://api.github.com/users/QAtechtools/followers",
    "type": "User",
    "url": "https://api.github.com/users/QAtechtools",
    "subscriptions_url": "https://api.github.com/users/QAtechtools/subscriptions",
    "received_events_url": "https://api.github.com/users/QAtechtools/received_events",
    "avatar_url": "https://avatars3.githubusercontent.com/u/65459490?v=4",
    "events_url": "https://api.github.com/users/QAtechtools/events{/privacy}",
    "html_url": "https://github.com/QAtechtools",
    "site_admin": false,
    "id": 65459490,
    "gravatar_id": "",
    "node_id": "MDQ6VXNlcjY1NDU5NDkw",
    "organizations_url": "https://api.github.com/users/QAtechtools/orgs"
  }
}

I have saved the schema of in the ownerSchema.json. and I will use this schema to validate the response.

{gists_url:'#string',repos_url:'#string',following_url:'#string',starred_url:'#string',login:'#string',followers_url:'#string',type:'#string',url: '#string',subscriptions_url:'#string',received_events_url:'#string',avatar_url:'#string',events_url:'#string',html_url:'#string',site_admin:'#string',id:'#number',gravatar_id:'#string',node_id:'#string',organizations_url:'#string'}

To understand more about the schema creation in Karate look at the below image.

Now the Validation code will look like something below.

Feature: Creating a repo in GIT using karate

  Background: Set the baseURL and call the authentication file
    * url baseURL
    * header Authorization = call read('basic-auth.js')

    Scenario: creating a repo and verifying the response
      # change the endpoint to GET
      * path '/user/repos'
      * method get
      #verify the status code
      * status 200
      * def allRepo = response
      * print allRepo
      #match the response with the name that was generated
      * def filterandfind = function(x){ return x.name == 'yDCbu' }
      #using karate.filter to find out repo
      * def myRepo = karate.filter(allRepo, filterandfind)
      * def myRepoName = $myRepo..name
      #once found we will get the report details
      * path '/repos/'+username+'/'+myRepoName[0]
      * method get
      * print myRepoName
      #verify the status code
      * print response
      * status 200

      #match the response with the name that was generated
      * match response.name == myRepoName[0]
      * def nameSchema = { name:  '#string'}
      * def ownerSchema = read('getRepoOwnerSchema.json')
      * def finalSchema = { "owner" : '#(ownerSchema)' }
      #Validate the owner part
      * match response contains any '#(finalSchema)'
      #alidating only the name
      * match response contains nameSchema
     


Exit mobile version