cancel
Showing results for 
Search instead for 
Did you mean: 

I want the data/ values comparison of a xml-file on my system with xml response of a rest API call?

Highlighted
Occasional Contributor

I want the data/ values comparison of a xml-file on my system with xml response of a rest API call?

In Ready API, I want to perform data/ values comparison of a xml-file on my system with xml response of a rest API call? Can someone help or knows how to resolve the issue step by step?

 

Thanks.

4 REPLIES 4
Highlighted
Community Hero

It depends on what data of xml that needs to be compared.

There are plenty of examples you can find on the net as xmlunit for entire xml comparison and if only certain node values to be compared there can be different ways.

If you can share some more specifics, that would help to understand the problem better.
At the moment question is very generic.


Regards,
Rao.
Highlighted
Occasional Contributor

Hi Rao-

 

Thanks for your response, appreciated. Here is the detail which may help you to understand.

I’m trying to compare the whole XML file from my hard drive with the XML response of a rest API call. I want to know how to get this done step by step in Ready API. If there is a groovy script, then what values need to be changed to what in the groovy script to perform this comparison.

 

  1. The XML file is on Hard drive
  2. Run the API call (using get method) in Ready API
  3. (XML) Response generates in Ready API, and it looks exactly as the file I'm comparing with but obviously it's not a file. Now I want to see the difference between the file and the response generated in this API call.
  4. Now, we probably need to add a step after in the test to do this comparison.
  5. Do I have to have a groovy script to achieve this or is there another way?
  6. If it has to be a groovy script, then where is the script I can find?  I don’t know what values and how to change them in groovy? 

    As per your instruction, I googled it. The following is one of a result

  7. import org.custommonkey.xmlunit.*;
    import org.xml.sax.SAXException;

    // Get XML documents
    def request = context.expand( '${Request 1#Response}' )
    def response = context.expand( '${Request 2#Response}' )

    // Creates a list of elements to ignore
    Set<String> ignoreList = new HashSet<String>();
    ignoreList.add("dateTime")
    ignoreList.add("packageId")

    // Create an object with differences between documents
    Diff myDiff = new Diff(request, response)
    DetailedDiff diff = new DetailedDiff(myDiff);

    // Get a list of all differences
    List allDifferences = diff.getAllDifferences();

    // Loop through all differences and find their node names
    for (int i = 0; i < allDifferences.size(); i++) {
        diffNodeName =""

        // Check the node type to get the right node name
        nodeType = allDifferences.get(i).getTestNodeDetail().getNode().getNodeType()

        if (nodeType == 1) {

            // Get the name of the node if the difference is in the comment
            diffNodeName = allDifferences.get(i).getTestNodeDetail().getNode().getNodeName()
        } else {

        // Get the name of the parent node if the difference is not in the comment
        diffNodeName = allDifferences.get(i).getControlNodeDetail().getNode().getParentNode().getNodeName()
        }

        // Make sure that the node with a difference is not on the list of nodes to ignore
        if (!ignoreList.contains(diffNodeName)) {

            // Fail assertion
             assert false
        }
    }

    assert true

    Now the challenge is, I'm not a groovy expert nor I'm a programmer. Now I need to help someone to let me know how to use the above in detail 

Thanks,


Ray

Highlighted
Super Contributor

As you have mentioned that you are looking for an alternative to Groovy, here is a suggestion (Note: This will only work if the file containing the XML data on your hard drive is identical):

 

1) First use a File Wait test step to pick up the file.

2) Run your REST request test step.

3) Within your REST request use a Message Content Assertion to compare the result of your API call with the contents of the file.

 

The key thing to note here is that the file Wait test step provides the contents of the file as a variable "fileContents" (review the above linked doc page). This can be accessed from another test step via a property expansion, if you are unsure about these you can use the point and click Get Data functionality. 

 

If your XML file is not exactly identical (for example it contains todays date), then as @nmrao mentioned you can use xmlunit, but this does involve installing 3rd party libraries and using Groovy, here's a link to a post I made previously about it:

 

https://community.smartbear.com/t5/SoapUI-Pro/Groovy-XML-Comparison-Dynamic-XML-Attributes-are-not-i...

Highlighted
Community Manager

nmrao, Radford, thanks a lot!

@API_Tester hi! Did you have a chance to see if the suggestions work? 


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