I have been working on a Cypress project for 2 months. My next task is to get the browser logs.
Let's imagine this page is the one I need to test for the moment:
<!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>
once this page is loaded there will be 6 logs. we can see it by opening inspect window.
I want to get each log count in the cypress code:
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
})
I have no access to the UI code. so I can't change it. only I have access to the automation code
Method 1
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)
})
that approach also not working. every time I run the test it always returns 0.
Are you looking for 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)
})