Improve Error Screenshots in Cypress: A How-To
In this article
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.jsorcypress.config.ts) - Access to your test files and the
cypress/supportfolder
Step-by-Step
- Set screenshot quality and scale in your config. In your Cypress config file, add a
screenshotOnRunFailure(default is true) and adjustscreenshotQuality(a number from 0 to 100, default 80) andviewportScale(default 1). For example, to get sharper images, setscreenshotQuality: 90andviewportScale: 1. Be aware that higher values increase file size and capture time. - Capture the full page on failure. By default, Cypress captures only the viewport. To capture the entire scrollable page, add a
beforehook or usecy.on('fail')to callcy.screenshot({ capture: 'fullPage' })before the test fails. A simpler approach is to setscreenshotOnRunFailure: trueand then in yourcypress/support/index.js(ore2e.js), add a globalbeforehook that sets the capture option for all failures: - 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: - 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 acy.on('fail')handler or before a manualcy.screenshot()call. - Use the
beforeandafterhooks 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 acy.wait(500)in the fail handler. - 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.
- 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 incypress/screenshotsto see if it captures the full page and is sharp enough.
Common Mistakes
- Setting
screenshotOnRunFailureto false accidentally while trying to disable screenshots for a specific test. - Forgetting that
viewportScaleaffects 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
screenshotcommand 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
screenshotQualityat 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 thatcapture: 'fullPage'is correctly set in the fail handler – remember thatcy.on('fail')must be registered before the test runs. - Screenshot is blurry. Increase
screenshotQualityto 100, but note that this only affects JPEG compression. If you need pixel-perfect, use PNG by settingscreenshotQuality: 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(ore2e.js) and that the support file is imported in your config.
Frequently Asked Questions
- Does Cypress take screenshots on every test failure by default? Yes, it does, unless you set
screenshotOnRunFailure: falsein your config. - Can I change the screenshot format to PNG? Yes, set
screenshotQuality: 0– Cypress will then use PNG instead of JPEG. - How do I capture a specific element instead of the whole page? Use
cy.get('selector').screenshot('element-name')– this captures only that element. - Why are my screenshots showing a blank viewport? This can happen if the test fails during page load. Try adding a
cy.wait()or usecy.intercept()to wait for network requests to finish. - 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. - 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.