Using Groovy to insert new tag into request. Need help with insert point (XmlParser - FindAll)
Hi. I have a request that I want to change by inserting a new tag after an existing tag. I am using Groovy code. I have my code working except for one little thing - I can't find where to put the new tag.
Below is my code. For the findAll() is there an 'it' value that will tell me the index of the item that I am looking at? In my code I have 5 for my insert point. This inserts the new tag in after the 5th tag in the parent object. It works but the new tag is not always going to be inserted at the pint. I need to use a variable for the insert point. For some reason I am having a hard time finding this answer.
def root = new XmlParser( false, true ).parseText( holder )
fragmentToAdd = new XmlParser( false, true ).parseText( toAdd ) // <tag>value</tag>
root.'**'.findAll() {
if ( it.name().toString().contains( rootNode)) { //rootNode = An existing tag after which I want to insert the new tag
it.parent().children().add( 5 , fragmentToAdd ) //insert command
}
}
Thank you,
Angie
P.S.
I am using the professional version of soapUI.
Again, this isn't pretty.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = context.expand( '${SOAPRequest#Request}' )
def root = new XmlParser( false, true ).parseText( holder )fragmentToAdd = new XmlParser( false, true ).parseText( toAdd ) //Node to be added in the format <tag>Value</tag>
root.'**'.findAll() {
//The new node will be added after rootNode
if ( it.name().toString().contains( rootNode.substring(2,rootNode.length()) )) {
int nodeCount = 0
def insertPoint = it.parent().children().find {
nodeCount++// Find the node in the list of nodes
it.name().toString().contains( rootNode.substring(2,rootNode.length()))
}
it.parent().children().add( nodeCount , fragmentToAdd ) //Add new node at the specified point.
}
}
String outxml = groovy.xml.XmlUtil.serialize( root )request = testRunner.testCase.getTestStepByName( "SOAPRequest" )
request.getProperty("request").setValue( outxml )Angie