code: private double popupMatches(double seconds, String expectedPopupPattern) {
          double end = System.currentTimeMillis() + (seconds * 1000);
          while (!app.get().alert().matches(expectedPopupPattern) && System.currentTimeMillis() < end) ;
          return Math.min((seconds * 1000) - (end - System.currentTimeMillis()), seconds * 1000) / 1000;
      }
docs: Wait for a popup to have the expected text, and then returns the
  amount of time that was waited
  
  @param seconds              - maximum amount of time to wait in seconds
  @param expectedPopupPattern - the expected pattern to wait for
  @return double: the total time waited