Reputation: 1291
I am trying to build a mock service in SoapUI, which dynamically returns a response, based on a value passed in the request. Example:
<foo>
<bar>
<ID>Response1</ID> <--- I want to extract this
<ReferenceID>stuff</ReferenceID>
<CreationDate>2016-05-01T11:34:56Z</CreationDate>
</bar>
</foo>
So I set my DISPATCH to SCRIPT and tried the following (the return value should specify the name of the response, which is returned):
def req = new XmlSlurper().parseText(mockRequest.requestContent)
return "${req.foo.bar.ID}"
And this:
def holder = new com.eviware.soapui.support.XmlHolder(mockRequest.requestContent )
def arg1 = holder.getNodeValue("ID") // also tried "//ID"
return arg1.toString();
Neither worked, the mock always returns the default response - hope some of you can help me with the solution :)
Upvotes: 0
Views: 2072
Reputation: 18507
The problem is probably that your <foo>
response is wrapped in a SOAP<envelope>
and <body>
so the path you're using with XmlSlurper
is not correct req.foo.bar.ID
.
Furthermore if in your case your response is not wrapper with <envelope>
and <body>
note that in the XmlSlurper
the root
node starts at the object itself so the req.foo
is not needed since <foo>
is the root node, looks at the follow example:
def xml =
'''<foo>
<bar>
<ID>Response1</ID>
<ReferenceID>stuff</ReferenceID>
<CreationDate>2016-05-01T11:34:56Z</CreationDate>
</bar>
</foo>
'''
def slurper = new XmlSlurper().parseText(xml)
println slurper.foo // prints nothing...
println slurper.bar.ID // prints Response1
Due to this maybe the easy way to get the node value is to use find
method, so in your DISPATCH script:
def req = new XmlSlurper().parseText(mockRequest.requestContent)
return req.'**'.find { it.name() == 'ID' }
Alternatively if you want to use XmlHolder
instead of XmlSlurper
as @Rao comments simply use a namespace on your XPath. Fortunately SOAPUI allows you to use *
as a wildcard for namespaces so correct ID
by //*:ID
:
def holder = new com.eviware.soapui.support.XmlHolder(mockRequest.requestContent )
return holder.getNodeValue("//*:ID").toString()
Hope it helps,
Upvotes: 1