Tengo problemas para recuperar un valor de una respuesta XML cuando uso Cypress para realizar la solicitud de API. La API devuelve un 200 y puedo ver el cuerpo de la respuesta, como se esperaba, en la consola del navegador.
Intenté lo siguiente después de mucha investigación en línea, sin embargo, el valor de 'Precio' devuelto no está definido.
La orientación sería muy apreciada.
/// <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', }, }) } })Consulte el ejemplo de respuesta XML a continuación. Sospecho que el problema es con la línea
const value = xmlDOM.getElementsByTagName("Price")[0];Respuesta XML:
<?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>parece que pasas un valor incorrecto al analizador aquí:
parser.parseFromString(responseFromXML,"text/xml"); probablemente debería ser responseFromXML.body :
parser.parseFromString(responseFromXML.body,"text/xml");