You can work with XML in your scripts. ReadyAPI provides custom methods used to get XML nodes and work with them. For information about advanced parsing of XML, see XML Parsing in ReadyAPI Test.
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.
Examples
Getting node values
To get XML node values, you use the groovyUtils
and XmlHolder
objects. Here is how you can get values from the Test Request – login request in the SOAP Sample project:
Groovy
// Create the groovyUtils and XmlHolder objects to hold the response XML
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( 'Test Request - login#Response' )
// Loop item nodes in the response message
for( item in holder.getNodeValues( "//faultcode" ))
log.info "Item : [$item]"
// If the desired content is namespace qualified, you need to define the namespace first
// Define the namespace
holder.namespaces['ns'] = 'http://www.soapui.org/sample/'
// Loop item nodes in the response message
for( item in holder.getNodeValues( "//ns:loginFault" ))
log.info "Item : [$item]"
Counting Nodes
When you already have an XML, you can use normal operations to work with it. For example, to count the number of item
elements in the XML, use the following code:
Groovy
// Create the groovyUtils and XmlHolder objects to hold the response XML
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "Test Request - search#Response" )
// Count the number of item
elements in the XML
def numberOfItemsInResponse = holder["count(//Body/searchResponse/searchResponseContent/item)"]
log.info numberOfItemsInResponse