Task: Write a Groovy script for the "TestRunListener.afterStep" event that will post the raw request URL and the full raw response to the printable report.
This is a solution created for [TechCorner Challenge #9]
Here is what I could come up with.
This report includes: -
- Properties like failure report. This may help to understand if all the properties are correct
- URL (same as in Raw request tab) of the request includes path and query parameters if any
- Request and its headers (same as in Raw request tab) which is actually sent to application
- Covers all http transports, HTTP, SOAP and REST
- Considers both UNKNOWN and OK status'es
Also attaching the sample report for quick view with passed, failed test each so that it is easy to see the report similarity (of details). Each test contains requests of REST, HTTP with both GET and POST methods and SOAP.
Here is the script for the "TestRunListener.afterStep" event
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep
import static com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus.*
import static com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport.HTTP_METHOD
//Closure to get ExtendedHttpMethod object
def eHM = { context.getProperty(HTTP_METHOD) }
//Initialize report
def message = new StringBuilder()
//Add the test properties to report similar to failure case as this helps to understand easily if something is missing or incorrect
def addPropertiesToReport = {
message.with {
append('\n----------------- Properties -----------------\n')
append("Endpoint: ${context.endpoint}\n")
switch (context.currentStep) {
case WsdlTestRequestStep:
append("Encoding: ${context.currentStep.testRequest.encoding}\n")
break
default:
append("HTTP Version: ${eHM().protocolVersion}\n")
append("Method: ${eHM().method}\n")
append("StatusCode: ${testStepResult.getProperty('StatusCode')}\n")
append("URL: ${eHM().URL}\n")
break
}
}
}
def addPayLoadToReport = { subHead, content, additionalData = null ->
message.with {
append("\n-------------- $subHead --------------\n")
if(additionalData) {
append(eHM().requestLine).append('\n')
additionalData.each { append(it.name + ' : ' + it.value + '\n')}
}
if (content) { append('\n').append(content).append('\n') }
}
}
//Actual business logic
if (([WsdlTestRequestStep, RestTestRequestStep, HttpTestRequestStep].any{context.currentStep in it}) && (testStepResult.status in [UNKNOWN, OK])) {
addPropertiesToReport()
addPayLoadToReport('Request', eHM().entity?.content?.text, eHM().allHeaders)
addPayLoadToReport('Response', context.rawResponse)
testStepResult.addMessage(message.toString())
}
NOTE:
It would be simple to use context.rawRequest to show the request. However, the E-Commerce REST Sample project is showing { "sample" : 1} even for the GET call which is actually not shown in the Raw Request tab, but being shown in the report. So, had to use below in the above
eHM().entity?.content?.text