recent posts

RESTinstance - How to access my response fields

Tip of the day

Often times when we are testing APIs, we need to validate specific response fields or reuse them in some fashion. This could be reusing an authentication token after signing in, or just validating that the response returned "potatoes" in the field "delicious". Not the greatest example, but you get the point. In this article, I hope to show you how to do field validation using the RESTinstance library for Robotframework.

Here is a breakdown of an example test case that we will be using to demonstrate a GET call that will output the response body and validate a specific set of fields. The full example will be provided at the end.
 GET Existing User  
   GET      /users/1  
   Output   response body  
Firstly, we make the `GET` call to the `/users/1` endpoint, and we expect user 1 to have a specific set of data. Before we go further though, we first `OUTPUT` the response body to see what is returned. As you can see below, the entire response body is outputted.
 JSON for 'response body' is:  
 {  
   "id": 1,  
   "name": "Leanne Graham",  
   "username": "Bret",  
   "email": "Sincere@april.biz",  
   "address": {  
     "street": "Kulas Light",  
     "suite": "Apt. 556",  
     "city": "Gwenborough",  
     "zipcode": "92998-3874",  
     "geo": {  
       "lat": "-37.3159",  
       "lng": "81.1496"  
     }  
   },  
   "phone": "1-770-736-8031 x56442",  
   "website": "hildegard.org",  
   "company": {  
     "name": "Romaguera-Crona",  
     "catchPhrase": "Multi-layered client-server neural-net",  
     "bs": "harness real-time e-markets"  
   }  
 }  
Now that we have the response body, we can pick and choose what we want to validate. In our case, we want to verify the following fields.
  • Verify the returned id is `1`
  • Verify the name is `Leanne Graham`
  • Verify the street is `Kulas Light`
  • Verify the lat is `-37.3159`, we do this so we can demonstrate how to traverse the tree. Notice the pattern yet?
Our final test case will look something like this.
 GET Existing User  
   GET      /users/1  
   Integer  response body id               1  
   String   response body name             Leanne Graham  
   String   response body address street   Kulas Light  
   String   response body address geo lat  -37.3159  
Here we remove the Output, as we no longer need it to figure out what fields to validate. We then verify each individual field by validating both its type and its value. In our case, we want to ensure that the id that is being returned is an Integer and has the value of 1. As for the remaining items, we ensure that they are strings, and we traverse down the json tree to validate each individual value. I hope this brief tutorial helped!

For more examples, visit the github page!


RESTinstance - How to access my response fields RESTinstance - How to access my response fields Reviewed by JJ The Engineer on 5:57 PM Rating: 5

No comments:

Powered by Blogger.