'SOAUI Groovy Getresponsenode.value from xml is null
I have a problem where I have tried very different scenarios, I am a fresh automation tester for api services and I'm using soapui and groovy to my services.
So here is the problem: I have a service that responds to a request and I want to get a specific data/value from the response. That's where the problem starts, because I have passed the xpath of the variable and its value but when I print a log on a groovy script it prints a null.
Can anyone who had some problem similar to this help? Thanks in regard.
Where is the part of my response that matters:
<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>
The values I want to get is the ID and the UUID of second TheaterProduct that is like this:
<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>
Then there is this script that I created but it returns null for reasons I don't know, since I have tried every possible solution I have thought and searched.
Here is the script in 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)
The case is I have used this line that works for a different service but it doesn't work here, which is really weird.
Here a proof of the line working on a different service:
Here is my response for the service I need:
If someone knows about any solution or something like it would help me a lot.
Solution 1:[1]
XML needs to have namespaces defined and associated with each node. Your code
def reqvalue1 = response.getNodeValue('//ns1:TheaterProduct[1]/@UUID')
uses namespace ns1
, however you did not define it.
For read-only operations, it is perfectly safe to use the wildcard namespace, like so:
def reqvalue1 = response.getNodeValue('//*:TheaterProduct[1]/@UUID')
Also note that internally SoapUI keeps everything represented as XML. You can ready the node value right at your context.expand()
line, line so:
def reqvalue1 = context.expand('${SOAP#Response#//*:TheaterProduct[1]/@UUID}')
SoapUI tips & tricks has some additional information.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SiKing |