soapUI: Scripting Objects - MessageExchange Interface

This is a continuation of a series of posts attempting to review some of the objects (and methods) you're likely to encounter when you start scripting in soapUI.  To see the full soapUI javadoc, click here.

Common MessageExchange Methods

SoapUI's MessageExchange interface defines methods for retrieving information related to test requests, including request and response data.  Within a Groovy Script assertion, a built-in variable (messageExchange) provides access to an object of a class that implements the MessageExchange interface.  Several step result classes (including the WsdlTestRequestStepResult class) also implement the MessageExchange interfaces.  Some of the common methods of the interface (available to objects of all classes that implement the interface) include:

getEndpoint() : returns the target endpoint as a String
getRequestContent() : returns the content of the test request
getRequestContentAsXml() : returns the content of the test request (as XML)
getResponseContent() : returns the content of the response to the test request
getResponseContentAsXml() : returns the content of the response to the test request (as XML)
getTimeStamp() : returns the request time stamp as a long number (see below for an example of converting from long to a standard time format)
getTimeTaken() : returns the amount of time taken to process the request (in milliseconds)
- getRequestHeaders() : returns request headers as a StringToStringsMap object
- getResponseHeaders() : returns response headers as a StringToStringsMap object

A map (as implemented in the StringToStringsMap objects returned by the getRequestHeader() and getResponseHeader() methods) is similar to a list in that it's capable of storing multiple data items.  Map "entries" are stored as data pairs: a key and a value.  So while an item in a list is retrieved using its location in the list order (its index), a data value in a map is retrieved using its associated key.  If that explanation still unclear, see the example below using the getResponseHeaders() method, which illustrates retrieving data from a StringToStringsMap object.

Examples Using MessageExchange Methods

These coding examples use the PeriodicTable service I've used in many other posts and reflect a test request call made to its GetAtomicNumber operation.  The first example illustrates some of the methods illustrated above as they might be used in a Groovy Script assertion, called using the script assertion's built-in messageExchange variable:

log.info("MessageExchange class = " + messageExchange.getClass().toString())
log.info("Endpoint = " + messageExchange.getEndpoint())
log.info("Request Content = " + messageExchange.getRequestContent())
log.info("Response Content Xml = " + messageExchange.getResponseContentAsXml())
log.info("Time Taken = " + messageExchange.getTimeTaken() + "ms")
//Using a map: get the map and assign it to variable respHeadersMap
respHeadersMap = messageExchange.getResponseHeaders()
//Retrieve the map's keys and assign them to a list, respHeadersKeys
respHeadersKeys = respHeadersMap.getKeys()
log.info("Response Headers info:")
/*Use a for loop to step through each key in the map and
  use the map's get function to get each key's corresponding
  map data.  The get function takes two arguments: the first
  is the key for which you'd like the corresponding data, the 
  second is a default value if the key can not be found in the map.*/
for(key in respHeadersKeys){
 log.info("   " + key + ": " + respHeadersMap.get(key,"NONE"))
}
//To clarify, here's the Date header retrieved again:
log.info("   Date (again): " + respHeadersMap.get("Date","NONE"))
/*And here's a call to retrieve from a key that doesn't exist;
  the default value of "NONE" is used instead.*/
log.info("   Bogus: " + respHeadersMap.get("Bogus","NONE"))

The script log output looks something like this:



As I mentioned above, you're not restricted to using these methods with the messageExchange variable in script assertions-- you can use them with any class that implements the MessageExchange interface.  Here's another example taken from a Groovy Script step that uses a WsdlTestRequestStep object to invoke some of the methods:

//Get the result for the first step, a test request step
//Assign it to variable tReqResult
tReqResult = testRunner.getResults()[0]
log.info("tReqResult Class = " + tReqResult.getClass().toString())
log.info("Endpoint = " + tReqResult.getEndpoint())
log.info("Request Content Xml = " + tReqResult.getRequestContentAsXml())
log.info("Response Content = " + tReqResult.getResponseContent())
//Assign the value returned by getTimestamp() to variable timeStampAsLong
timeStampAsLong = tReqResult.getTimestamp()
//Use long value stored in timeStampAsLong to create a new Date object, assigned
//to variable timeStampAsTime; this can be used to get a "friendly" date format.
timeStampAsTime = new Date(timeStampAsLong)
log.info("Time Stamp = " + timeStampAsTime)

And the expected output:

3 comments:

  1. I am using SoapUIPro 4.6.4 when I copied 'log.info("Endpoint = " + tReqResult.getEndpoint())' in my test case groovy script and ran it. It gave error message not recognizing message exchange. I am pretty new to groovy and soapUI.

    ReplyDelete
    Replies
    1. Could you post some more of your script? At first glance, it sounds like that particular error message may not be the result of that particular line-- there doesn't seem to be any reference to a message exchange in there, so it seems odd that soapUI would tell you it's not recognizing one. Contact me via the Contact form and post it there if you'd rather not post it here.

      Delete
    2. Its working now. I was copying the script in regular groovy script window where as I am supposed to copy it to Assertion window.

      Delete

Please be respectful of others (myself included!) when posting comments. Unfortunately, I may not be able to address (or even read) all comments immediately, and I reserve the right to remove comments periodically to keep clutter to a minimum ("clean" posts that aren't disrespectful or off-topic should stay on the site for at least 30 days to give others a chance to read them). If you're looking for a solution to a particular issue, you're free to post your question here, but you may have better luck posting your question on the main forum belonging to your tool's home site (links to these are available on the navigation bar on the right).