Hi ReadyAPI Community!
I bring more challenges 🙂
If you have the "Complete error logs" option enabled, the printable report includes the Raw request and Raw response for the failed test steps. But, you may need to get this info for passed test steps, as well.
Currently, there is no built-in option in ReadyAPI to show raw request and response of passed test steps in the printable (Jasper) report.
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.
Difficulty:
Here is an example:
Check out the TechCorner Leaderboard here.
Good luck😊
Solved! Go to Solution.
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's what I came up with:
// Get the test step's results, PASS or FAIL
def testStatus = testStepResult.getStatus();
// Get a reference to the test step
def testStep = testStepResult.getTestStep();
// Get the test type, looking for soap or rest, not sure if needed for challenge
def tsType = testStep.config.type.toString()
// Skip steps that are not soap or rest
if (tsType.equals("soaprequest") || tsType.equals("restrequest"))
{
// Skip tests that are not PASS
if (testStatus.toString().equals("PASS"))
{
// Build full URL
def endPoint = testStep.getHttpRequest().getEndpoint();
def URI = testStepResult.getTestStep().getHttpRequest().getPath()
def path = "$endPoint/$URI";
def response = testStep.getPropertyValue("RawResponse");
// Add the URL and raw response to the test step result messages, which flows to the test report.
testStepResult.addMessage("Request URL: $path");
testStepResult.addMessage("Raw Response: $response");
}
}
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: -
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
@nmrao Yes, you are correct!
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's what I came up with:
// Get the test step's results, PASS or FAIL
def testStatus = testStepResult.getStatus();
// Get a reference to the test step
def testStep = testStepResult.getTestStep();
// Get the test type, looking for soap or rest, not sure if needed for challenge
def tsType = testStep.config.type.toString()
// Skip steps that are not soap or rest
if (tsType.equals("soaprequest") || tsType.equals("restrequest"))
{
// Skip tests that are not PASS
if (testStatus.toString().equals("PASS"))
{
// Build full URL
def endPoint = testStep.getHttpRequest().getEndpoint();
def URI = testStepResult.getTestStep().getHttpRequest().getPath()
def path = "$endPoint/$URI";
def response = testStep.getPropertyValue("RawResponse");
// Add the URL and raw response to the test step result messages, which flows to the test report.
testStepResult.addMessage("Request URL: $path");
testStepResult.addMessage("Raw Response: $response");
}
}
@msiadak works like a charm!
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: -
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
@nmrao Awesome job! Thank you!
User | Count |
---|---|
6 | |
5 | |
4 | |
2 | |
1 |
Subject | Author | Latest Post |
---|---|---|