Groovy 脚本中的 SOAP UI 中的访问响应

access response in SOAP UI in Groovy Script

我是 Groovy 脚本的新手。我正在尝试访问 Response 节点的值 下面是脚本

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def responseHolder = groovyUtils.getXmlHolder( testRunner.testCase.testSteps["request"].testRequest.response.responseContent );
responseHolder.namespaces["ns0"]="http://xmlns.int.com/orders/xsd/v1"
String mySection = responseHolder.getNodeValue["//ns0:MT_OrderCreateDTCFulfillmentResponse/ns0:StatusCode"] ;
log.info mySection

mySection 打印为 []

回复XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header xmlns:v1="http://xmlns.int.com/orders/xsd/v1"/>
   <soapenv:Body xmlns:v1="http://xmlns.int.com/orders/xsd/v1">
      <ns0:MT_OrderCreateDTCFulfillmentResponse xmlns:ns0="http://xmlns.int.com/orders/xsd/v1">
         <StatusCode>000</StatusCode>
         <ReferenceDocNbr>NA</ReferenceDocNbr>
         <SchemaValidationStatus>Validated</SchemaValidationStatus>
         <StatusTimestamp>2015-08-03T18:58:01.602</StatusTimestamp>
         <FaultDetails>Request for customer order number NA received successfully and format validated.</FaultDetails>
      </ns0:MT_OrderCreateDTCFulfillmentResponse>
   </soapenv:Body>
</soapenv:Envelope>

SOAP UI 项目结构 - 我是 运行 Test_Script。告诉我我错过了什么

您必须使用:

responseHolder.getNodeValue("//ns0:MT_OrderCreateDTCFulfillmentResponse/StatusCode");

而不是responseHolder.getNodeValue["//ns0:MT_OrderCreateDTCFulfillmentResponse/ns0:StatusCode"];

请注意,我将 responseHolder.getNodeValue 调用更改为使用 () 而不是 [],并且还更改了您的 xpath,因为在您的回复中 <StatusCode> 它不是在 xmlns:ns0="http://xmlns.int.com/orders/xsd/v1".

中定义

另一种选择是使用 * 通配符作为命名空间来映射任何人。所以在这种情况下你可以使用:

responseHolder.getNodeValue("//*:MT_OrderCreateDTCFulfillmentResponse/*:StatusCode");

此外,请注意您 XML 可能是错误的,因为我认为 <MT_OrderCreateDTCFulfillmentResponse> 的所有子元素都必须属于 "http://xmlns.int.com/orders/xsd/v1" 命名空间...所以您必须声明它是:

<ns0:MT_OrderCreateDTCFulfillmentResponse xmlns:ns0="http://xmlns.int.com/orders/xsd/v1">
         <ns0:StatusCode>000</ns0:StatusCode>
         <ns0:ReferenceDocNbr>NA</ns0:ReferenceDocNbr>
         <ns0:SchemaValidationStatus>Validated</ns0:SchemaValidationStatus>
         <ns0:StatusTimestamp>2015-08-03T18:58:01.602</ns0:StatusTimestamp>
         <ns0:FaultDetails>Request for customer order number NA received successfully and format validated.</ns0:FaultDetails>
      </ns0:MT_OrderCreateDTCFulfillmentResponse>

或将此标签用作默认值:

<MT_OrderCreateDTCFulfillmentResponse xmlns="http://xmlns.int.com/orders/xsd/v1">
         <StatusCode>000</StatusCode>
         <ReferenceDocNbr>NA</ReferenceDocNbr>
         <SchemaValidationStatus>Validated</SchemaValidationStatus>
         <StatusTimestamp>2015-08-03T18:58:01.602</StatusTimestamp>
         <FaultDetails>Request for customer order number NA received successfully and format validated.</FaultDetails>
      </MT_OrderCreateDTCFulfillmentResponse>

请注意,如果您根据我的指示更改您的 XML 您的第一个 XPath,那么它是正确的,因为现在 StatusCode 属于您的命名空间。

希望对您有所帮助,