我想使用groovy在运行时更新WsdlRequest参数值。 假设我有一个包含两个参数的WsdlRequest:name,address。我想在创建WsdlSubmit实例之前向我的WsdlRequest传递我想要的值。我知道基本代码是这样的:
WsdlProject project = new WsdlProject()
WsdlInterface iface = WsdlInterfaceFactory.importWsdl(project, wsdl, true)[0]
WsdlOperation operation = (WsdlOperation) iface.getOperationAt(3)
WsdlRequest request = operation.addNewRequest(requestName)
request.setRequestContent (requestContent);
requestContent是String格式的soapxml。有没有一种很好的方法来插入我的值(比方说我希望名称值为'Test',地址值为'Example'作为请求)?我宁愿不将xml存储为字符串,如果我在生成请求时已经有了这些信息,请更新它。
以下是xml:
的示例<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:data="http://test.com">
<soapenv:Header/>
<soapenv:Body>
<data:updateFieldName>
<fieldId>?</fieldId>
<!--Optional:-->
<newFieldId>?</newFieldId>
</data:updateFieldName>
</soapenv:Body>
</soapenv:Envelope>
在创建WsdlRequest之前,我创建了一个groovy对象,其中包含我要填充到上面的soap xml消息中的值。假设这个对象声明fieldId = 10和newFieldRequest = 15.我不知道如何将这些值传递给请求。有没有办法用SoapUI API做到这一点?我也有专业许可证。
答案 0 :(得分:2)
您可以使用XMLHolder
来解析xml,并且可以使用setNodeValue(xpath, value)
指定此节点的值,在您的情况下如下所示:
import com.eviware.soapui.support.XmlHolder
// your request content
def requestContent = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:data="http://test.com">'+
'<soapenv:Header/>'+
'<soapenv:Body>'+
'<data:updateFieldName>'+
'<fieldId>?</fieldId>'+
'<newFieldId>?</newFieldId>'+
'</data:updateFieldName>'+
'</soapenv:Body>'+
'</soapenv:Envelope>'
// parse it as xml bean
def requestXml = new XmlHolder(requestContent)
// set your node values
requestXml.setNodeValue("//*:fieldId","10");
requestXml.setNodeValue("//*:newFieldId","15");
然后再次将xml内容设为string
,您可以使用getXml()
方法,如下所示:
WsdlRequest request = ...
// to set in your request use getXml()
request.setRequestContent (requestXml.getXml());
有关详细信息,请查看XMLHolder api documentation
还有另一种方法可以在没有groovy脚本的情况下执行此操作;使用属性。您可以在TestCase
中添加属性,然后直接在TestStep
请求中使用,如下所示:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:data="http://test.com">
<soapenv:Header/>
<soapenv:Body>
<data:updateFieldName>
<fieldId>${#TestCase#yourProperty}</fieldId>
<newFieldId>${#TestCase#anotherProperty}</newFieldId>
</data:updateFieldName>
</soapenv:Body>
</soapenv:Envelope>
如果您对此感兴趣,请查看:Working with properties和property expansion
基于评论的编辑:
我使用公共wsdl
为您的代码和xml持有者编写了一个完整的示例,因为您可以尝试在没有NPE的情况下获得结果,并与您进行比较以检查发生了什么:
import com.eviware.soapui.impl.wsdl.WsdlProject
import com.eviware.soapui.impl.wsdl.WsdlInterface
import com.eviware.soapui.impl.WsdlInterfaceFactory
import com.eviware.soapui.impl.wsdl.WsdlOperation
import com.eviware.soapui.impl.wsdl.WsdlRequest
import com.eviware.soapui.support.XmlHolder
wsdl = "http://www.webservicex.net/geoipservice.asmx?WSDL"
WsdlProject project = new WsdlProject()
WsdlInterface iface = WsdlInterfaceFactory.importWsdl(project, wsdl, true )[0]
WsdlOperation operation = (WsdlOperation) iface.getOperationByName( "GetGeoIP" )
WsdlRequest request = operation.addNewRequest("Request")
def defaultRequest = operation.createRequest(true)
def xmlHolder = new XmlHolder(defaultRequest)
xmlHolder.setNodeValue("//*:IPAddress","127.0.0.1");
request.setRequestContent (xmlHolder.getXml());
希望这有帮助,
答案 1 :(得分:0)
以下是以下的groovy脚本:
import static com.eviware.soapui.impl.wsdl.actions.iface.UpdateInterfaceAction.recreateRequests
import static com.eviware.soapui.impl.wsdl.actions.iface.UpdateInterfaceAction.recreateTestRequests
project = testRunner.testCase.testSuite.project; //get the project reference
def ifaceList = project.getInterfaceList(); //get all the interfaces present in the project in a list
//start a loop for number of interfaces
for(int i = 0; i < project.getInterfaceCount() ; i++)
{
def iface = project.getInterfaceAt(i);
def url = iface.definition;
iface.updateDefinition( url, true); //updateDefinition(String url , Boolean createRequests)
//The above part updates the definition
//The part below recreates the requests based on updated wsdl definition
//syntax -
//recreateRequests( WsdlInterface iface, boolean buildOptional, boolean createBackups, boolean keepExisting, boolean keepHeaders )
recreateRequests(iface,true,true,true,true);
recreateTestRequests(iface,true,true,true,true);
}
//End of Script//