最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress parse XML response - Stack Overflow

programmeradmin2浏览0评论

I have an api, which return xml data.

I am writing a testcase in cypress, through which I am requesting that api, which returns following data

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Student>
    <Roll>55</Roll>
    <Name>ABC</Name>
</Student>

How do I parse this response body and get Name of the student from this response ?

I have an api, which return xml data.

I am writing a testcase in cypress, through which I am requesting that api, which returns following data

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Student>
    <Roll>55</Roll>
    <Name>ABC</Name>
</Student>

How do I parse this response body and get Name of the student from this response ?

Share Improve this question edited May 20, 2021 at 1:13 user14783414 asked May 19, 2021 at 12:15 philein-sophosphilein-sophos 3721 gold badge6 silver badges28 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

Synchronously, with jQuery

const xml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <Student>
    <Roll>55</Roll>
    <Name>ABC</Name>
  </Student>`

it('parses the student name from xml', () => {

  function xmlProperty(xml, property) {
    return Cypress.$(Cypress.$.parseXML(xml)).find(property).text()
  }

  const name = xmlProperty(xml, 'Name') 
  console.log(name)

})

or in a Cypress mand chain

const xml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <Student>
    <Roll>55</Roll>
    <Name>ABC</Name>
  </Student>`

it('parses student name in a Cypress mand', () => {

  cy.wrap(Cypress.$(xml))
    .then(xml => xml.filter('student').find('name').text())
    .should('eq', 'ABC')
})
发布评论

评论列表(0)

  1. 暂无评论