Forum Discussion
- nmraoChampion Level 3It 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_TesterOccasional 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.
- The XML file is on Hard drive
- Run the API call (using get method) in Ready API
- (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.
- Now, we probably need to add a step after in the test to do this comparison.
- Do I have to have a groovy script to achieve this or is there another way?
- 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 - 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- RadfordSuper 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:
Related Content
- 10 years ago
- 6 years ago
- 4 years ago
Recent Discussions
- 5 days ago
- 10 days ago