SoapUI - 自动将自定义SOAP标头添加到传出请求

时间:2014-01-22 08:42:02

标签: soap wsdl soapui

所以我想要做的是自动将SOAP标头添加到SoapUI中生成的每个请求中,因为我已经有数百个请求并且手动执行此操作很烦人。

让我们说这是我从WSDL生成的示例请求,如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pol="http://something">
   <soapenv:Header>
   </soapenv:Header>
   <soapenv:Body>
      <pol:GetSomething>
         <tag1>3504</tag1>
         <tag2>ALL</tag2>
      </pol:GetSomething>
   </soapenv:Body>
</soapenv:Envelope>

当我发出请求时,我希望SoapUI将其修改为:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pol="http://something">
   <soapenv:Header>
      <token xmlns="ns1">${TOKEN}</token>
      <user xmlns="ns2">user</user>
      <system xmlns="ns3">system</system>
   </soapenv:Header>
   <soapenv:Body>
      <pol:GetSomething>
         <tag1>3504</tag1>
         <tag2>ALL</tag2>
      </pol:GetSomething>
   </soapenv:Body>
</soapenv:Envelope>

在SoapUI中可以吗?

1 个答案:

答案 0 :(得分:4)

在你的testCase中,你可以添加Groovy Script类型的第一步,在这个脚本中你可以操作每个请求以在<soap:Header>上添加必要的元素,我给你一个适合我的例子:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
def tcase = testRunner.testCase ;
// get total number of testSteps
def countTestSteps = tcase.getTestStepList().size();
// start with 1 to avoid groovy script testStep
for(i=1;i<countTestSteps;i++){

// get testStep
def testStep = tcase.getTestStepAt(i);
// get request
def request = testStep.getProperty('Request').getValue();
// get XML
def xmlReq = groovyUtils.getXmlHolder(request);
// get SOAPHEADER
def soapHeader = xmlReq.getDomNode("declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/'; //soap:Header")
// document to create new elements
def requestDoc = soapHeader.getOwnerDocument()
// create new element
def newElem = requestDoc.createElementNS(null, "element");
// insert in header
soapHeader.insertBefore(newElem, soapHeader.getFirstChild());
// now put your new request in testStep
log.info xmlReq.getXml();
testStep.setPropertyValue('Request', xmlReq.getXml());
}

此示例代码仅在<soap:header>上添加一个新元素,但您可以对其进行修改以添加属性,文本内容和更多节点。您还可以查看:

dynamically create elements in a SoapUI request | SiKing