Remove Cookie Banners from Cypress Screenshots
In this article
Cookie banners often appear in Cypress screenshots, cluttering visual regression tests and documentation. This guide shows you how to hide or remove them reliably using Cypress's built-in commands, CSS injection, or network stubbing—so your screenshots stay clean and consistent. You'll need a working Cypress setup and basic familiarity with your app's cookie banner selector.
What You'll Need
- A Cypress project (v10 or later recommended)
- Access to your application's DOM (to inspect the cookie banner element)
- Basic knowledge of CSS selectors and Cypress commands
Step-by-Step
- Identify the cookie banner selector. Open your app in a browser, right-click the cookie banner, and choose "Inspect." Note the element's ID, class, or data attribute. For example,
#cookie-banneror.cookie-consent. If the banner appears only after a delay, note that too. - Add a custom Cypress command (recommended). In your
cypress/support/commands.jsfile, add a command that hides the banner before taking a screenshot. Example:
This checks if the banner exists and hides it. CallCypress.Commands.add('hideCookieBanner', () => { cy.get('body').then(($body) => { if ($body.find('#cookie-banner').length) { cy.get('#cookie-banner').invoke('css', 'display', 'none'); } }); });cy.hideCookieBanner()beforecy.screenshot(). - Inject CSS to hide the banner globally. In your
cypress/support/e2e.jsfile, add a style tag that hides known banner selectors. Example:
This runs before each test, so the banner never appears in screenshots. Be careful not to hide elements that are essential for the test.cy.document().then((doc) => { const style = doc.createElement('style'); style.innerHTML = '#cookie-banner, .cookie-consent { display: none !important; }'; doc.head.appendChild(style); }); - Use network stubbing to block the banner's script. If the banner loads via a third-party script, you can intercept the request in your test. Example:
Place this before visiting the page. This prevents the banner from loading entirely. You'll need the URL pattern of the script.cy.intercept('**/cookie-banner.js', { body: '' }); - Dismiss the banner via UI interaction. If the banner has a close button, click it before taking a screenshot:
This works if the banner's dismissal is fast and doesn't cause side effects. Add acy.get('#cookie-banner .accept-button').click(); cy.screenshot();cy.wait(500)if the animation needs time. - Take the screenshot after hiding. Always call your hide method or CSS injection right before
cy.screenshot(). For example:
If you use the CSS injection incy.visit('/'); cy.hideCookieBanner(); cy.screenshot('homepage');e2e.js, you don't need to call anything per test. - Verify the screenshot. Open the screenshot from
cypress/screenshotsto confirm the banner is gone. If it's still there, check your selector and timing.
Common Mistakes
- Using a selector that's too broad. Hiding
bodyordivwill break the page. Use the most specific selector for the banner. - Hiding the banner before the page fully loads. If the banner is injected dynamically after your hide command, it will reappear. Use
cy.waitor the CSS injection method that runs after DOM ready. - Forgetting to re-enable the banner for other tests. If you hide it globally, some tests might need the banner to test its functionality. Use a conditional or a separate test suite.
- Blocking the wrong network request. If the banner loads from a CDN, the URL might be different. Check the network tab to get the exact request.
Tips
- Use a data attribute for the banner. If you control the app, add
data-cy="cookie-banner"to the element. This makes your selectors resilient to style changes. - Combine methods. Use CSS injection as a fallback and UI click as a primary if the banner has a functional requirement.
- Set a viewport size. Some banners only appear on certain screen sizes. Use
cy.viewport(1280, 720)to standardize your screenshots. - Test the hide command in isolation. Run a single test with
cy.hideCookieBanner()to ensure it works before adding it to all tests.
Troubleshooting
- Banner still appears in screenshot. Check if the banner is inside an iframe. If so, you need to access the iframe's content with
cy.iframe()(requires a plugin) or use the CSS injection method on the iframe's document. - Hide command works but banner reappears after navigation. If your test navigates to a new page, the banner may reload. Call the hide command again after each navigation, or use the global CSS injection in
e2e.js. - CSS injection doesn't work. Some apps use inline styles with
!important. Override withdisplay: none !importantas well. Or try settingvisibility: hiddenandheight: 0. - Network stubbing blocks other scripts. Make your URL pattern specific, e.g.,
**/cookie-banner.jsinstead of**/*.js.
Frequently Asked Questions
Can I remove cookie banners for all screenshots automatically?
Yes, use the CSS injection method in cypress/support/e2e.js. It runs before each test, so every screenshot will be clean.
What if the cookie banner is a shadow DOM element?
You need to pierce the shadow root using cy.get('selector', { includeShadowDom: true }) or use the CSS injection with a shadow-piercing selector like ::part() if available.
Will hiding the banner affect my test's functionality?
Only if your test interacts with the banner. If not, hiding it is safe. For tests that need the banner, use a conditional check or a separate test file.
How do I handle a banner that appears after a delay?
Use cy.wait(1000) before hiding, or use the CSS injection method which applies immediately after DOM ready.
Can I use this with Cypress Cloud or visual regression tools?
Yes, the screenshots generated will be clean, so any visual regression tool will see consistent images. Just ensure your hide method runs before the screenshot command.
Is there a way to remove the banner only for screenshots, not for the test run?
Yes, call the hide command only right before cy.screenshot(). The banner will remain visible during other test steps.