Below are the scripts used to solve the problem described above based on my specific needs.
Groovy script used to remove specific XML tags based on the data presented in the excel sheet:
import com.eviware.soapui.support.xml.XmlUtils
def request = context.expand( '${--TEST-STEP-NAME--#Request}' )
def parsedXml = new XmlParser().parseText(request)
def buffer = new StringWriter()
new XmlNodePrinter(new PrintWriter(buffer)).print(parsedXml)
def filteredRequest = buffer.toString()
filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
filteredRequest = filteredRequest.replaceAll( "<(.+)>--REMOVE--<(.+)>", "")
filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
context.requestContent = filteredRequest
XML tags that have the string
“—REMOVE--” will be deleted from the original request. The
“—REMOVE--” string and all other necessary data comes from the excel sheet and is used to create a test request, then this script is called by the soapui event RequestFilter.filterRequest to filter the request contents.
Groovy script used to remove XML nodes based on the data presented in the excel sheet:import com.eviware.soapui.support.xml.XmlUtils
def nodeRemoveFlag = context.expand( '${DataSource#removeXmlNode}' )
if ( nodeRemoveFlag == 'Yes'){
def request = context.expand( '${--TEST-STEP-NAME--#Request}' )
def parsedXml = new XmlParser().parseText(request)
def nodeToRemove = parsedXml.children()
def nodeFind = parsedXml.find { it.text() == '--XML-NODE-TO-REMOVE--' }
nodeToRemove.remove(nodeFind)
def buffer = new StringWriter()
new XmlNodePrinter(new PrintWriter(buffer)).print(parsedXml)
def filteredRequest = buffer.toString()
filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
context.requestContent = filteredRequest
}
If the remove flag is set to
“Yes” in the excel sheet it's possible to use this script to remove a specific xml node. In this case the node that can be removed it’s hard coded on the script, but if needed the script can easily be adapted to remove different xml nodes based on the data driven approach.
Regards
Tiago Neto Rodrigues