首页 > 解决方案 > xml 中的 SOAUI Groovy Getresponsenode.value 为空

问题描述

我有一个问题,我尝试了非常不同的场景,我是一个新的 api 服务自动化测试人员,我在我的服务中使用了 soapui 和 groovy。

所以这就是问题所在:我有一个响应请求的服务,我想从响应中获取特定的数据/值。这就是问题开始的地方,因为我已经传递了变量的 xpath 及其值但是当我在 groovy 脚本上打印日志,它会打印一个空值。

任何有类似问题的人都可以帮助吗?对此表示感谢。

我的回复中重要的部分在哪里:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <ns0:outputMessage xmlns:ns0="urn://soa/es/Theater/GetBarProducts/Root">
         <ns1:Header xmlns:ns1="http://xmlns.soa.com/SharedResources">
            <ns1:timeout>14500</ns1:timeout>
            <ns1:creationTime>2019-09-23T11:07:20+01:00</ns1:creationTime>
            <ns1:iTrackingID>f7e8ea93-9736-4380-97ab-846b9cc69fbd</ns1:iTrackingID>
            <ns1:eTrackingID>>f81c1210-88fa-4188-8358-b86825d5df70</ns1:eTrackingID>
            <ns1:application>CINEMAS</ns1:application>
            <ns1:user>APIQMS</ns1:user>
            <ns1:process>GetBarProducts</ns1:process>
            <shar:AccessData xmlns:data="urn://soa/es/Theater/GetBarProducts/Data" xmlns:jms1="http://www.tibco.com/namespaces/tnt/plugins/jms" xmlns:ns0="http://xmlns.soa.com/SharedResources" xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:oas1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:root="urn://soa/es/Theater/GetBarProducts/Root" xmlns:shar="http://xmlns.soa.com/SharedResources" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
               <shar:userOS>os</shar:userOS>
               <shar:remoteHost>host</shar:remoteHost>
               <shar:remoteIP>ip</shar:remoteIP>
               <shar:otherInfo>info</shar:otherInfo>
            </shar:AccessData>
            <ns0:Status xmlns:jms1="http://www.tibco.com/namespaces/tnt/plugins/jms" xmlns:ns0="http://xmlns.soa.com/SharedResources">
               <ns0:eCodes>
                  <ns0:eCode>COM-000</ns0:eCode>
                  <ns0:eDescription>Success</ns0:eDescription>
               </ns0:eCodes>
               <ns0:eNative>
                  <ns0:eCode>OK</ns0:eCode>
                  <ns0:eDescription/>
                  <ns0:eSystem>ArpuxNew</ns0:eSystem>
               </ns0:eNative>
            </ns0:Status>
         </ns1:Header>
         <ns1:DataOutput xmlns:ns1="urn://soa/es/Theater/GetBarProducts/Data">
            <ns1:TheaterProducts>
               <ns1:TheaterProduct Description="Menu_Pequeno" ID="C101" Price="5.4" Size="Pequeno" Type="Menu Pipocas" UUID="00aacfa5-8169-4e53-b315-ee9682461936">
                  <ns1:Group GroupID="App1" Name="Menus"/>
                  <ns1:SubGroup GroupID="App2" Name="Menu Clássico"/>
                  <ns1:Files>
                     <ns1:File Height="400" Path="//cdn.nos.pt/cinemas/products/files/400x400/null" Width="400"/>
                     <ns1:File Height="225" Path="//cdn.nos.pt/cinemas/products/files/225x225/null" Width="225"/>
                     <ns1:File Height="1000" Path="//cdn.nos.pt/cinemas/products/files/1000x1000/null" Width="1000"/>
                  </ns1:Files>
                  <ns1:TheaterProductItems>
                     <ns1:TheaterProductItem>
                        <ns1:TheaterProducts>
                           <ns1:TheaterProduct Description="Caixa Doce Pequena" ID="B028" Index="1" Size="Pequeno" Type="Doces" UUID="6fad59b3-b623-4f59-9fda-a25cf4cc2738">
                              <ns1:Group GroupID="02" Name="Pipocas"/>
                           </ns1:TheaterProduct>
                           <ns1:TheaterProduct Description="Caixa Sal Pequena" ID="B032" Index="2" Size="Pequeno" Type="Salgadas" UUID="4c69ead4-04a6-4413-9e21-bd25a9ab51ee">
                              <ns1:Group GroupID="02" Name="Pipocas"/>
                           </ns1:TheaterProduct>
                           <ns1:TheaterProduct Description="Caixa Mista Pequena" ID="B070" Index="3" Size="Pequeno" Type="Mistas" UUID="663f57b3-0534-4e75-9f41-fedbef008b4a">
                              <ns1:Group GroupID="02" Name="Pipocas"/>
                           </ns1:TheaterProduct>
                        </ns1:TheaterProducts>
                     </ns1:TheaterProductItem>
                     <ns1:TheaterProductItem>
                        <ns1:TheaterProducts>

我想要获取的值是第二个 TheaterProduct 的 ID 和 UUID,如下所示:

<ns1:TheaterProduct Description="Caixa Doce Pequena" ID="B028" Index="1" Size="Pequeno" Type="Doces" UUID="6fad59b3-b623-4f59-9fda-a25cf4cc2738">
                              <ns1:Group GroupID="02" Name="Pipocas"/>
                           </ns1:TheaterProduct>

然后是我创建的这个脚本,但由于我不知道的原因它返回 null,因为我已经尝试了所有可能的解决方案,我已经想到并搜索了。

这是groovy中的脚本:

//vai buscar o valor prentendido ao response
import com.eviware.soapui.support.XmlHolder

//Using context object retrieve the response of a soaprequest - AddRequest
def res = context.expand('${SOAP#response}')
//log.info res

//To parse and get the tags in req/res we have to use XMLHolder class
def response = new XmlHolder(res)

//To retrieve the result of request and response tags using getNodeValue Method

//def reqvalue1 = response.getNodeValue('//ns1:Session[1]/@UUID')
def reqvalue1 = response.getNodeValue('//ns1:TheaterProduct[1]/@UUID')
def reqvalue2 = response.getNodeValue('//ns1:Header/ns1:DataOutput/ns1:TheaterProductItems/ns1:TheaterProductItem/ns1:TheaterProducts/ns1:TheaterProduct[1]/@ID')


log.info reqvalue1
log.info reqvalue2

//cria um ficheiro com o valor da resposta pretendido
//new File('sessionID').text = reqvalue1


//Descomentar a linha seguinte para correr na propria maquina
//e comentar a linha que cria o ficheiro
testRunner.testCase.testSuite.project.setPropertyValue('TheaterProductUUID',reqvalue1)
testRunner.testCase.testSuite.project.setPropertyValue('TheaterProductID',reqvalue2)

情况是我使用了这条适用于不同服务的线路,但它在这里不起作用,这真的很奇怪。

这是该线路在不同服务上工作的证明:

脚本行工作并获得值的不同服务

这是我对我需要的服务的回应:

服务获取空值

如果有人知道任何解决方案或类似的东西会对我有很大帮助。

标签: xmlgroovysoapuiweb-api-testingnodevalue

解决方案


XML 需要定义命名空间并与每个节点相关联。你的代码

def reqvalue1 = response.getNodeValue('//ns1:TheaterProduct[1]/@UUID')

使用 namespace ns1,但是您没有定义它。

对于只读操作,使用通配符命名空间是非常安全的,如下所示:

def reqvalue1 = response.getNodeValue('//*:TheaterProduct[1]/@UUID')

另请注意,内部 SoapUI 将所有内容都表示为 XML。您可以在您的context.expand()线路上准备好节点值,如下所示:

def reqvalue1 = context.expand('${SOAP#Response#//*:TheaterProduct[1]/@UUID}')

SoapUI提示和技巧有一些附加信息。


推荐阅读