Cypress.io - sitemap.xml 验证测试

Cypress.io - sitemap.xml validation test

:) 我选择了一个工具 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 编写测试。

感谢您的宝贵时间! :)

如果您想使用 libxmljs 来解析您的站点地图,您应该

  • 使用 cy.request
  • 阅读站点地图本身
  • 添加一个 custom task to Cypress(因为 libxmljs 是一个节点库,cy.task 是从您的 Cypress 测试中使用 Node.js 脚本的唯一方法)
  • returns 从您的任务中解析的数据
  • 在 Cypress 测试中断言它

这些是您需要执行的高级步骤

虽然 ,但我发现了一个 更简单 的替代方案,不需要任何第 3 方代码。

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 类似 promise 的命令而不是异步调用。

describe('Sitemap', () => {
  let urls = [];

  before(() => {
    cy.request('sitemap.xml')
    .as('sitemap')
    .then((response) => {
      urls = Cypress.$(response.body)
            .find('loc')
            .toArray()
            .map(el => el.innerText);
    });
  });

  it('should succesfully load each url in the sitemap', () => {
    urls.forEach(cy.visit);
  });
});

在 Cypress 中使用异步可能会引发错误“Cypress 检测到您在测试中返回了一个 promise,但也在该 promise 中调用了一个或多个 cy 命令”。

describe('Sitemap', () => {
  let urls = [];

  before(() => {
    const parser = new DOMParser();

    cy.request('/sitemap.xml').then((response) => {
      const document = parser.parseFromString(response.body, 'application/xml');
      const parsedUrls = document.getElementsByTagName('loc');

      urls = Array.from(parsedUrls).map((item) => item.innerHTML);
    });
  });

  it('Should load each url from the sitemap', () => {
    urls.forEach(cy.visit);
  });
});