Cypress:监控控制台输出

Val*_*anu 5 javascript cypress

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

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

有可能吗?

Ric*_*sen 5

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

文档可以使用一点再治具,所以这里就是我如何设置的间谍。

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

})
Run Code Online (Sandbox Code Playgroud)