我有一个 api,它返回 xml 数据。
我正在用 cypress 编写一个测试用例,通过它我请求那个 api,它返回以下数据
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Student>
<Roll>55</Roll>
<Name>ABC</Name>
</Student>
我如何解析这个响应正文并从这个响应中获取学生的 Name
?
最佳答案
与 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)
})
或在 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')
})
关于javascript - Cypress 解析 XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67603093/