Forum Discussion

richard_mccarth's avatar
richard_mccarth
Contributor
16 years ago

Samples showing use of Xpath and Scripts in Dispatch for Response Selection

Hope this is'nt to dumb a question but I am new to SOAP UI and I am trying to explore the use of XPath and Scripts in the dispatch selection for a mock operation.

I cant seem to find any examples of either of these in the documentation, does anyone know if samples exist or have a script or Xpath that selects a response based upon the request data ?

5 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    sorry for the lack of documentation on this. There have been a couple of posts on this both here and on the sourceforge forums (http://sourceforge.net/forum/forum.php?forum_id=494205), please search around to find examples. Also, the documentation at http://www.soapui.org/userguide/mock/op ... ispatching and the included sample project has some code for this..

    If this doesn't help, please ask more specific questions and I'll try to help you forward!

    regards,

    /Ole
    eviware.com
  • Hi Ole,

    Thanks for your reply. I dug around and managed to figure out the basic formats for XPath and script as shown below.

    XPath
    --------

    Return the data of the FromCurrency element in the 'web' namespace in the request, this needs to match a response name configured for the request.

    e.g. if the from currency is USD then you need a response called USD

    declare namespace web='http://www.webserviceX.NET/';
    //web:FromCurrency/text()

    Note where the return does not match a response name then you get the default response.

    Script
    -------

    Using the same idea as above but with a script instead

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils (context)
    def holder = groovyUtils.getXmlHolder ( mockRequest.requestContent )
    def fromCcy = holder.getNodeValue("//web:FromCurrency/text()")
    return fromCcy

    Note it appears that where the return does not match a response name then you do not get the default response.
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    thanks! I've fixed the issue where the default response it not returned in case of a missing name returned by a dispatch script..

    We really want to improve on the xpath version, which should basically allow you to select a target xpath expression and then configure which response to return based on the result (and not require the result to contain the name of the mockresponse)..

    any other ideas for improvements are always welcome!

    regards,

    /Ole
    eviware.com
  • Hi Ole,

    You are welcome. I was'nt sure if I had it right or not about the default response. The change to the XPath logic would be great as its a bit limited as it is,

    Richard
  • I found using groovy's native xml processing abilities much more faster and flexible in whatever you want to achieve while parsing response and generating request from the response. It is also easier than using xmlHolder. I had difficulties using xmlHolder where I need to pull node from response and add to next request. I simply could not do it. Plus add more confusion with namespaces for response being inconsistent with those for the request that you want to create. Using groovy you can totally define namespaces for generated request and even for each gpathresult

    hope this helps

    Sachin