title: Recall method in cypress until a button is no longer visible
body: <p>How can I write the method below from cypress so that the &quot;show more upcoming&quot; button is only pressed while it is visible. At this time the method causes the button to be pressed even after it is no longer visible and automatically generates an error. I've tried to use a kind of recursive function.</p>
  <p>I've also tried that intercept to always load the appointment list, but I'm not sure it's necessarily useful.</p>
  <p>I hope I've provided all the details, thank you.</p>
  <pre><code>  checkVisibilityUpcoming(): void {
          cy.get(&quot;.mat-flat-button.mat-primary&quot;).then($button =&gt; {
              if($button.is(':visible')) {
                  cy.intercept('https://apilink').as('upcoming')
                  cy.contains('Show more upcoming').click();
                  cy.wait('@upcoming')
                  this.checkVisibilityUpcoming()
              }
          })
  }
  </code></pre>