cancel
Showing results for 
Search instead for 
Did you mean: 

Compare Json Response from the data source with the actual response in event

SOLVED
Highlighted
Occasional Contributor

Compare Json Response from the data source with the actual response in event

In my correct setup I have scenarios where I need to compare the JSON response from the data source to actual response, this can be done in a separate groovy but because of implementation structure we want to compare it in RequestFilter.afterRequest even.

The problem we are running into is in event we are not able to fetch expected response from datasource, can someone please have a look into this and suggest how this can be achived.  

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Manager

Re: Compare Json Response from the data source with the actual response in event

I see that the Support Team provided several suggestions on for this case, let me copy it here so that anyone experiencing the same thing could have this reply as a reference:

 

>>

1. You can use property expansions in events, for example:
def value = context.expand( '${DataSource#ExpectedResponse}' )
2. You can use a script assertion to compare JSON values instead of events: https://support.smartbear.com/readyapi/docs/testing/assertions/reference/script.html

Also, this article might be helpful for you: https://smartbear-cc.force.com/portal/KbArticleViewer?name=How-Do-I-Compare-Two-Dynamic-JSON-Respons...

<<


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

Highlighted
Occasional Contributor

Re: Compare Json Response from the data source with the actual response in event

Thanks this solved my problem

View solution in original post

14 REPLIES 14
Highlighted
Community Hero

Re: Compare Json Response from the data source with the actual response in event

Hey @vpnkumar,

No ones answered this yet and ive only ever used events intermittently but i'll try and help out.

You mention " because of implementation structure" you want to do the compare using an event handler rather than individual groovy scripts. Can you clarify what you mean by "implementation structure"? Ive no idea what that even means.

You included the tag SOAP, but mention JSON in your post. Is it SOAP or is it JSON? SOAP can only be XML, so just want to be clear about what youre after...especially if youre talking about comparing the xml payload within a soap container.

I understand how having one event handler script rather than a script in each testcase makes things a lot easier to setup/create and maintain but im struggling to think how this would work. If the groovy is doing a comparison, i take it youd need >=1 assertions in your script. How would having assertions in an event handler script work? What i mean by this is, if this comparison assertion was in an event that fires after the request and was the only assertion in the test, would the tests status get updated correctly? Another thing to think about, wouldnt having assertions in the event skew the reporting?

You mention that youre struggling to fetch the expected reaponse from the Datasource, but why? Why arent you able to fetch the response from the Datasource in the event groovy? The same objects are available in the event handler groovy as in the normal groovy steps, so im struggling to understand why this ismt working.

It would probably help tremendously if you published the event handler groovy you have already got, as the groovy experts on here (@nmrao, @HimanshuTayal, @msiadak) will more likely be able to help if they see what groovy you have tried already.

Nice one,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Occasional Contributor

Re: Compare Json Response from the data source with the actual response in event

Thanks Rich please find more details below and in case if I am not clear please let me know I will try to explain more

From Implementation structure, I mean at current for my Json request things like updating it with dynamic values, removing empty (key, values, array of empty objects) all are handled with events.

For response verification I already have a expected response in the data source there are few dynamics id that’s need to be removed before comparing actual and expected Json response, this is already implemented in a groovy script but instead of adding this groovy script after ever Rest request for me it looks it will be better if this can be handled in events, at current I can get the current response in the events but when I am using same approach  as listed below but It didn’t work in event because of unavailability of testrunner in events.

 

def ds = testRunner.testCase.testSteps['DataSource']

def ExpJson = ds.getPropertyValue("ExpectedResponse"); 

 

for Assertion point I was thinking of one simple status code assertion in the rest request and other will be JSON response comparison in event, it will use the existing groovy function where input parameter is expected json from datsource and actual one from REST response

 

Below is the sample code I had used in event for request handling.

 

if(request.getName()=="Request1" || request.getName()=="Request2" || request.getName()=="Request3" ){
def requestContent = (String) context.getProperty(BaseHttpRequestTransport.REQUEST_CONTENT)
log.info("Request Body BEFORE " + requestContent)
def finalrequest = ModifiedRequest(requestContent);
log.info("final request is " + finalrequest);
log.info("Request Body AFTER " + finalrequest)
context.setProperty(BaseHttpRequestTransport.REQUEST_CONTENT, finalrequest);
def httpMethod = context.getProperty(BaseHttpRequestTransport.HTTP_METHOD)
httpMethod.setEntity(new ByteArrayEntity(finalrequest.getBytes(Charset.defaultCharset())))

}

 

 
Highlighted
Community Hero

Re: Compare Json Response from the data source with the actual response in event

@vpnkumar,

As far as i was aware, testRunner IS available within events....it even says its available in the title of thr editable event field????

Ta

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Occasional Contributor

Re: Compare Json Response from the data source with the actual response in event

Rich  to re-verify  if testRunner IS available within events or not I went to the tool and find testRunner is missing, please refer below screenshot of events. In case if I am looking into some wrong place please do update me.

 

vpnkumar_0-1597411931729.png

 

Highlighted
Community Hero

Re: Compare Json Response from the data source with the actual response in event

Hey @vpnkumar,


No. Youre definitely looking im the right place.
What version of ReadyAPI! are you using? Im running 3.3.0.
The screenshot you supplied is too small for me to see (im on my phone at the moment), but the text above the editable field on the far right actually includes testRunner as one of the available options.

If testRunner isnt available, then youll have to use messageExchange object instead im guessing...like you have to use instead of testRunner in script assertions.

I'll be in front of my laptop tonight so i'll try converting your groovy to use messageExchange instead of testRunner when i get the chance. Unless you wanna do it. Its straightforward.

Nice one

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Occasional Contributor

Re: Compare Json Response from the data source with the actual response in event

Thanks please refer this screenshot, I will also start digging into messageExchange  and see how that will help.

 

vpnkumar_0-1597426394349.png

 

 

Highlighted
Occasional Contributor

Re: Compare Json Response from the data source with the actual response in event

I Ran into this example  but is there something I need to import before using it, because I am always running into issue.

 

// Get a test case property
def testCaseProperty = messageExchange.modelItem.testStep.testCase.getPropertyValue( "MyProp" ) , 

 

Highlighted
Community Hero

Re: Compare Json Response from the data source with the actual response in event

Hey @vpnkumar,

Yep! That's the one i was talking about.

Nah, you don't need any additional import declarations to use the messageExchange object if it is available im the events scripting interface.
Im still curious. Whst versiom of ReadyAPI! are you running, cos as i said before...my ReadyAPI! says testRunner is available in event scripting.

Ta

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Manager

Re: Compare Json Response from the data source with the actual response in event

Thank you for your help Richie.

 

@vpnkumar Please make sure you are running the latest version of ReadyAPI as it looks like this works in v.3.3.0 and above.

 

In case this is still an issue for you even then, please report this to our Customer Care Team and they will set up an investigation. 


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
New Here?
Join us and watch the welcome video:
Announcements