Postman Tutorial Part 53 – Extracting Value From XML Response in Postman

As a part of Postman Tutorial – End to End , in this post, we will learn how can we extract value from XML response in Postman.

Already, we learnt how can we send a XML and parameterized XML payload in Postman. In this post we will learn to extract values form XML response and assert.

Extracting or parsing XML response in Postman is little tricky. Let’s learn it.

Consider XML response as below :-


	12
	
		Amod
		Mahajan
		1000
		true
		
			2021-01-01
			2022-01-01
		
		Lunch
	

First you need to convert above XML response to JSON.

var response = xml2Json(responseBody)

This “response” will be root node similar to <head> tag in html DOM. Now we need to travel node by node. For example:- To reach node “firstName”, you need to start from root node -> created-booking – > booking -> firstname. It is similar to absolute XPath.

syntax:- rootNode[“node1”][“node2”][“node3”]

 // Convert xml response to json response
 var response = xml2Json(responseBody);
 // We need to travel node to node and each node will be in []
 var firstName= response["created-booking"]["booking"]["firstname"]
 console.log("Booking ID is : " + bookingID)

Let’s write whole code for above XML response :-

pm.test("Verify booking id is not null and an integer", function() {
    // Convert xml response to json response
    var response = xml2Json(responseBody);
    // We need to travel node to node and each node will be in []
    var bookingID = response["created-booking"]["bookingid"]
    pm.expect(bookingID).to.not.equal(null);
    // Since type of bookingID is string to converting to int first.
    // You can know type of variable using typeof(var) method
    pm.expect(parseInt(bookingID)).to.be.a("number");
});

pm.test("Verify booking is created with expected values passed in request", function() {
    var jsonReq = xml2Json(pm.request.body.raw);
    var response = xml2Json(responseBody);
    pm.expect(jsonReq["booking"]["firstname"]).to.eql(response["created-booking"]["booking"]["firstname"]);
    pm.expect(jsonReq["booking"]["lastname"]).to.eql(response["created-booking"]["booking"]["lastname"]);
    pm.expect(jsonReq["booking"]["totalprice"]).to.eql(response["created-booking"]["booking"]["totalprice"]);
    pm.expect(jsonReq["booking"]["depositpaid"]).to.eql(response["created-booking"]["booking"]["depositpaid"]);
    pm.expect(jsonReq["booking"]["bookingdates"]["checkin"]).to.eql(response["created-booking"]["booking"]["bookingdates"]["checkin"]);
    pm.expect(jsonReq["booking"]["bookingdates"]["checkout"]).to.eql(response["created-booking"]["booking"]["bookingdates"]["checkout"]);
    pm.expect(jsonReq["booking"]["additionalneeds"]).to.eql(response["created-booking"]["booking"]["additionalneeds"]);
})

pm.test("Verify booking is created with expected values from environment file", function() {
    var response = xml2Json(responseBody);
    pm.expect(pm.environment.get("firstname")).to.eql(response["created-booking"]["booking"]["firstname"]);
    pm.expect(pm.environment.get("lastname")).to.eql(response["created-booking"]["booking"]["lastname"]);
    pm.expect(pm.environment.get("totalprice")).to.eql(response["created-booking"]["booking"]["totalprice"]);
    pm.expect(pm.environment.get("depositpaid")).to.eql(response["created-booking"]["booking"]["depositpaid"]);
    pm.expect(pm.environment.get("checkin")).to.eql(response["created-booking"]["booking"]["bookingdates"]["checkin"]);
    pm.expect(pm.environment.get("checkout")).to.eql(response["created-booking"]["booking"]["bookingdates"]["checkout"]);
    pm.expect(pm.environment.get("additionalneeds")).to.eql(response["created-booking"]["booking"]["additionalneeds"]);
})

Import above example collection from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

2 thoughts on “Postman Tutorial Part 53 – Extracting Value From XML Response in Postman”

  1. I am having difficulty with this- my root node I need to access contains multiple elements. How do I access one of the elements within the root node?

  2. Special thanks to you for covering the various aspects of postman. It was great to see 53 tutorials

    When we have already converted xml2json why do we need to access like [][] and not by .

    Can you cover them how to save this as code repository?
    Cover other methods as well

Leave a Reply

Your email address will not be published. Required fields are marked *