TechBuzzPro

Improve Error Screenshots in Cypress: A How-To

· 4 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

When a Cypress test fails, the default screenshot can be a blurry, cropped, or unhelpful image that makes debugging slower. By configuring Cypress's screenshot behavior, you can capture full-page views, adjust image quality, and even add custom styling to make failures instantly understandable. This guide walks you through the key settings and techniques to get clearer, more useful error screenshots.

What You'll Need

  • A Cypress project (version 10 or later recommended)
  • Basic familiarity with your Cypress configuration file (cypress.config.js or cypress.config.ts)
  • Access to your test files and the cypress/support folder

Step-by-Step

  1. Set screenshot quality and scale in your config. In your Cypress config file, add a screenshotOnRunFailure (default is true) and adjust screenshotQuality (a number from 0 to 100, default 80) and viewportScale (default 1). For example, to get sharper images, set screenshotQuality: 90 and viewportScale: 1. Be aware that higher values increase file size and capture time.
  2. Capture the full page on failure. By default, Cypress captures only the viewport. To capture the entire scrollable page, add a before hook or use cy.on('fail') to call cy.screenshot({ capture: 'fullPage' }) before the test fails. A simpler approach is to set screenshotOnRunFailure: true and then in your cypress/support/index.js (or e2e.js), add a global before hook that sets the capture option for all failures:
  3. Use a custom command to override the failure screenshot. Create a custom command like Cypress.Commands.overwrite('screenshot', ...) to modify the default behavior. For example, you can force fullPage capture and add a timestamp to the filename:
  4. Add visual context with CSS injection. Before taking a screenshot, you can inject a temporary class or style to highlight the failing element. Use cy.get('selector').then($el => { $el.css('outline', '3px solid red'); }) inside a cy.on('fail') handler or before a manual cy.screenshot() call.
  5. Use the before and after hooks to set up a clean state. Ensure that overlays, modals, or animations are not obscuring the screenshot. In your test, you can close popups or wait for animations to finish before the failure screenshot is taken by adding a cy.wait(500) in the fail handler.
  6. Leverage the Cypress Dashboard for video and screenshot comparison. If you use Cypress Cloud (formerly Dashboard), it automatically captures screenshots and videos. You can configure the project to upload these artifacts, making them easier to review in CI.
  7. Test your configuration with a deliberate failure. Write a temporary test that fails (e.g., cy.get('nonexistent').should('exist')) and run it. Check the screenshot in cypress/screenshots to see if it captures the full page and is sharp enough.

Common Mistakes

  • Setting screenshotOnRunFailure to false accidentally while trying to disable screenshots for a specific test.
  • Forgetting that viewportScale affects all screenshots, including manual ones, and can cause layout shifts.
  • Using capture: 'fullPage' on pages with infinite scroll – it will capture a huge image and may time out.
  • Overwriting the screenshot command incorrectly, breaking the default behavior for all tests.
  • Not accounting for fixed headers – they may appear multiple times in fullPage screenshots.

Tips

  • Use cy.screenshot('name', { capture: 'viewport' }) in your tests when you want a specific, cropped shot – don't rely on the failure screenshot for everything.
  • Keep the default screenshotQuality at 80 unless you need extra detail – higher values slow down the run.
  • In CI, store screenshots as artifacts (e.g., in GitHub Actions or CircleCI) so you can download them after a run.
  • Add a timestamp to the screenshot filename to avoid overwriting previous failures: cy.screenshot(`failure-${Date.now()}`).
  • If you use TypeScript, add types for your custom commands to avoid errors.

Troubleshooting

  • Screenshot is still cropped. Check if you have a cy.screenshot() call in your test that overrides the failure capture. Also, ensure that capture: 'fullPage' is correctly set in the fail handler – remember that cy.on('fail') must be registered before the test runs.
  • Screenshot is blurry. Increase screenshotQuality to 100, but note that this only affects JPEG compression. If you need pixel-perfect, use PNG by setting screenshotQuality: 0 (Cypress will use PNG).
  • Screenshot is blank or white. This often happens when the page is still loading or the test fails before the DOM is ready. Add a cy.wait(1000) in the fail handler to give the browser time to paint.
  • FullPage capture is extremely slow. This is expected on long pages. Consider using capture: 'viewport' and rely on the video to see the rest, or split the page into sections.
  • Custom command not working. Make sure you place the overwrite in cypress/support/commands.js (or e2e.js) and that the support file is imported in your config.

Frequently Asked Questions

  1. Does Cypress take screenshots on every test failure by default? Yes, it does, unless you set screenshotOnRunFailure: false in your config.
  2. Can I change the screenshot format to PNG? Yes, set screenshotQuality: 0 – Cypress will then use PNG instead of JPEG.
  3. How do I capture a specific element instead of the whole page? Use cy.get('selector').screenshot('element-name') – this captures only that element.
  4. Why are my screenshots showing a blank viewport? This can happen if the test fails during page load. Try adding a cy.wait() or use cy.intercept() to wait for network requests to finish.
  5. Can I take a screenshot on a manual command, not just on failure? Absolutely, call cy.screenshot() anywhere in your test to capture the current state.
  6. Do screenshots work in headless mode? Yes, Cypress captures screenshots in both headed and headless modes, but you may need to configure the browser's viewport size to match your needs.