我正在向库中添加可重用功能。我添加了一个类来从SOAP响应中提取JSON,但我得到一个NULL作为回报。下面的脚本运行正常,问题是我将它添加到类中。
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 to property
def addResponseID = slurperresponse.items[0].id
testRunner.testCase.testSuite.setPropertyValue("ScheduleId", addResponseID)
班级:
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 with same case as 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) )
致电脚本:
// get a reference to the library TestSuite
library = testRunner.getTestCase().getTestSuite().getProject().getWorkspace().getProjectByName("Library")
library = library.getTestSuiteByName("Library");
// find the module within the library
module = library.testCases["Library"].testSteps["GetXMLNodeValueByJSON"]
// initialise the library; which places an instance of Example in the context
module.run(testRunner, context)
// get the instance of example from the context.
def GetXmlNode = context.GetXmlNode
// run the method, with parameter
log.info "GetXmlNode.execute(responseSOAXmlStep,responseNodePath,storeProperty) = " + GetXmlNode.execute("TestStepName","items[0].id","JsonId")
回应: 11月19日星期三14:00:58 CAT 2014:INFO:节点值:null
更新代码:
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 with same case as 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) )
响应: Tue Dec 02 10:16:33 CAT 2014:INFO:节点值:resultValue null