I'm writing Cypress-tests to a site, that has several modals (newsletter signups inside iframe), popups ("Remember free shipping if X, Y and Z") and overlays.
These elements are quite the problem, since it makes it impossible to 'click field A' and fill it out with text: abcd
. Since the overlays opens on top of the whole thing.
Overarching question: How do I get rid of these modals, popups and overlays, so I can interact with the page?
Solution attempt 1: Inject a stylesheet in beforeEach
, with display: none;
I imagined something like this:
cy.get('head').then( (head) => {
let gottenHead = document.head;
let styles = document.createElement('style');
styles.type = 'text/css';
styles.innerHTML = '.iframes-container { display: none !important; }'
gottenHead.appendChild( styles ); // Doesnt work,
head.appendChild( styles ); // Doesnt work
});
Or [this gist here], but that doesn't work either, throwing the error: "Cannot read properties of undefined (reading 'appendChild')" for this line: doc.body.head.appendChild($style);
.
Solution attempt 2:
Adding a mutation observer, checking if any of the wrapping elements gets added to the dom. And if they do - remove them again.
This just again, doesn't seem like 'The Cypress Way'. So I haven't tried this yet.
Solution attempt 3: Figure out when they open and click 'close' on them
This seems like the most organic approach. But I'd actually rather have a lazy option, that simply hides them all, whenever Cypress is running.
But the problem with this is that the 'close'-button is inside an iframe, which Cypress doesn't like interacting with.
Other info
I checked Cypress' documentation, but couldn't find any guidelines on how to bypass these modals and popups, weirdly enough.
Update1: Agoff asked what keeps track of, if the modal/popups should show. I tried checking if a cookie was set, a session variable or some local storage. But I couldn't see anything being added, after 'clicking them away'. :-/