aboutsummaryrefslogtreecommitdiff
path: root/src/io/appium/droiddriver/helpers/PollingListeners.java
blob: 2508fdfbb2de3185c9aad90a0fac3b02676b7490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package io.appium.droiddriver.helpers;

import io.appium.droiddriver.DroidDriver;
import io.appium.droiddriver.Poller.PollingListener;
import io.appium.droiddriver.exceptions.ElementNotFoundException;
import io.appium.droiddriver.finders.Finder;

/**
 * Static utility methods to create commonly used PollingListeners.
 */
public class PollingListeners {
  /**
   * Tries to find {@code watchFinder}, and clicks it if found.
   *
   * @param driver a DroidDriver instance
   * @param watchFinder Identifies the UI component to watch
   * @return whether {@code watchFinder} is found
   */
  public static boolean tryFindAndClick(DroidDriver driver, Finder watchFinder) {
    try {
      driver.find(watchFinder).click();
      return true;
    } catch (ElementNotFoundException enfe) {
      return false;
    }
  }

  /**
   * Returns a new {@code PollingListener} that will look for
   * {@code watchFinder}, then click {@code dismissFinder} to dismiss it.
   * <p>
   * Typically a {@code PollingListener} is used to dismiss "random" dialogs. If
   * you know the certain situation when a dialog is displayed, you should deal
   * with the dialog in the specific situation instead of using a
   * {@code PollingListener} because it is checked in all polling events, which
   * occur frequently.
   * </p>
   *
   * @param watchFinder Identifies the UI component, for example an AlertDialog
   * @param dismissFinder Identifies the UiElement to click on that will dismiss
   *        the UI component
   */
  public static PollingListener newDismissListener(final Finder watchFinder,
      final Finder dismissFinder) {
    return new PollingListener() {
      @Override
      public void onPolling(DroidDriver driver, Finder finder) {
        if (driver.has(watchFinder)) {
          driver.find(dismissFinder).click();
        }
      }
    };
  }

  private PollingListeners() {}
}