Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress.io - sitemap.xml validation test

:) 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');

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'

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! :)

like image 652
Bohumil Havlíček Avatar asked Nov 05 '25 06:11

Bohumil Havlíček


2 Answers

Although @NoriSte's answer is correct, I found a simpler alternative without the need for any 3rd party code.

Cypress API exposes all the necessary methods to:

  • load a file (the sitemap.xml in your case): cy.request.
  • parse XML file (it exposes the jQuery API): Cypress.$
  • check if a page successfully loads (with a 200 status code): cy.visit

This is the following test that I use to test if all of the pages declared in the sitemap are loading (and make sure it doesn't point to any 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)
  })
})
like image 148
gion_13 Avatar answered Nov 06 '25 20:11

gion_13


If you want to use libxmljs to parse your sitemap you should

  • read the sitemap itself with cy.request
  • add a custom task to Cypress (because libxmljs is a node library, cy.task is the only way to consume Node.js scripts from your Cypress tests)
  • returns the parsed data from your task
  • assert about it in a Cypress test

Those are the high-level steps you need to do 😉

like image 37
NoriSte Avatar answered Nov 06 '25 20:11

NoriSte