【问题标题】:cypreess to get browser logs for specific page赛普拉斯获取特定页面的浏览器日志
【发布时间】:2022-11-12 08:11:29
【问题描述】:

我已经在 Cypress 项目上工作了 2 个月。我的下一个任务是获取浏览器日志。

假设这个页面是我目前需要测试的页面:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Home</title>
    </head>
    <body>
        <h1>
            Home page
        </h1>
        <script>
            console.log ("log test");
            console.warn("warn test");
            console.warn("warn test2");
            console.error ("console error");
            console.error ("console error 2222");
            console.info("info test");
        </script>
    </body>
    </html>

加载此页面后,将有 6 个日志。我们可以通过打开检查窗口看到它。

我想获取柏树代码中的每个日志计数:

      it('should not greater than the previous error log count', () => {
    
               const allLogs = getlogs(); // get all the broswer logs.
               const previousCount = getPreviousValueFromExcel() // this method already implemented
               const erroLogCount = // filter allLogs and get only console.error count
    
            
        })

我无法访问 UI 代码。所以我无法改变它。只有我可以访问自动化代码

方法一

it('should not greater than the previous error log count', () => {
  cy.visit('/foo', {
    onBeforeLoad(win) {
      // Stub your functions here
      cy.stub(win.console, 'error').as('consoleError');
    }
  });
  const previousCount = 2;
  cy.get('@consoleError').should('have.length', previousCount) 
})

这种方法也行不通。每次我运行测试它总是返回 0。

【问题讨论】:

  • 我想这就是你要找的东西stackoverflow.com/a/65566283/9884190
  • @ManuelAbascal 我无权访问 UI 代码。所以我无法改变它。只有我可以访问自动化代码
  • 无论您是否有权访问 UI 代码,它都应该有效。赛普拉斯正在启动一个电子应用程序并将浏览器嵌入应用程序的窗口中……因此我们应该可以访问浏览器日志。您是否尝试过在 it 块中使用此代码?
  • @ManuelAbascal 不,它不起作用
  • 您将需要更新您的答案以包括屏幕截图、添加的代码、日志、错误消息等...

标签: javascript automated-tests cypress spy


【解决方案1】:

You can use cy.spy to spy on the console.log functions.

it('should not greater than the previous error log count', () => {
  cy.visit('/foo', {
    onBeforeLoad(win) {
      // Stub your functions here
      cy.stub(win.console, 'error').as('consoleError');
    }
  });
  const previousCount = getPreviousValueFromExcel();
  cy.get('@consoleError').should('have.length', previousCount) // or however you are determining the expected error count.
})

【讨论】:

  • 它总是返回 0 - “ assertexpected [Function: consoleError] 的长度为 2 但得到 0 ”
  • 啊,如果你使用have.callCount 而不是have.length 呢?
  • 谢谢你的回复。,我更新描述。你能检查一下吗?我试过你回答但它失败了,我添加了错误的截图
  • 您是否也尝试使用have.callCount 而不是have.length?截图还有have.length
  • 在这里我们检查确切的计数。你知道检查日志计数是否小于给定数字的方法吗
【解决方案2】:

你在找Cypress spy on multiple calls of the same method

it('should not greater than the previous error log count', () => {
  cy.visit('/foo', {
    onBeforeLoad(win) {
      cy.spy(win.console, 'error').as('consoleError')
    }
  });
  const previousCount = 2;

  cy.get('@consoleError')
    .its('callCount')
    .should('eq', previousCount)
})

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多