:) 我选择了 Cypress.io 工具进行自动化测试。 我需要对我的 sitemap.xml 文档进行一些测试,但我不知道该怎么做:(
我已经尝试安装一个 npm 包 libxmljs
npm install libxmljs --save
并将其作为插件加载到 cypress/plugins/index.js 中
const libxmljs = require('libxmljs');
但这有一个问题。它显示一个错误
The plugins file is missing or invalid.
Your pluginsFile is set to /home/my-app/cypress/plugins/index.js, but
either the file is missing,
it contains a syntax error, or threw an error when required.
The pluginsFile must be a .js or .coffee file.
Please fix this, or set pluginsFile to false if a plugins file is not
necessary for your project.
Error: The module '/home/my-app/node_modules/libxmljs/build/Release/xmljs.node'
请帮助我,我如何在 Cypress.io 中使用 libxmljs,或者我应该如何在这个端到端测试工具中为 Sitemap.xml 编写测试。
感谢您的宝贵时间! :)
最佳答案
虽然@NoriSte's answer is correct ,我找到了一个更简单的替代方案,不需要任何第三方代码。
Cypress API 公开了所有必要的方法:
- 加载文件(在您的情况下为 sitemap.xml):
cy.request
. - 解析 XML 文件(它公开了 jQuery API ):
Cypress.$
- 检查页面是否加载成功(状态码为 200):
cy.visit
这是我用来测试站点地图中声明的所有页面是否正在加载(并确保它不指向任何 404)的以下测试:
describe('Sitemap', () => {
// initialize the url array
let urls = []
// be sure to get the url list before executing any tests
before(async () => {
// getch the sitemap content
const response = await cy.request('sitemap.xml')
// convert sitemap xml body to an array of urls
urls = Cypress.$(response.body)
// according to the sitemap.xml spec,
// the url value should reside in a <loc /> node
// https://www.google.com/sitemaps/protocol.html
.find('loc')
// map to a js array
.toArray()
// get the text of the <loc /> node
.map(el => el.innerText)
})
it('should succesfully load each url in the sitemap', () => {
urls.forEach(cy.visit)
})
})
关于Cypress.io - sitemap.xml 验证测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55100545/