【发布时间】: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)
})
【问题讨论】:
-
我想这就是你要找的东西stackoverflow.com/a/65566283/9884190
-
@ManuelAbascal 我无权访问 UI 代码。所以我无法改变它。只有我可以访问自动化代码
-
无论您是否有权访问 UI 代码,它都应该有效。赛普拉斯正在启动一个电子应用程序并将浏览器嵌入应用程序的窗口中……因此我们应该可以访问浏览器日志。您是否尝试过在
it块中使用此代码? -
@ManuelAbascal 不,它不起作用
-
您将需要更新您的答案以包括屏幕截图、添加的代码、日志、错误消息等...
标签: javascript automated-tests cypress spy