title: In Xcode UI Test, how can I repeatedly check if an element exists and if so perform an action?
body: <p>I’m implementing UI tests. The app makes API calls that could make alerts ( it's a UIView attached to the window ) appear. Of course, these are random/not predictable. If they show up, I have to dismiss them (clicking on the close button). Any idea how to do this? Do I have some event that says that something happened on the UI? I was thinking to have a thread that executes every 0.5 seconds that checks if the dismiss button exists and if so I tap on it.</p>
  
  <pre><code>        DispatchQueue.global().async {
          while true
          {
              DispatchQueue.main.async {
                  if(self.app.buttons["NotificationCloseButton"].exists)
                  {
                      self.app.buttons["NotificationCloseButton"].tap()
                  }
              }
              sleep(5)
          }
      }
  </code></pre>
  
  <p>The problem with this is that it causes random crashes: <code>Neither attributes nor error returned</code></p>