Reputation: 83
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<m:GetPriceRespon`enter code here`se xmlns:m="https://www.w3schools.com/prices">
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
</soap:Body>
</soap:Envelope>
I want to read "Price" from above XML , how to read this price using cypress and store it to a variable ? could any one help me with the sample cypress code ?
Upvotes: 2
Views: 388
Reputation: 31914
You can use a DOMParser to make a queryable document,
it('parses XML price', () => {
const xmlStr = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<m:GetPriceResponse xmlns:m="https://www.w3schools.com/prices">
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
</soap:Body>
</soap:Envelope>`
const parser = new DOMParser();
const doc = parser.parseFromString(xmlStr, "application/xml");
const price = +doc.querySelector("Price").textContent // numeric value of text content
cy.wrap(price)
.as('price') // save for later
.should('eq', 1.90) // verify the value
});
Upvotes: 1
Reputation: 18650
You can do something like this:
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<m:GetPriceResponse xmlns:m="https://www.w3schools.com/prices">
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
</soap:Body>
</soap:Envelope>`
cy.wrap(Cypress.$(xml))
.then((xml) => xml.filter('soap:Body').find('m:Price').text())
.as('price')
cy.get('@price').then((price) => {
//Access price here
})
Upvotes: 0