Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
5 years ago

[TechCorner Challenge #9] A script to include custom details to the report

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 ...
  • groovyguy's avatar
    groovyguy
    5 years ago

    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");
    	}
    }

     

  • nmrao's avatar
    5 years ago

    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