1package io.appium.droiddriver.helpers;
2
3import io.appium.droiddriver.DroidDriver;
4import io.appium.droiddriver.Poller.PollingListener;
5import io.appium.droiddriver.exceptions.ElementNotFoundException;
6import io.appium.droiddriver.finders.Finder;
7
8/**
9 * Static utility methods to create commonly used PollingListeners.
10 */
11public class PollingListeners {
12  /**
13   * Tries to find {@code watchFinder}, and clicks it if found.
14   *
15   * @param driver a DroidDriver instance
16   * @param watchFinder Identifies the UI component to watch
17   * @return whether {@code watchFinder} is found
18   */
19  public static boolean tryFindAndClick(DroidDriver driver, Finder watchFinder) {
20    try {
21      driver.find(watchFinder).click();
22      return true;
23    } catch (ElementNotFoundException enfe) {
24      return false;
25    }
26  }
27
28  /**
29   * Returns a new {@code PollingListener} that will look for
30   * {@code watchFinder}, then click {@code dismissFinder} to dismiss it.
31   * <p>
32   * Typically a {@code PollingListener} is used to dismiss "random" dialogs. If
33   * you know the certain situation when a dialog is displayed, you should deal
34   * with the dialog in the specific situation instead of using a
35   * {@code PollingListener} because it is checked in all polling events, which
36   * occur frequently.
37   * </p>
38   *
39   * @param watchFinder Identifies the UI component, for example an AlertDialog
40   * @param dismissFinder Identifies the UiElement to click on that will dismiss
41   *        the UI component
42   */
43  public static PollingListener newDismissListener(final Finder watchFinder,
44      final Finder dismissFinder) {
45    return new PollingListener() {
46      @Override
47      public void onPolling(DroidDriver driver, Finder finder) {
48        if (driver.has(watchFinder)) {
49          driver.find(dismissFinder).click();
50        }
51      }
52    };
53  }
54
55  private PollingListeners() {}
56}
57