Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
5 years ago
Solved

[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.

Difficulty

 

Some resources you might find useful:

JSON Response Example:

 

 

 

 

 

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

 

 

 

 

 

Have fun!

  • 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

3 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    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

  • 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 🙂

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

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