I am new to SoapUI and have just configured a very simple MockService. Is it possible to manipulate the response so that for a particular request the response's elements are dynamically built up?
Scenario 1:
Request:
<record>
<identifier>ID1</identifier>
</record>
Response:
<response>
<child1>child 1</child1>
</response>
Scenario 2:
Request:
<record>
<identifier>ID2</identifier>
</record>
Response:
<response>
<child2>child 2</child2>
</response>
It is for a simple test and I don't need it to do any more than the above. I am currently doing the following that yields the results I want but since I am completely new to this I am sure there are better alternatives:
Response:
<response>
${dynElement}
</response>
Groovy script:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def reqRef = String.valueOf(holder.getNodeValue("//identifier"))
def child1Text = "<child1>child 1</child1>"
def child2Text = "<child2>开发者_StackOverflow中文版;child 2</child2>"
if (reqRef == "ID1") {
context.setProperty("dynElement", child1Text)
} else if (reqRef == "ID2") {
context.setProperty("dynElement", child2Text)
}
Instead of xpath, you could also use XmlSlurper.
def req = new XmlSlurper().parseText(mockRequest.requestContent)
def reqRef = req.record.identifier
Shamelessly ripped of from this question's answers, please don't hurt me.
I do it with "canned responses" and xpath queries. To do this, you'll set up a series of dispatch handlers in the mockservice to match requsts with respones. Suppose you've got Request1, Request2, Response1, Response2. Use the "Query Match" dispatch method, to match an XPATH expression. On that match, return the desired response. i.e. if you find ID1 in the xpath, return the canned Response1.
Also, the PRO version does a great job with the XPATH, so you don't have to hand code it. i.e. it can look at a response click on the thing that you want to trigger on (in your case, ID1) and it builds the XPATH expression for you. I requested a PRO license, just based on that. Currently awaiting budgeting...
IMO, much easier to get started than figuring out groovy.
Chris
精彩评论