简体   繁体   中英

How to check request value by groovy in SoapUI

I have simple mock service with request like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fa1="http://TargetNamespace.com/FA1">
   <soapenv:Header/>
   <soapenv:Body>
      <fa1:items>
         <fa1:itemname>logo</fa1:itemname>
         <fa1:price></fa1:price>
         <fa1:vat>10</fa1:vat>
         <fa1:description>nové logo</fa1:description>
      </fa1:items>
   </soapenv:Body>
</soapenv:Envelope>

I have used script to control response value. At the start XmlHolder didn't work. So I used

def req = mockRequest.getContentElement().execQuery("/")
def records = new XmlParser(false, false).parseText(req[0].xmlText())

to get request and parse it into nodes.

{http://TargetNamespace.com/FA1}items[attributes={}; value=[
{http://TargetNamespace.com/FA1}itemname[attributes={}; value=[logo]], 
{http://TargetNamespace.com/FA1}price[attributes={}; value=[1]], 
{http://TargetNamespace.com/FA1}vat[attributes={}; value=[10]], 
{http://TargetNamespace.com/FA1}description[attributes={}; value=[nové logo]]]
]

But I cannot get price value. I have tried

def price = records.price.text()

Expression records.find( { it.text() == "1"} ) returns {http://TargetNamespace.com/FA1}price[attributes={}; value=[1]] {http://TargetNamespace.com/FA1}price[attributes={}; value=[1]] but records.find( { it.name() == "price"} ) alike records.find( { it.name() == "{http://TargetNamespace.com/FA1}price"} ) returns null.

The key is use def records = new XmlParser( false, false ).parseText(req[0].xmlText())

That brings request value like this:

fa1:items[attributes={xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/, xmlns:fa1=http://TargetNamespace.com/FA1}; value=[
fa1:itemname[attributes={}; value=[logo]], 
fa1:price[attributes={}; value=[1]], 
fa1:vat[attributes={}; value=[10]], 
fa1:description[attributes={}; value=[nové logo]]]
]

Then you can read value by name namespace:name so records.find( { it.name() == "fa1:price"} ).text() returns 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM