Forum Discussion

API_Tester's avatar
API_Tester
Occasional Contributor
6 years ago

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.

  • nmrao's avatar
    nmrao
    Champion Level 3
    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.
    • API_Tester's avatar
      API_Tester
      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