Cypress 解析 XML 响应
Cypress parse XML response
我有一个 api,其中 return xml 数据。
我正在用 cypress 编写一个测试用例,通过它我请求 api,其中 return 跟随数据
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Student>
<Roll>55</Roll>
<Name>ABC</Name>
</Student>
如何解析此响应正文并从此响应中获取学生的 Name
?
Edi:我进行了搜索并找到了一个 similar question,它具有似乎有效的好解决方案
const text = "<string>This is my xml</string>"; //API response in XML
const parser = new DOMParser();
const xmlDOM = parser.parseFromString(text,"text/xml");
const value = xmlDOM.getElementsByTagName("string")[0].childNodes[0].nodeValue;
console.log(value)
与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')
})
我有一个 api,其中 return xml 数据。
我正在用 cypress 编写一个测试用例,通过它我请求 api,其中 return 跟随数据
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Student>
<Roll>55</Roll>
<Name>ABC</Name>
</Student>
如何解析此响应正文并从此响应中获取学生的 Name
?
Edi:我进行了搜索并找到了一个 similar question,它具有似乎有效的好解决方案
const text = "<string>This is my xml</string>"; //API response in XML
const parser = new DOMParser();
const xmlDOM = parser.parseFromString(text,"text/xml");
const value = xmlDOM.getElementsByTagName("string")[0].childNodes[0].nodeValue;
console.log(value)
与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')
})