Cypress.io - sitemap.xml validation test

Boh*_*ček 2 sitemap libxml-js cypress

:) I chose for automated testing a tool Cypress.io. I need some tests for my sitemap.xml document and I dont know how to do that :(

I have tried install an npm package libxmljs

npm install libxmljs --save

and load it as plugin in cypress/plugins/index.js

const libxmljs = require('libxmljs');
Run Code Online (Sandbox Code Playgroud)

But there is a problem with this. It shows an error

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'
Run Code Online (Sandbox Code Playgroud)

Please help me, how can I use libxmljs in Cypress.io or how i should write tests for Sitemap.xml in this end-to-end testing tool.

Thanks for your time! :)

gio*_*_13 5

虽然@NoriSte 的回答是正确的,但我发现了一个更简单的替代方案,不需要任何 3rd 方代码。

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)
      })
    })
Run Code Online (Sandbox Code Playgroud)