Capture + Display all logs for everything that happens in Cypress
  • 31-May-2023
Lightrun Team
Author Lightrun Team
Share
Capture + Display all logs for everything that happens in Cypress

Capture + Display all logs for everything that happens in Cypress

Lightrun Team
Lightrun Team
31-May-2023

Explanation of the problem

Many users have expressed their desire to view console.log statements when running headlessly in our application. By default, these statements are visible when working in Dev Tools in headed GUI mode. However, the challenge arises when running headlessly, as console.log statements are not readily available. Although we can display these statements by mixing them into stdout, it is considered more of a workaround rather than a true solution. Additionally, it doesn’t provide a comprehensive understanding of what is happening during a test. To address this, we aim to implement a comprehensive logging solution that not only captures console.log statements but also encompasses other crucial information such as network traffic, commands, retries, under the hood Cypress wizardry, and DOM snapshots on failure. This comprehensive logging approach will provide users with a complete picture of the test execution and enable them to access more information than what the GUI headed version offers.

 

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Capture + Display all logs for everything that happens in Cypress

The proposed solution for comprehensive logging in Cypress is still in the early stages and does not have a confirmed timeline for delivery. In the meantime, there are existing plugins that can be used as workarounds. The “cypress-log-to-output” plugin prints browser console logs to the terminal during test runs, while the “cypress-failed-log” plugin saves the Cypress test command log as a JSON file in case of test failures. Another option is the “cypress-terminal-report” plugin, which logs Cypress commands, route request data, and browser console errors and warnings specifically when tests fail on CI. However, an alternative suggestion is to redirect all browser console logs to the terminal during CI runs, providing a simpler solution for debugging. This approach allows for immediate access to logging statements from both the tests and the application, facilitating the analysis of any encountered issues.

 

Other popular problems with cypress-io cypress

Problem: Slow performance when running tests with a large number of elements

When running tests with a large number of elements, Cypress can experience slow performance due to the need to interact with each element individually. This can lead to long test execution times and decreased productivity.

Solution:

To address this issue, it is recommended to limit the number of elements that are interacted with in a single test. This can be achieved by breaking down tests into smaller, more focused tests that only interact with the necessary elements. Additionally, using the cy.get method to retrieve specific elements, rather than using more general methods like cy.find, can help to improve performance.

Problem: Tests that are brittle and prone to breaking with changes to the application

One of the biggest challenges with end-to-end testing is maintaining the stability of tests over time as the application changes. Tests that are overly reliant on specific element selectors, for example, are prone to breaking when the application changes and these selectors are no longer valid.

Solution:

To address this issue, it is important to write tests that are flexible and can adapt to changes in the application. This can be achieved by using data-driven testing techniques, which allow you to define the elements you are testing in a data file that can be easily updated. Additionally, it is important to write tests that are focused on testing the functionality of the application, rather than specific element selectors.

Problem: Debugging tests can be difficult and time-consuming

Debugging tests can be difficult and time-consuming, especially when tests are failing for complex or obscure reasons. This can be a major obstacle to efficient testing and can slow down development.

Solution:

To address this issue, Cypress provides powerful debugging tools that make it easier to understand why tests are failing. For example, the time-travel debugging feature allows you to step through the execution of your tests in real-time, observing the state of the application and the DOM at each step. Additionally, the real-time reloading feature allows you to make changes to your tests while they are running, which can greatly speed up the debugging process

A brief introduction to cypress-io cypress

Cypress is an end-to-end testing framework designed to make testing modern web applications simple and fast. It provides a streamlined, browser-based testing environment that allows developers to write, run, and debug tests directly in the browser. Cypress leverages the same environment that the application is running in, providing a more accurate and efficient testing experience.

Cypress utilizes a JavaScript API that interacts with the DOM and the browser directly, enabling it to access and manipulate elements and their behaviors in real-time. This API is designed to be easy to use, with a simple syntax that makes it possible to write tests quickly and effectively. Additionally, Cypress includes features such as automatic waiting, real-time reloading, and time-travel debugging, which make it easier for developers to write, run, and debug tests. These features, combined with the fast, browser-based testing environment, make Cypress a powerful tool for end-to-end testing.

Most popular use cases for cypress-io cypress

  1. End-to-end testing of web applications Cypress is a powerful tool for end-to-end testing of web applications. It provides an easy-to-use API for interacting with elements on a page, making it possible to test the full user experience, from navigation to the submission of forms. For example, you can use the following code to navigate to a page and fill out a form:
cy.visit('https://example.com')
cy.get('#form-username').type('my-username')
cy.get('#form-password').type('my-password')
cy.get('#form-submit').click()
  1. Real-time reloading and time-travel debugging Cypress also provides real-time reloading and time-travel debugging features, which can greatly speed up the development process. The real-time reloading feature allows you to make changes to your tests while they are running, which can save you time and effort by eliminating the need to constantly stop and restart your tests. The time-travel debugging feature allows you to step through the execution of your tests in real-time, observing the state of the application and the DOM at each step.
  2. Automated testing of web applications in a real browser environment Finally, Cypress provides a true end-to-end testing experience by executing tests directly in a real browser environment. This provides a more accurate testing experience, as tests run in the same environment as the users of your application, allowing you to catch problems that may not be evident in a simulated environment.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.