Looking for the Groovy script to compare 2 JSon responses
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Looking for the Groovy script to compare 2 JSon responses
Hello All,
I have a REST request. I want to run the request and save the response as a text doc. I want to run it again after couple of days and see if both the responses(Old and New) are same or not(assert).
Can some one help me to understand, and how to achieve that with the Groovy script. Thanks in-advance!
Regards,
Sudhakar
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi TSRAO,
Is there a reason why you need to export the response to an external file such as a text document?
If you simply want to output the response of a request to a file, you can use a data sink to do this. This would be an easier alternative to writing a script. Here's an example of using a data sink to write the response to a file.
But if the goal here is to compare the results from day to another then you could simply accomplish this by using an assertion. The Smart Assertion in particular would be a good option to consider since it can apply conditional checks (assertions) to any part of the metadata and response data.
The Smart Assertion will automatically fill in the current response from a request as the valid value. The default condition that it checks is 'Equal' so there wouldn't be much setup required to configure the assertion.
An example of the Smart Assertion interface is below:
If you use the Smart Assertion expecting a certain set of data, and then run the request at a later date, the assertion will fail if anything changes.
Below is an assertion for weather data. I sent a request to create the assertion, then sent the request again a few minutes later. The temperature values changed within that time period, so the assertion fails and identifies all the values that are not matching.
You can also utilize the comparison tool as well to see if any changes are present between two different test runs.
I'll add in links to the Smart Assertion, Compare tool, and Data Sinks below.
Smart Assertion: https://support.smartbear.com/readyapi/docs/testing/assertions/reference/property/smart-assertion.ht...
Compare Tool: https://support.smartbear.com/readyapi/docs/functional/results/compare.html
Data Sink: https://support.smartbear.com/readyapi/docs/functional/steps/data-sink.html
If you still want to do a script to output the response I would do this in a groovy test step or in a teardown/setup script. You can use Get Data to pass in the response of a request into your file.write section of your script. A good example of reading and writing files in groovy is below.
https://code-maven.com/groovy-files
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much SarahS! and sorry for the late reply.
Does the smart Assertion work for the config files where the new additions/deletions happen frequently?
Today I have a response with 10 fields in the response and tomorrow if I run it again, I may see there will be 12 or 15 fields (few more fields got added). So, in that case will that smart assertion works? And what if I want to see what are all the fields got added from the previous run/response?
Thanks again!
