Preventing cookie banners with Cypress

If your company has one of these cookie banners and you use Cypress for testing, you might have had issues with your tests failing because the banner covers the page. Here is a super small article to explain how to work around that problem.
In most cas…


This content originally appeared on Kitty Giraudel and was authored by Kitty Giraudel

If your company has one of these cookie banners and you use Cypress for testing, you might have had issues with your tests failing because the banner covers the page. Here is a super small article to explain how to work around that problem.

In most cases, the way a cookie banner works is that it renders the banner, and when the user interacts with it, it sets a value in a cookie so next page loads do not render the banner again.

We can set that cookie before loading any page thanks to a Cypress event.

In the code below, replace the value of the two main constants with the way it works for your website, and add this code snippet in Cypress “support file” (defaults to cypress/support/index.js).

// The name of the cookie holding whether the user has accepted
// the cookie policy
const COOKIE_NAME = "cookie_notice";
// The value meaning that user has accepted the cookie policy
const COOKIE_VALUE = "ACCEPTED";

Cypress.on("window:before:load", window => {
window.document.cookie = `${COOKIE_NAME}=${COOKIE_VALUE}`;
});

If your code relies on Local Storage instead of cookies to store consent, the concept is exactly the same.


This content originally appeared on Kitty Giraudel and was authored by Kitty Giraudel


Print Share Comment Cite Upload Translate Updates
APA

Kitty Giraudel | Sciencx (2020-03-15T00:00:00+00:00) Preventing cookie banners with Cypress. Retrieved from https://www.scien.cx/2020/03/15/preventing-cookie-banners-with-cypress-2/

MLA
" » Preventing cookie banners with Cypress." Kitty Giraudel | Sciencx - Sunday March 15, 2020, https://www.scien.cx/2020/03/15/preventing-cookie-banners-with-cypress-2/
HARVARD
Kitty Giraudel | Sciencx Sunday March 15, 2020 » Preventing cookie banners with Cypress., viewed ,<https://www.scien.cx/2020/03/15/preventing-cookie-banners-with-cypress-2/>
VANCOUVER
Kitty Giraudel | Sciencx - » Preventing cookie banners with Cypress. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/03/15/preventing-cookie-banners-with-cypress-2/
CHICAGO
" » Preventing cookie banners with Cypress." Kitty Giraudel | Sciencx - Accessed . https://www.scien.cx/2020/03/15/preventing-cookie-banners-with-cypress-2/
IEEE
" » Preventing cookie banners with Cypress." Kitty Giraudel | Sciencx [Online]. Available: https://www.scien.cx/2020/03/15/preventing-cookie-banners-with-cypress-2/. [Accessed: ]
rf:citation
» Preventing cookie banners with Cypress | Kitty Giraudel | Sciencx | https://www.scien.cx/2020/03/15/preventing-cookie-banners-with-cypress-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.