10

I need to handle the case where my page may display a popup dialog at the load stage, or it may not appear. Clicking anywhere will remove it and I'm not interested in testing this dialog, but it blocks the page I need to access so it must be removed

This is the code that will get the dialog when it appears

cy.get('.wps_popup')
  .find('[data-wps-popup-close]')
  .click()

but I cannot put that at the top of the test, because this element might not appear.

How do I handle a conditional element - do I need to intercept DOM changes and put that code in the event listener?

Lisa La
  • 157
  • 9
  • Have you answered the question of what triggers the modal to appear? This may help you find an easier answer than polling for the element. – jjhelguero Jul 13 '22 at 15:20
  • @jjhelguero. Yes indeed, it's a special offer coupon for first-time shoppers. Unless I try to hack the A/B testing mechanism which seems OTT, the best solution is the accepted one. – Lisa La Jul 14 '22 at 23:39
  • Sure, if you can somehow avoid that with an API call that would be better. I'm not sure how your app determines a first-time shopper. – jjhelguero Jul 15 '22 at 00:13
  • 1
    Does this answer your question? [How to check for an element that may not exist using Cypress](https://stackoverflow.com/questions/47773525/how-to-check-for-an-element-that-may-not-exist-using-cypress) – TylerH May 31 '23 at 16:36
  • In general conditional assertions is an anti-pattern, it will be easier and more rebust to figure out how to dismiss the popup or prevent it from showing up. e.g. most of those popups set a cookie when they are being dismissed to avoid showing up in the same session. If that's the case you can use https://docs.cypress.io/api/commands/setcookie to set it yourself – Stratos Ion Aug 25 '23 at 15:40

0 Answers0