在 Cypress 测试运行器上隐藏 XHR 调用

Hide XHR calls on Cypress test runner

我试图在 cypress 测试运行器上隐藏 XHR 调用。我在 support/index.js 中添加了以下代码,但它仍然不起作用。谁能告诉我它是如何工作的?

Cypress.Server.defaults({
  delay:500,
  force404:false,
  ignore: (xhr) => {
    return false;
  },
})

看起来 Cypress.Servercy.server() 一起被弃用(可能是同一回事)。

拦截可能会做你想做的事

cy.intercept(url, (req) => {
  if (req.type === 'xhr') {
    // custom logic for handling
  }
})

但我不认为您使用的示例代码旨在“隐藏”xhr 请求。你想用它们做什么?

试试这个,对我有用

将以下内容添加到 cypress/support/index.js:

// Hide fetch/XHR requests
const app = window.top;
if (!app.document.head.querySelector('[data-hide-command-log-request]')) {
  const style = app.document.createElement('style');
  style.innerHTML =
    '.command-name-request, .command-name-xhr { display: none }';
  style.setAttribute('data-hide-command-log-request', '');

  app.document.head.appendChild(style);
}

参考和获得的详细信息https://gist.github.com/simenbrekken/3d2248f9e50c1143bf9dbe02e67f5399