Common MessageExchange Methods
- 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
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:
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.
ReplyDeleteCould 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.
DeleteIts working now. I was copying the script in regular groovy script window where as I am supposed to copy it to Assertion window.
Delete