Ask a Question

Re: Compare two REST API responses for every attribute (node

SOLVED
rana1
New Contributor

Re: Compare two REST API responses for every attribute (node

Hi

How can I compare two json responses in ready api i.e.,I have a url I have requested a response1 and received response1. Now I have created another step in the same test case with the same parameters and requested response2 and received the response2.

NOW

I want to I want to compare RESPONSE1 with RESPONSE2.

Is it possible..?? If yes please reply me asap.

Thanks

Rana

1 ACCEPTED SOLUTION

Accepted Solutions
New2API
Frequent Contributor

Re: Compare two REST API responses for every attribute (node

Hello, It is possible to compare two JSON responses.

 

Simplest approach is to parse the response using JsonSlurper and assert.

 

import groovy.json.JsonSlurper

 

def Response1 = context.expand( '${#TestStepName#Response}' ).toString()
      JSON1 = new JsonSlurper().parseText Response1

 

def Response2 = context.expand( '${#TestStepName#Response}' ).toString()
      JSON2 = new JsonSlurper().parseText Response2

 

assert JSON1 == JSON2

 

 

View solution in original post

9 REPLIES 9
rana1
New Contributor

How can I compare 2 JSON responses in readyapi pro 1.9.0

Hi I'm using Ready API Pro 1.9.0 version

How can I compare two json responses in ready api i.e.,I have a url I have requested a response1 and received response1. Now I have created another step in the same test case with the same parameters and requested response2 and received the response2.

NOW

I want to I want to compare RESPONSE1 with RESPONSE2.

Is it possible..?? If yes please reply me asap.

Thanks

Ranadheer

nmrao
Community Hero

Re: Compare two REST API responses for every attribute (node

 

Now moved this messaged to right forum.

 

Please add more details of your data and use case.



Regards,
Rao.
rana1
New Contributor

Re: Compare two REST API responses for every attribute (node

I want to use My 1st response as a base line and I want to compare my 2nd response to my 1st response and check whether I'm getting the same response in both or not. Is it possible to check...???

nmrao
Community Hero

Re: Compare two REST API responses for every attribute (node

Yes, but you need to write code based on how one the data which you did not provide.

By the way, there is an enhancement requested, please consider voting / kudo it so that there may be chances to productize it based user demand. Please see below thread.

https://community.smartbear.com/t5/SoapUI-Feature-Requests/Compare-test-step/idi-p/134568


Regards,
Rao.
New2API
Frequent Contributor

Re: Compare two REST API responses for every attribute (node

Hello, It is possible to compare two JSON responses.

 

Simplest approach is to parse the response using JsonSlurper and assert.

 

import groovy.json.JsonSlurper

 

def Response1 = context.expand( '${#TestStepName#Response}' ).toString()
      JSON1 = new JsonSlurper().parseText Response1

 

def Response2 = context.expand( '${#TestStepName#Response}' ).toString()
      JSON2 = new JsonSlurper().parseText Response2

 

assert JSON1 == JSON2

 

 

View solution in original post

GauravGandhi
New Contributor

Re: Compare two REST API responses for every attribute (node

Hi,

 

This is only giving whether the Assert passes or fails, could you suggest how to find the difference that which element was not present in the comparison.

 

Regards,

Garv

bagochips
Contributor

Re: Compare two REST API responses for every attribute (node

I had this issue and this is what I could figure out to do.

 

import groovy.json.JsonSlurper
import java.text.SimpleDateFormat
import org.apache.commons.lang.StringUtils

filePath = context.testCase.testSuite.getPropertyValue("failedLog").toString()
f = new File(filePath)


def account = context.testCase.getPropertyValue("account")

def response1 = testRunner.testCase.getTestStepByName("Request1").getPropertyValue("response")
def slurperResponse1 = new JsonSlurper().parseText(response1)
def response2 = testRunner.testCase.getTestStepByName("Request2").getPropertyValue("response")
def slurperResponse2 = new JsonSlurper().parseText(response2)
def account = context.testCase.getPropertyValue("account")

 

status_code_1 = (slurperResponse1.status.code)
account_status_code_2 = (slurperResponse2.status.code)
if (status_code_1 <=> status_code_2) {
f.append '\n' + ("Account number " + account +" at " + "status_code" + " failed")
}

 

You would then have to map each line to pull the data from both requests, compare and apply some logic to report issues. This is fine if you have only a few fields to validate but if your response has 100 or more lines you might want to do something like this.

Have a step that writes the Raw Response into individual xml files then have a nother that will read each file line by line while comparing the lines.

 

Step 1

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectPath = groovyUtils.projectPath
def account = testRunner.testCase.getPropertyValue( "account" ) // pull account number to add text to the name of the file if running data driven test

def response1 = context.expand( '${Account - Request 1#RawResponse}' )
def response2 = context.expand( '${Account - Request 2#RawResponse}' )

 

//assert response == response2

//write payload to file
FP = filePath=projectPath+"\\Responses\\" + account + "_" + "response1.xml"
f = new File(FP)
f.append(response1)

 

//write payload to file
FP = filePath=projectPath+"\\Responses\\" + account + "_" + "response2.xml"
f = new File(FP)
f.append(response2)


Step 2

import java.io.FileReader;
import java.io.LineNumberReader;
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectPath = groovyUtils.projectPath
def account = testRunner.testCase.getPropertyValue( "account" )

LineNumberReader r1 = new LineNumberReader(new FileReader(projectPath + "\\Responses\\" + account + "_" + "response1.xml"));
LineNumberReader r2 = new LineNumberReader(new FileReader(projectPath + "\\Responses\\" + account + "_" + "response2.xml"));

String line1 = null;
String line2 = null;

while ((line1 = r1.readLine()) != null) {
line2 = r2.readLine()
if (line1 != line2){
//log.info line1 + " - " + line2
FP = filePath=projectPath+"\\Discrepancies\\" + account + "_" + "discrepancy.txt"
f = new File(FP)
f.append(line1 + " <===> " + line2)
f.append '\n'
}

}
r1.close();
r2.close();

nmrao
Community Hero

Re: Compare two REST API responses for every attribute (node

@GauravGandhi, it depends on the data; accordingly the script needs to changed for comparison to work. Provide your sample data.


Regards,
Rao.
nmrao
Community Hero

Re: Compare two REST API responses for every attribute (node

By the way, open a new topic instead reviving a solved question.


Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: