I am having problems retrieving a value from an XML response when using Cypress to make the API request. The API returns a 200, and I can see the response body, as expected, in the browser console.
I have tried the following after much research online, however the 'Price' value returned is undefined.
Guidance would be much appreciated.
/// <reference types="cypress" />
it('Submit test', () => {
cy
.readFile('../fixtures/test1.xml')
.then(fetchXML)
.then(responseFromXML => {
expect(responseFromXML.status).to.eq(200)
const parser = new DOMParser();
const xmlDOM = parser.parseFromString(responseFromXML,"text/xml");
const value = xmlDOM.getElementsByTagName("Price")[0];
console.log("Extracted value is "+value)
})
function fetchXML(xml_body) {
return cy.request({
url: Cypress.env('test_endpoint'),
method: 'POST',
body: xml_body,
headers: {
'Content-Type': 'application/xml',
},
})
}
})
Please see sample XML response below. I suspect the issue is with line
const value = xmlDOM.getElementsByTagName("Price")[0];
XML Response:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<tns:CreateV2ResponseParameter xmlns:tns="http://ws.xxx.com/wsdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:PriceList>
<tns:PriceDetails>
<tns:Price>211</tns:Price>
</tns:PriceDetails>
</tns:PriceList>
</tns:CreateV2ResponseParameter>
</SOAP:Body>
</SOAP:Envelope>
looks like you pass incorrect value to the parser here:
parser.parseFromString(responseFromXML,"text/xml");
it should probably be responseFromXML.body:
parser.parseFromString(responseFromXML.body,"text/xml");