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

Kyl*_*yle 5 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>
Run Code Online (Sandbox Code Playgroud)

当我发出请求时,我希望 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>
Run Code Online (Sandbox Code Playgroud)

在 SoapUI 中可以吗?

alb*_*iff 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());
}
Run Code Online (Sandbox Code Playgroud)

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

在 SoapUI 请求中动态创建元素 | 丝金