KeyofSea123
13 years agoContributor
JsonSlurper and Overflow
I'm not new to SoapUI but I'm pretty new to Groovy scripting. I'm supporting a web services team. We have Soap requests and responses which are fine. We have REST requests and responses which are fine. We have REST requests with JSON responses and this is where I'm having some difficulty.
This script is for the REST response and it works. I need to create a script for the JSON response that accomplishes the same thing.
***********************************************
import com.eviware.soapui.support.XmlHolder
// Update the 3 lines below to copy paste this code.
def testCase = context.expand( '${DataSource CGD3#TestCase}' )
def testCaseNumber = context.expand( '${DataSource CGD3#TestCaseNumber}' )
def stepName = "Get_Child_Guaranteed_Detail3"
def request = testRunner.testCase.testSteps[stepName].testRequest
def response = testRunner.testCase.testSteps[stepName].getPropertyValue("Response")
def msgExch = request.messageExchange
def rawRequest = new String(msgExch.rawRequestData)
log.info rawRequest
def dateString = new Date()
folderName = context.expand('${projectDir}') + "/Messages/" + dateString.format("YYYY_MM_dd")
def createFolder = new File(folderName)
if (!createFolder.exists())
{
createFolder.mkdirs()
}
def responseHolder = new XmlHolder(response)
def file = new File(folderName+'/'+ stepName + '.txt')
def fw = new FileWriter(file,true)
def pw = new PrintWriter(fw);
pw.println("###################################################################################")
pw.println( "$dateString-----------TestCase: $testCaseNumber $testCase -------------------")
pw.println("###################################################################################")
pw.println(rawRequest)
pw.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
pw.println(responseHolder.prettyXml)
pw.close()
// Clean up
responseHolder.clear()
***********************************************
I've been trying to use the JsonSlurper but I can't seem to get it to work right. The closest that I've come is an Overflow error. This is what I have so far. The JSON response is 113 lines, if that matters.
***********************************************
import groovy.json.*
// Update the 3 lines below to copy paste this code.
def testCase = context.expand( '${DataSource CGD3#TestCase}' )
def testCaseNumber = context.expand( '${DataSource CGD3#TestCaseNumber}' )
def stepName = "Get_Child_Guaranteed_Detail3_JSON"
def request = testRunner.testCase.testSteps[stepName].testRequest
def response = testRunner.testCase.testSteps[stepName].testRequest.response.contentAsString
log.info response
def list = new JsonSlurper().parseText(response)
log.info list.each
***********************************************
I'm not sure where to go from here. Any help will be extremely appreciated.
This script is for the REST response and it works. I need to create a script for the JSON response that accomplishes the same thing.
***********************************************
import com.eviware.soapui.support.XmlHolder
// Update the 3 lines below to copy paste this code.
def testCase = context.expand( '${DataSource CGD3#TestCase}' )
def testCaseNumber = context.expand( '${DataSource CGD3#TestCaseNumber}' )
def stepName = "Get_Child_Guaranteed_Detail3"
def request = testRunner.testCase.testSteps[stepName].testRequest
def response = testRunner.testCase.testSteps[stepName].getPropertyValue("Response")
def msgExch = request.messageExchange
def rawRequest = new String(msgExch.rawRequestData)
log.info rawRequest
def dateString = new Date()
folderName = context.expand('${projectDir}') + "/Messages/" + dateString.format("YYYY_MM_dd")
def createFolder = new File(folderName)
if (!createFolder.exists())
{
createFolder.mkdirs()
}
def responseHolder = new XmlHolder(response)
def file = new File(folderName+'/'+ stepName + '.txt')
def fw = new FileWriter(file,true)
def pw = new PrintWriter(fw);
pw.println("###################################################################################")
pw.println( "$dateString-----------TestCase: $testCaseNumber $testCase -------------------")
pw.println("###################################################################################")
pw.println(rawRequest)
pw.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
pw.println(responseHolder.prettyXml)
pw.close()
// Clean up
responseHolder.clear()
***********************************************
I've been trying to use the JsonSlurper but I can't seem to get it to work right. The closest that I've come is an Overflow error. This is what I have so far. The JSON response is 113 lines, if that matters.
***********************************************
import groovy.json.*
// Update the 3 lines below to copy paste this code.
def testCase = context.expand( '${DataSource CGD3#TestCase}' )
def testCaseNumber = context.expand( '${DataSource CGD3#TestCaseNumber}' )
def stepName = "Get_Child_Guaranteed_Detail3_JSON"
def request = testRunner.testCase.testSteps[stepName].testRequest
def response = testRunner.testCase.testSteps[stepName].testRequest.response.contentAsString
log.info response
def list = new JsonSlurper().parseText(response)
log.info list.each
***********************************************
I'm not sure where to go from here. Any help will be extremely appreciated.