Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add child elements dynamically to SOAP request in SOAPUI using groovy script

Tags:

groovy

soapui

I have a request which accepts 1..n items to add to basket. default request has one "item" element with some child elements, i want to add N no of items with child element i'm able to add Item parent element using creaeElementNS now i want to add child elements to "item" like following, can someone shed some light on this

<basket:item>
<basket:itemId>222</basket:itemId>
<basket:itemCode>DEF</basket:itemCode>
<basket:item>

Groovy script

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "createBasket#Request" )
def parentnode = holder.getDomNode( "//basket:createBasketRequest" )
def basketTotal=   holder.getDomNode( "//basket:itemsTotal" ) 
def itemsTag = requestDoc.createElementNS(parentnode.getNamespaceURI(), "item")
parentnode.insertBefore(itemsTag  , basketTotal)
holder.updateProperty()

Current Output

<basket:createBasketRequest>
<basket:item>
    <basket:itemId>111</basket:itemId>
    <basket:itemCode>ABC</basket:itemCode>
</basket:item>
<basket:item>
</basket:item>
    <basket:itemsTotal>500.00</basket:itemsTotal>
</basket:createBasketRequest>

Desired Output

<basket:createBasketRequest>
    <basket:item>
        <basket:itemId>111</basket:itemId>
        <basket:itemCode>ABC</basket:itemCode>
    </basket:item>
    <basket:item>
        <basket:itemId>222</basket:itemId>
        <basket:itemCode>DEF</basket:itemCode>
    <basket:item>
    <basket:itemsTotal>500.00</basket:itemsTotal>
</basket:createBasketRequest>

EDIT: I'm using soapUI and TestCase option, there is a GroovyScript step before add basket request step, groovy script prepares add basket request before actually sending request, for example if my testcase mentions 1 item it should request with default one item so i have no worries to add another Item elements but when my testcase says 2 items i have to create second Items elements(along with child elements) and prepare the add basket request using groovy script step before actually sending add basket request

like image 628
StaleElementException Avatar asked Aug 22 '13 13:08

StaleElementException


1 Answers

Not sure if it will work (I don't use SoapUI), but does this help?

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder      = groovyUtils.getXmlHolder( "createBasket#Request" )
def parentnode  = holder.getDomNode( "//basket:createBasketRequest" )
def basketTotal = holder.getDomNode( "//basket:itemsTotal" ) 

def nodeText    = '''<basket:item>
                    |    <basket:itemId>222</basket:itemId>
                    |    <basket:itemCode>DEF</basket:itemCode>
                    |</basket:item>'''.stripMargin()

def nodeHolder  = new com.eviware.soapui.support.XmlHolder( nodeText )
def nodeItem    = nodeHolder.getDomNode( "//basket:item" )
def importNode  = requestDoc.importNode( nodeItem, true )

parentnode.insertBefore( importNode, basketTotal )

holder.updateProperty()
like image 132
tim_yates Avatar answered Oct 08 '22 15:10

tim_yates