Access Request XML Content

Applies to ReadyAPI 3.9, last modified on July 16, 2021

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:

Examples

The following examples modify the XML content of the Test Request - login test step:

Original XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://www.soapui.org/sample/">
 <soapenv:Header/>
 <soapenv:Body>
  <sam:login>
   <username>Login</username>
   <password>ChangeMe</password>
  </sam:login>
 </soapenv:Body>
</soapenv:Envelope>

Change a Request XML

This example sets the login and password in the XML content of the SOAP Request test step:

Groovy

// Create groovyUtils and XmlHolder for the request of the Test Request - login 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"] = "MyUsername"
holder["//password"] = "p@ssw0rd"

// Update the request and write the updated request back to the test step
holder.updateProperty()
context.requestContent = holder.xml

After running this script, you will have the following XML content in the request test step:

Resulting XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://www.soapui.org/sample/">
 <soapenv:Header/>
 <soapenv:Body>
  <sam:login>
   <username>MyUsername</username>
   <password>p@ssw0rd</password>
  </sam:login>
 </soapenv:Body>
</soapenv:Envelope>

Remove Empty XML Elements From the Request

You can already remove empty XML nodes by using the Remove Empty Content property. 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

See Also

Request Editor Interface
Scripting

Highlight search results