cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Community Manager

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

 

DifficultyStar_Gold.pngStar_gray.pngStar_gray.png


Here is an example:test-step-results.png

 

 

 

💡Hint: https://community.smartbear.com/t5/SoapUI-Pro/TechCorner-Challenge-9-A-script-to-include-custom-deta...

 

Check out the TechCorner Leaderboard here.

Good luck😊


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Tags (1)
2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Hero

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

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Tags (1)
Highlighted
Community Hero

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

 



Regards,
Rao.

View solution in original post

Tags (1)
7 REPLIES 7
Highlighted
Community Hero

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?


Regards,
Rao.
Highlighted
Community Manager

@nmrao Yes, you are correct! 


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Community Hero

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

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Tags (1)
Highlighted
Community Manager

@msiadak works like a charm!


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Community Hero

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

 



Regards,
Rao.

View solution in original post

Tags (1)
Highlighted
Community Manager

@nmrao Awesome job! Thank you!


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Community Hero

Thank you Sonya.

Can someone tell why it is happening so? I am referring the note.


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get a full Raw JMS request

Participate!

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors