首页 文章

SoapUI Pro,使用groovy脚本验证响应

提问于
浏览
1

我正在使用SoapUI Pro来测试用于创建邮寄发货货件的网络服务 .

我使用groovy脚本来验证我的Web服务请求 . 如果我希望请求成功,我会在'status'字段中查找'Allocated'值 . 如果我希望请求失败,我会在集成页脚中查找正确的错误代码和errorDescription .

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

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

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

actualReturn1  = holder.getNodeValue(testElementOne);

testElementOne可以是哪个

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

要么

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

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

但第二个路径似乎没有效果,并为actualReturn1指定null .

下面是我的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>

使用无法解压缩的errorCode响应...

<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中吗?

1 回答

  • 0

    现在发生了这件事,所以加上答案 .

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

    元素 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'
    

相关问题