Groovy class to extract json returns null -
i'm in process of adding library of reusable functions. i've added class extract json soap response null in return. script below works fine, issue when add class.
import com.eviware.soapui.support.types.stringtostringmap import groovy.json.jsonslurper import groovy.json.* responsecontent = testrunner.testcase.getteststepbyname("teststepname").getpropertyvalue("response"); slurperresponse = new jsonslurper().parsetext(responsecontent); log.info (slurperresponse.items[0].id); //write response property def addresponseid = slurperresponse.items[0].id testrunner.testcase.testsuite.setpropertyvalue("scheduleid", addresponseid)
the class:
import com.eviware.soapui.support.types.stringtostringmap; import groovy.json.jsonslurper; import groovy.json.*; class getxmlnode { def log def context def testrunner def responsesoaxmlstep def resultvalue def responsenodepath def responsecontent = "mycontent" def slurperresponse = "myresponse" def storeproperty = "id" // class constructor same case class name def getxmlnode(login,contextin,testrunnerin) { this.log = login this.context = contextin this.responsesoaxmlstep = responsesoaxmlstep this.responsenodepath = responsenodepath this.testrunner = testrunnerin this.storeproperty = storeproperty } def execute(responsesoaxmlstep,responsenodepath,storeproperty) { responsecontent = testrunner.testcase.getteststepbyname(responsesoaxmlstep).getpropertyvalue("response"); slurperresponse = new jsonslurper().parsetext(responsecontent); resultvalue = (slurperresponse.responsenodepath); log.info("node value: " + resultvalue ); testrunner.testcase.testsuite.project.setpropertyvalue(storeproperty, resultvalue ); return responsesoaxmlstep } } context.setproperty( "getxmlnode", new getxmlnode( log, context, testrunner) )
the calling script:
// reference library testsuite library = testrunner.gettestcase().gettestsuite().getproject().getworkspace().getprojectbyname("library") library = library.gettestsuitebyname("library"); // find module within library module = library.testcases["library"].teststeps["getxmlnodevaluebyjson"] // initialise library; places instance of example in context module.run(testrunner, context) // instance of example context. def getxmlnode = context.getxmlnode // run method, parameter log.info "getxmlnode.execute(responsesoaxmlstep,responsenodepath,storeproperty) = " + getxmlnode.execute("teststepname","items[0].id","jsonid")
the response: wed nov 19 14:00:58 cat 2014:info:node value: null
updated code:
import com.eviware.soapui.support.types.stringtostringmap; import groovy.json.jsonslurper; import groovy.json.*; class getxmlnode { def log def context def testrunner def responsesoaxmlstep def resultvalue def responsenodepath = "items[0].id" def responsecontent = "" def slurperresponse = "" def storeproperty = "" // class constructor same case class name def getxmlnode(login,contextin,testrunnerin) { this.log = login this.context = contextin this.responsesoaxmlstep = responsesoaxmlstep this.responsenodepath = responsenodepath this.testrunner = testrunnerin this.storeproperty = storeproperty } def execute(responsesoaxmlstep,responsenodepath,storeproperty) { def groovyutils = new com.eviware.soapui.support.groovyutils( context ); responsecontent = testrunner.testcase.getteststepbyname(responsesoaxmlstep).getproperty("response").getvalue(); def jsonslurper = new jsonslurper().parsetext(responsecontent); // log.info (slurperresponse.responsenodepath); // def holder = groovyutils.getxmlholder( responsesoaxmlstep+"#responseasxml" ); resultvalue = "resultvalue " + jsonslurper[responsenodepath]; log.info("node value: " + resultvalue ); log.info("response node path: " +responsenodepath) testrunner.testcase.testsuite.project.setpropertyvalue(storeproperty, resultvalue ); return responsesoaxmlstep } } context.setproperty( "getxmlnode", new getxmlnode( log, context, testrunner) )
response: tue dec 02 10:16:33 cat 2014:info:node value: resultvalue null
Comments
Post a Comment