Request and response handling using Scripts:
soapUI provides us with a few important APIs to use with request and response messages:
1. com.eviware.soapui.support.GroovyUtils: The API documentation of the GroovyUtilsclass (http://www.soapui.org/apidocs/com/eviware/soapui/support/GroovyUtils.html) provides us with all the necessary information to use this API.
2. com.eviware.soapui.support.XmlHolder: This is a very useful API to act upon XML request and response messages. More details about the API can be found at the official API documentation (http://www.soapui.org/apidocs/com/eviware/soapui/support/XmlHolder.html)
3. com.eviware.soapui.model.iface.MessageExchange: This interface represents an exchange of request and response messages using various API methods. For more details, visit http://www.soapui.org/apidocs/com/eviware/soapui/model/iface/MessageExchange.html.
Let's find out the basic usage of each of these classes using our sample project.
1. Add a new Groovy Script TestStep under the getRoomDetails TestCase of RoomManagementService TestSuite.
2. Add the following script:
def xmlHolder = new com.eviware.soapui.support.XmlHolder(context, "getRoomDetails#Response")
log.info xmlHolder.getXml()
Here, we created a new XmlHolder object which makes use of WsdlTestStep context variable and response of getRoomDetails request through property expansion. Submit the getRoomDetails TestStep once so that we will have a valid response in context. Then, run the Groovy script which will execute the script against the last received response message. You will find the response SOAP message in the script log.
3. We can get hold of the response XML message using the GroovyUtils class as follows:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def xmlHolder = groovyUtils.getXmlHolder("getRoomDetails#Response")
log.info xmlHolder.getXml()
4. Similarly, the request message can also be accessed:
def xmlHolder = new com.eviware.soapui.support.XmlHolder(context, "getRoomDetails#Request")
Once you get hold of request and response messages, you can do various XML manipulations through the methods included in the XmlHolder class such as getDomNode(xpath) and getNodeValue(xpath).
Script assertion:
Script assertion is another type of assertion that can be used to validate the responses. The major advantage of using script assertion over the other assertions is you have much more control over the messages exchanged. Thus, you can validate the message content or headers using Groovy or JavaScript.
1. Open the getRoomDetails test request editor. Click on the Assertions tab at the bottom pane and select the Adds an assertion to this item option. Select Script Assertion and click on OK:

3. At the upper-right of the script editor, you will notice the message, "Script is invoked with log, context and messageExchange variables". Thus, you can use these variables to access request and response messages to do various content level validations.
4. The com.eviware.soapui.model.iface.MessageExchange interface represents an exchange of request and response message in a test run. Therefore, we can use the methods exposed by this interface such as getResponseContent() and getResponseHeaders() to access the request and response messages.
5. Add the following script in the script assertion editor and run the getRoomDetails TestCase:
import com.eviware.soapui.support.XmlHolder
def responseHolder = new XmlHolder(messageExchange.getResponseContentAsXml())
def requestHolder = new XmlHolder(messageExchange.getRequestContentAsXml())
assert responseHolder["//ns:roomNumber"] == requestHolder["//typ:roomNumber"]
We used two XmlHolder objects to hold request and response. Request and response messages were retrieved by calling the getReponseContentAsXml and getRequestContentAsXml methods of the messageExchange object.
This is a trivial example which we used to demonstrate the usage of script assertion in a soapUI project. By using the context and messageExchange variables, you can try out much advanced and comprehensive operations on request and response messages.




