Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
Cypress - extract value from XML response

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>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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");
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda