Comparing Current xml Response to a Previously Saved xml Response

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:

Is there a way to save an xml response as a variable and run the same request at a later date to compare the current response to the saved response?

Details (like screenshots):

How I found the problem:

I’ve already tried:

I found a solution to stringify a JSON response and save the first response as a variable and then the second response as another variable and then compare the two. Unfortunately, my organization test xml responses as well as Json responses. Currently I am parsing the xml response and testing each variable but that is very time consuming.

The following works, but doesn’t really tell you which nodes are incorrect, which is a bit of an issue.

var firstString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><employees><employee><firstname>John</firstname><lastname>Doe</lastname></employee><employee><firstname>Jane</firstname><lastname>Doe</lastname></employee></employees>";
var secondString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><employees><employee><firstname>John</firstname><lastname>Doe</lastname></employee><employee><firstname>Jane</firstname><lastname>Doe</lastname></employee></employees>";
var thirdString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><employees><employee><firstname>John</firstname><lastname>Doe</lastname></employee><employee><firstname>Dave</firstname><lastname>Jones</lastname></employee></employees>";

// parse the XML to a Javascript object.  xml2Json is built into Postman, so I'm using that
var javascriptObject1 = xml2Json(firstString); 
var javascriptObject2 = xml2Json(secondString);
var javascriptObject3 = xml2Json(thirdString);  

// just a pretty view of the existing XML (by converting the javaScript object back to an XML string) - not used in the comparison
var xml2js = require("xml2js");
var builder = new xml2js.Builder();
console.log(builder.buildObject(javascriptObject1));
console.log(builder.buildObject(javascriptObject2));
console.log(builder.buildObject(javascriptObject3));

// the comparison is using lodash _isEqual against the javascriptObjects
pm.test("First and second string should match", () => {
    var isEqual = _.isEqual(javascriptObject1, javascriptObject2);
    pm.expect(isEqual).to.be.true;
});    

pm.test("first and third string should not match", () => {
    var isEqual = _.isEqual(javascriptObject1, javascriptObject3);
    pm.expect(isEqual).to.be.false;
});    

pm.test("first and third string should not match v2", () => { // this should fail
    var isEqual = _.isEqual(javascriptObject1, javascriptObject3);
    pm.expect(isEqual).to.be.true;
});    

What I suspect is needed is a Javascript function to provide the difference between the two objects. You can then assert on whether the result is null (no differences) or not. The result will show the elements that don’t match, which would be useful for troubleshooting purposes.

I’ve found quite a few online options for producing the “diff” of two XML documents, but I haven’t managed to find a simple function that I can just copy and paste and reuse in Postman.