如何编写 Cypress 测试来读取 XML 文件并检查元素是否与指定值匹配?
这是 XML 文件:
<?xml version='1.0'?>
<Establishment>
<Detail>
<BusinessName>Pizza</BusinessName>
</Detail>
</Establishment>
我正在尝试获取
BusinessName
元素并检查它是否与字符串匹配。
当您使用
cy.readFile()
读取 XML 文件或在 API 响应中接收 XML 时,它是一个字符串(文本)。您需要将其解析为 XML 文档才能有效地进行测试。
DOMParser 是进行解析的浏览器对象。
cy.readFile('path-to-my-xml-file')
.then(xmlText => {
const parser = new DOMParser();
const doc = parser.parseFromString(xmlText, "application/xml");
const businessNameContent = doc.querySelector('BusinessName').textContent
expect(businessNameContent).to.eq('Pizza')
})