How to parse XML and validate the response fields using the cheerio library in postman?
Sample URL: https://www.w3schools.com/xml/tempconvert.asmx
Sample Request Payload:
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<FahrenheitToCelsius xmlns="https://www.w3schools.com/xml/">
<Fahrenheit>108</Fahrenheit>
</FahrenheitToCelsius>
</soap12:Body>
</soap12:Envelope>
Expected Response:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<FahrenheitToCelsiusResponse xmlns="https://www.w3schools.com/xml/">
<FahrenheitToCelsiusResult>42.2222222222222</FahrenheitToCelsiusResult>
</FahrenheitToCelsiusResponse>
</soap:Body>
</soap:Envelope>
Test to add under "Tests" tab of postman:
pm.test("XML response data value verification", function () {
var responseData = pm.response.text();
const $ = cheerio.load(responseData, {
ignoreWhitespace: true,
xmlMode: true
});
let FahrenheitToCelsiusResult= $('FahrenheitToCelsiusResponse').find('FahrenheitToCelsiusResult');
pm.expect(FahrenheitToCelsiusResult.text()).to.eql("42.2222222222222");
});
Comments
Post a Comment