Your Groovy scripts have access to the XML content of the request and can modify it. You can change the request text before, during the test run, and right before the request is sent.
Where To Use
There are several ways to run these scripts:
-
As a Groovy Script test step.
-
As a setup or teardown script on the test case level in functional tests.
-
As an event in the project.
Examples
Change a Request XML
This example sets the password inside a request SOAP message:
Groovy
// Create groovyUtils and XmlHolder for the request of the Sample Request test step.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( 'Test Request - login#Request' )
// Change the node values in the imported XML.
holder["//username"] = "3216431654"
holder["//password"] = "Loginn1123"
// Update the request and write the updated request back to the test step.
holder.updateProperty()
context.requestContent = holder.xml
Remove Empty XML Elements From the Request
You can already remove empty XML nodes by using the Remove Empty Content option. However, it only removes empty end nodes, and can leave empty parent nodes. To remove those, use the following script:
Groovy
// Create groovyUtils and XmlHolder for the request of the Sample Request test step.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( 'Test Request - login#Request' )
// Find nodes that only consist of whitespaces.
for( item in holder.getDomNodes( "//*[normalize-space(.) = '' and count(*) = 0]" )){
item.removeXobj()
}
// Update the request and write the updated request back to the test step.
holder.updateProperty()
context.requestContent = holder.xml