SoapUI Groovy: Slurping JSON in Script Assertions

Groovy has what’s called a JsonSlurper. This takes the given response and ‘slurps’ it so we can assert on various properties. All examples below are for Script Assertions, see my other Groovy post if you’d like to make these into actual Groovy Scripts within a Test Case. Also, a lot of these you can put into various loops (I’ve outlined a few) to check if values aren’t null, etc, but I’ve left them out for the most part just for simplifying the explanations.

See also Test Automation using the JsonSlurper and JsonBuilder for a way to create Json using similar techniques.

1) Assert values are correct for JSON Response

Response:

{
   "Id": 1,
   "Type": "String",
   "GUID": "String-ABC"
}

Script Assertion:

//imports
import groovy.json.JsonSlurper

//grab the response
def ResponseMessage = messageExchange.response.responseContent
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)

//verify the slurper isn't empty
assert !(jsonSlurper.isEmpty())

//verify the Id, Type and Guid aren't null
assert jsonSlurper.Id != null
assert jsonSlurper.Type != null
assert jsonSlurper.GUID != null

//verify Id is 1
assert jsonSlurper.Id == 1

//verify Type and GUID are Strings
assert jsonSlurper.Type == "String"
assert jsonSlurper.GUID == "String-ABC"

2) Assert values are correct a level deep

Response:

{
   "Id": 1,
   "ObjectSummary": {
   "Type": "String",
   "GUID": "String-ABC"
   }
}

Script Assertion:

//imports
import groovy.json.JsonSlurper

//grab the response
def ResponseMessage = messageExchange.response.responseContent
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)

//verify the slurper isn't empty
assert !(jsonSlurper.isEmpty())

//verify the Id, Type and Guid aren't null
assert jsonSlurper.Id != null
assert jsonSlurper.ObjectSummary.Type != null
assert jsonSlurper.ObjectSummary.GUID != null

//verify Id is 1
assert jsonSlurper.Id == 1

//verify Type and GUID are Strings
assert jsonSlurper.ObjectSummary.Type == "String"
assert jsonSlurper.ObjectSummary.GUID == "String-ABC"

3) Assert values that are returned in an array

Response:

{
   "Id": 1,
   "ObjectSummaries": [{
   "Type": "String",
   "GUID": "String-ABC"
   }]
}

Script Assertion:

//imports
import groovy.json.JsonSlurper

//grab the response
def ResponseMessage = messageExchange.response.responseContent
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)

//verify the slurper isn't empty
assert !(jsonSlurper.isEmpty())

//verify the Id, Type and Guid aren't null
assert jsonSlurper.Id != null
assert jsonSlurper.ObjectSummaries[0].Type != null
assert jsonSlurper.ObjectSummaries[0].GUID!= null

//verify Id is 1
assert jsonSlurper.Id == 1

//verify Type and GUID are Strings
assert jsonSlurper.ObjectSummaries[0].Type == "String"
assert jsonSlurper.ObjectSummaries[0].GUID == "String-ABC"

4) A combination of the above using loops and defined functions

Response:

{
   "Id": 1,
   "ObjectSummaries": [{
   "Type": "String1",
   "GUID": "String-ABC1",
   "Description": "StringDescription"
   },
   "Type": "String2",
   "GUID": "String-ABC1"
  }],
  "OverallCount": 2
}

Script Assertion:

import groovy.json.JsonSlurper

def ResponseMessage = messageExchange.response.responseContent
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)

def i = 0
def x = 0

//as long as there are values in the array
while(jsonSlurper.ObjectSummaries[i]!=null){

  id = jsonSlurper.ObjectSummaries[i].Id
  type = jsonSlurper.ObjectSummaries[i].Type
  GUID = jsonSlurper.ObjectSummaries[i].GUID
  type = type.toLowerCase()

  if (CheckIfDescription(jsonSlurper.ObjectSummaries[i]) == true) {
  		//do something here
  }

  if (type.equals("String1") && CheckIfDescription(jsonSlurper.ObjectSummaries[i]) == true) {
      //you'd have your assertion here
  } else if (type.equals("String1") && CheckIfDescription(jsonSlurper.ObjectSummaries[i]) == false) {
     //another assertion
  }  else  {
    //some other random count assertion
   x++
  }
 i++
}

assert jsonSlurper.OverallCount == i

//function to check if Description exists
def CheckIfDescription(value)
{
  if (value.containsKey("Description") && value.Description != null) {
    return true
  } else {
    return false
  }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s