Forum Discussion

M0nkeyG's avatar
M0nkeyG
New Contributor
6 years ago
Solved

SOAP UI assert on JSON Response check not 0

Hi. Cannot find an aswer in other posts, hoping this isn't too niche! Basically I have an API getting a response on data size (along with a few other APIs calling on other info) I have automated ...
  • M0nkeyG's avatar
    6 years ago

    Thank you to: https://thetestsuite.wordpress.com/2013/09/30/soapui-groovy-slurping-json-in-script-assertions/

     

    Script assert....

     

    //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 queue size is not 0
    assert jsonSlurper.lastRecordedQueueSize != 0

    //verify the queue size is not null
    assert jsonSlurper.lastRecordedQueueSize != null

     

    Can't figure out the bonus point question though (yet) - can't see how to verify 1 assert from one response against the value of another assert from another response - playing with Groovy script but anyone got any tips on this would be amazeballs!

  • M0nkeyG's avatar
    M0nkeyG
    6 years ago

    //imports
    import groovy.json.JsonSlurper

    //grab the response
    def ResponseMessage = testRunner.testCase.testSteps["Local Cluster During Flush"].testRequest.response.responseContent
    def oldResponseMessage = testRunner.testCase.testSteps["Local Cluster With Queued Data"].testRequest.response.responseContent
    //define a JsonSlurper
    def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
    def oldJsonSlurper = new JsonSlurper().parseText(oldResponseMessage)

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

    //verify the queue size is not 0
    assert jsonSlurper.lastRecordedQueueSize != 0

    //verify the queue size is not null
    assert jsonSlurper.lastRecordedQueueSize != null

    // Convert old size String to an Integer
    def oldSizeStr = oldJsonSlurper.lastRecordedQueueSize
    oldSizeStr = oldSizeStr.substring(0, oldSizeStr.indexOf("(")).trim()
    oldSizeStr = oldSizeStr.replaceAll(",", "")
    int oldSize = oldSizeStr.toInteger()

    // Convert new size String to an Integer
    def newSizeStr = jsonSlurper.lastRecordedQueueSize
    newSizeStr = newSizeStr.substring(0, newSizeStr.indexOf("(")).trim()
    newSizeStr = newSizeStr.replaceAll(",", "")
    int newSize = newSizeStr.toInteger()

    // Compare old and new sizes
    assert newSize < oldSize