Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
4 years ago
Solved

[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 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:

 

 

 

πŸ’‘Hint: https://community.smartbear.com/t5/SoapUI-Pro/TechCorner-Challenge-9-A-script-to-include-custom-details-to-the/m-p/205242/thread-id/46875

 

Check out the TechCorner Leaderboard here.

Good luck😊

  • 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: -

    • 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

     

7 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    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

     

      • nmrao's avatar
        nmrao
        Champion Level 3
        Thank you Sonya.

        Can someone tell why it is happening so? I am referring the note.
  • nmrao's avatar
    nmrao
    Champion Level 3
    Is this specific for only Pro edition?
    When test execution is done with testrunner, junit as reporting would generate the responses saved in a file.
    But the question is asking for PDF. Correct?
      • groovyguy's avatar
        groovyguy
        Champion Level 1

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