Access Request XML Content

Applies to ReadyAPI 3.54, last modified on July 23, 2024

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 use these scripts:

Examples

The following examples modify XML content of a test step with the Add Pet name. We assume the test step has this content:

Payload of the Add pet test step

<Pet>
    <id></id>
    <Category>
        <id></id>
        <name></name>
    </Category>
    <name></name>
    <photoUrls></photoUrls>
    <tags>
        <Tag>
            <id></id>
            <name></name>
        </Tag>
    </tags>
    <status></status>
</Pet>

Change a Request XML

This example modifies the name and status of a pet.

Groovy

// Create groovyUtils and XmlHolder for the request of the Add pet test step
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( 'Add pet#Request' )

// Change the node values in the imported XML
holder["/Pet/name"] = "Groovy"
holder["//status"] = "sold"

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

If you use this script as the Setup Script of a test case, it modifies the XML before each run of the test case:

Modifying XML from a Groovy script

Click the image to enlarge it.

Resulting XML

<Pet>
    <id></id>
    <Category>
        <id></id>
        <name></name>
    </Category>
    <name>Groovy</name>
    <photoUrls></photoUrls>
    <tags>
        <Tag>
            <id></id>
            <name></name>
        </Tag>
    </tags>
    <status>sold</status>
</Pet>

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( 'Add pet#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