【问题标题】:SoapUI Pro, validating response using groovy scriptSoapUI Pro,使用 groovy 脚本验证响应
【发布时间】:2013-12-02 20:35:49
【问题描述】:

我正在使用 SoapUI Pro 测试用于创建邮政快件的 Web 服务。

我正在使用一个 groovy 脚本来验证我的 Web 服务请求。如果我希望请求成功,我会在“状态”字段中查找“已分配”值。如果我预计请求会失败,我会在集成页脚中查找正确的错误代码和 errorDescription。

有时有效的请求会有警告信息(例如,通知用户字段数据太长并且已被截断)。我也想验证这些警告消息。

我在数据源文件中指定要验证的元素路径,然后将其传递给进行验证的 groovy 脚本。

groovy 脚本检索元素路径中的值,并使用 .. 将其分配给变量 actualReturn1。

actualReturn1  = holder.getNodeValue(testElementOne);

其中 testElementOne 可以是

//NS1:completedShipmentInfo/NS1:status/status/statusCode/code

//NS1:createShipmentResponse/NS1:integrationFooter/errors/error/errorCode

第一个路径是有效的,并且正确地将这个状态字段的值分配给actualReturn1

但第二条路径似乎无效,并将 null 分配给 actualReturn1。

以下是我尝试从中提取数据的 2 个响应文件的一部分。

成功提取状态元素的响应..

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <NS1:createShipmentResponse xmlns:NS1="http://www.royalmailgroup.com/api/ship/V1">
         <NS1:integrationHeader>
            <dateTime xmlns="http://www.royalmailgroup.com/integration/core/V1">2013-12-02T17:06:11</dateTime>
            <version xmlns="http://www.royalmailgroup.com/integration/core/V1">1</version>
            <identification xmlns="http://www.royalmailgroup.com/integration/core/V1">
               <applicationId>111111113</applicationId>
               <transactionId>420642961</transactionId>
            </identification>
         </NS1:integrationHeader>
         <NS1:completedShipmentInfo>
            <NS1:status>
               <status>
                  <statusCode>
                     <code>Allocated</code>
                  </statusCode>
               </status>

带有无法提取的错误代码的响应......

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <NS1:createShipmentResponse xmlns:NS1="http://www.royalmailgroup.com/api/ship/V1">
         <NS1:integrationHeader>
            <dateTime xmlns="http://www.royalmailgroup.com/integration/core/V1">2013-12-02T17:06:13</dateTime>
            <version xmlns="http://www.royalmailgroup.com/integration/core/V1">1</version>
            <identification xmlns="http://www.royalmailgroup.com/integration/core/V1">
               <applicationId>111111113</applicationId>
               <transactionId>420642961</transactionId>
            </identification>
         </NS1:integrationHeader>
         <NS1:integrationFooter>
            <errors xmlns="http://www.royalmailgroup.com/integration/core/V1">
               <error>
                  <errorCode>E1101</errorCode>
                  <errorDescription>Name is a required field</errorDescription>
               </error>
            </errors>

有人能告诉我为什么这不适用于第二个响应吗?如果我在有效响应中有警告消息,那么我也无法提取该值。是因为这是在 integrationFooter 中吗?

【问题讨论】:

    标签: soap groovy soapui


    【解决方案1】:

    现在正好看到这个,所以添加答案。

    在第二种情况下,由于命名空间问题,它不适合您。

    元素errors 使用默认命名空间,与它的父元素integrationFooter 不同。在您的xpath 中,没有命名空间涉及元素错误及其子元素。

    这是适用于第二种情况的脚本:

    def xml = '''
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
       <soapenv:Body>
          <NS1:createShipmentResponse xmlns:NS1="http://www.royalmailgroup.com/api/ship/V1">
             <NS1:integrationHeader>
                <dateTime xmlns="http://www.royalmailgroup.com/integration/core/V1">2013-12-02T17:06:13</dateTime>
                <version xmlns="http://www.royalmailgroup.com/integration/core/V1">1</version>
                <identification xmlns="http://www.royalmailgroup.com/integration/core/V1">
                   <applicationId>111111113</applicationId>
                   <transactionId>420642961</transactionId>
                </identification>
             </NS1:integrationHeader>
             <NS1:integrationFooter>
                <errors xmlns="http://www.royalmailgroup.com/integration/core/V1">
                   <error>
                      <errorCode>E1101</errorCode>
                      <errorDescription>Name is a required field</errorDescription>
                   </error>
                </errors>
             </NS1:integrationFooter>
          </NS1:createShipmentResponse>
       </soapenv:Body>
    </soapenv:Envelope>'''
    def holder = new com.eviware.soapui.support.XmlHolder( xml )
    holder.declareNamespace('a','http://www.royalmailgroup.com/api/ship/V1')
    holder.declareNamespace('b','http://www.royalmailgroup.com/integration/core/V1' )
    def errorCode = holder.getNodeValue( "//a:integrationFooter/b:errors/b:error/b:errorCode" )
    assert errorCode == 'E1101'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多