【问题标题】:Cypress: monitor console output赛普拉斯:监控控制台输出
【发布时间】:2019-10-24 08:31:04
【问题描述】:

我知道 Cypress 可以在浏览器控制台中打印调试信息,但它可以在测试期间从控制台读取数据吗?

我正在开发一个由 three.js 驱动的应用程序,因此我无法正确测试应用程序的 3d 方面,但我想在浏览器控制台中监听 javascript 错误。

有可能吗?

【问题讨论】:

  • 您可以尝试在window (developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/…) 上收听error 事件。但这不会“读取”控制台。您可以尝试覆盖全局 console 对象以“捕获”错误,然后再将它们打印到浏览器的控制台中,但您必须在初始化任何其他脚本之前覆盖它:const _err = console.error; console.error = (...args) => _err('Caught!', ...args);

标签: javascript cypress


【解决方案1】:

您可以使用 Cypress cy.spy() 拦截控制台消息,但如果您想进一步了解数据 - 我还没有看到任何方法。

docs 可能需要重新设置一下,所以这就是我设置间谍的方法。

let spy;
Cypress.on('window:before:load', (win) => {
  spy = cy.spy(win.console, "error")  // can be other methods - log, warn, etc
})

it('Doing something that should not cause a console error', () => {

  // Run test steps here that may cause a console error

  cy.wait(100).then(x => {  
    expect(spy).not.to.be.called
  })

  // or perhaps this, to auto-retry (have not tried this syntax)
  cy.wrap({}).should(() => {  
    expect(spy).not.to.be.called
  })

  // The docs imply you can just do this

  expect(spy).not.to.be.called

  // ..but that line may run before any other cy command above finish
  // so I'd stick with using cy.wrap({}).then(...) to put it in the command chain

  // The spy call count is reset after each test

})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2019-10-25
    • 2019-09-26
    • 2020-12-23
    • 1970-01-01
    相关资源
    最近更新 更多