Cypress 解析 XML 响应

phi*_*hos 2 javascript xml automation automated-tests cypress

我有一个 api,它返回 xml 数据。

我正在 cypress 中编写一个测试用例,通过它我请求该 api,它返回以下数据

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Student>
    <Roll>55</Roll>
    <Name>ABC</Name>
</Student>
Run Code Online (Sandbox Code Playgroud)

我如何解析此响应正文并Name从此响应中获取学生信息?

小智 5

使用 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)

})
Run Code Online (Sandbox Code Playgroud)

或在 Cypress 命令链中

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 command', () => {

  cy.wrap(Cypress.$(xml))
    .then(xml => xml.filter('student').find('name').text())
    .should('eq', 'ABC')
})
Run Code Online (Sandbox Code Playgroud)