cancel
Showing results for 
Search instead for 
Did you mean: 

[TechCorner Challenge #3] How to Go Through All Response Nodes and Check Value of Required Field

SOLVED
Highlighted
Community Manager

[TechCorner Challenge #3] How to Go Through All Response Nodes and Check Value of Required Field

Hi Community!

 

Here’s a new challenge the results of which will make lives of many ReadyAPI users easier.

 

When working with dynamic responses, testers often need to create Groovy script assertions. One of popular use cases is checking whether the required field matches a specific value.

 

And, this is your next task:

 

Write a script for the script assertion (for the REST test step) which will go through all response nodes and compare the value of the name field with the required value. The number of nodes is dynamic.

DifficultyStar_Gold.pngStar_gray.pngStar_gray.png

 

Some resources you might find useful:

JSON Response Example:

 

 

 

 

 

{"items":[{
"code":1,
"name":"test"},
{"code":2,
"name":"test"},
{"code":3,
"name":"test"}]
}

 

 

 

 

 

Have fun!


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

How to Go Through All Response Nodes and Check Value of Required Field

Task: Write a script for the script assertion (for the REST test step) which will go through all response nodes and compare the value of the name field with the required value. The number of nodes is dynamic.

 

This is a solution created for [TechCorner Challenge #3]

 

Similar question answered numerous times in the forum I believe.

 

Taking little different json (but with same structure as given in the question) for better understanding.

 

Assume that user received the below json as response, say get cars.

 

 

 

{
  "cars": [
    {
      "name" : "Jetta",
      "brand" : "Volkswagen"
    },
    {
      "name" : "Polo GT",
      "brand" : "Volkswagen"
    },
    {
      "name" : "i30",
      "brand" : "Hyundai"
    }
  ]
}

 

 

 

Now he wants to assert cars of brand "Volkswagen"

 

Here goes the script assertion for the getCars REST call

 

 

 

 

//User looking for below data in the response
def brandToCheck = 'Volkswagen'
def expectedCars = ['Jetta', 'Polo GT']

//to read dynamic response; you can test it with fixed json as well
def jsonText = context.response 

//Get all the cars
def cars = new groovy.json.JsonSlurper().parseText(jsonText).cars

//Verify if the expectedCars in the response for the given brand
assert expectedCars == cars.findAll { it.brand == brandToCheck }.name, 'Not matching the expected cars'

 

 

 

 

Another test can be validate the brand of a car name, say i30

 

 

 

//User want to validate brand of i30
assert 'Hyundai' == cars.find { it.name == 'i30'}.brand, 'Not matching the brand for i30 car'

 

 

 

One can test it online (but using fixed json)

https://ideone.com/CaLMHy



Regards,
Rao.

View solution in original post

Tags (1)
3 REPLIES 3
Highlighted
Community Hero

How to Go Through All Response Nodes and Check Value of Required Field

Task: Write a script for the script assertion (for the REST test step) which will go through all response nodes and compare the value of the name field with the required value. The number of nodes is dynamic.

 

This is a solution created for [TechCorner Challenge #3]

 

Similar question answered numerous times in the forum I believe.

 

Taking little different json (but with same structure as given in the question) for better understanding.

 

Assume that user received the below json as response, say get cars.

 

 

 

{
  "cars": [
    {
      "name" : "Jetta",
      "brand" : "Volkswagen"
    },
    {
      "name" : "Polo GT",
      "brand" : "Volkswagen"
    },
    {
      "name" : "i30",
      "brand" : "Hyundai"
    }
  ]
}

 

 

 

Now he wants to assert cars of brand "Volkswagen"

 

Here goes the script assertion for the getCars REST call

 

 

 

 

//User looking for below data in the response
def brandToCheck = 'Volkswagen'
def expectedCars = ['Jetta', 'Polo GT']

//to read dynamic response; you can test it with fixed json as well
def jsonText = context.response 

//Get all the cars
def cars = new groovy.json.JsonSlurper().parseText(jsonText).cars

//Verify if the expectedCars in the response for the given brand
assert expectedCars == cars.findAll { it.brand == brandToCheck }.name, 'Not matching the expected cars'

 

 

 

 

Another test can be validate the brand of a car name, say i30

 

 

 

//User want to validate brand of i30
assert 'Hyundai' == cars.find { it.name == 'i30'}.brand, 'Not matching the brand for i30 car'

 

 

 

One can test it online (but using fixed json)

https://ideone.com/CaLMHy



Regards,
Rao.

View solution in original post

Tags (1)
Highlighted
Community Leader

Re: Your Input Needed - How to Go Through All Response Nodes and Check Value of Required Field

@sonya_m :

 

Write a script for the script assertion (for the REST test step) which will go through all response nodes and compare the value of the name field with the required value. The number of nodes is dynamic.

 

Yes sometime we do need to write custom code to assert on particular field. So, below is my way of asserting/validating the response via Script assertion.

 

def response = '''{
  "items": [
    {
      "code": 1,
      "name": "test"
    },
    {
      "code": 2,
      "name": "test"
    },
    {
      "code": 13,
      "name": "test"
    }
  ]
}'''

import groovy.json.JsonSlurper

def expectedCode = 3
def expectedName = "test"
boolean flag = false
def jsonSlurper = new JsonSlurper().parseText(response)
def itemSize = jsonSlurper.items.size()
for(int i = 0 ; i < itemSize ; i++){
	if(expectedCode == jsonSlurper.items[i].code)
		flag = true
		assert jsonSlurper.items[i].name == expectedName : "Name doesn't match"
}
assert flag , "Expected Code : "+expectedCode+" not Found in Response"

 

I hope it will pass all your desired Test Cases and answer your question too 🙂

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Community Manager

Re: Your Input Needed - How to Go Through All Response Nodes and Check Value of Required Field

Thank you a lot Rao, Himanshu. Job well done!


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors