Cypress.io - sitemap.xml 验证测试

标签 cypress sitemap libxml-js

:) 我选择了 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 公开了所有必要的方法:

这是我用来测试站点地图中声明的所有页面是否正在加载(并确保它不指向任何 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/

相关文章:

azure - 如何设置 Azure AD 和 Cypress 以便 SSO 登录工作

javascript - 如何更改登录测试场景以使其正常运行

seo - 将选择性 URL 添加到 XML 站点地图

gatsby - 如何从索引中排除 gatsby 创建的页面?

javascript - 如何从 XML 文档中获取对象?

Cypress:我可以将日志中的值用作字符串吗?

javascript - cypress.io : Command namespacing, 链接

ruby-on-rails - 在 Rails 3.1 中构建站点地图

xml - 如何使用 libxmljs 从 pom.xml 检索 xpath

javascript - 我正在使用 libxmljs 解析 XML 文件,并且 .get() 始终返回第一个子文件