Esempio n. 1
0
  /**
   * Waits for a given view.
   *
   * @param view the view to wait for
   * @param timeout the amount of time in milliseconds to wait
   * @param scroll {@code true} if scrolling should be performed
   * @param checkIsShown {@code true} if view.isShown() should be used
   * @return {@code true} if view is shown and {@code false} if it is not shown before the timeout
   */
  public View waitForView(View view, int timeout, boolean scroll, boolean checkIsShown) {
    long endTime = SystemClock.uptimeMillis() + timeout;
    int retry = 0;

    if (view == null) return null;

    while (SystemClock.uptimeMillis() < endTime) {

      final boolean foundAnyMatchingView = searcher.searchFor(view);

      if (checkIsShown && foundAnyMatchingView && !view.isShown()) {
        sleeper.sleepMini();
        retry++;

        View identicalView = viewFetcher.getIdenticalView(view);
        if (identicalView != null && !view.equals(identicalView)) {
          view = identicalView;
        }

        if (retry > 5) {
          return view;
        }
        continue;
      }

      if (foundAnyMatchingView) {
        return view;
      }

      if (scroll) scroller.scrollDown();

      sleeper.sleep();
    }
    return view;
  }
Esempio n. 2
0
  /**
   * Waits for a text to be shown.
   *
   * @param classToFilterBy the class to filter by
   * @param text the text that needs to be shown, specified as a regular expression.
   * @param expectedMinimumNumberOfMatches the minimum number of matches of text that must be shown.
   *     {@code 0} means any number of matches
   * @param timeout the amount of time in milliseconds to wait
   * @param scroll {@code true} if scrolling should be performed
   * @param onlyVisible {@code true} if only visible text views should be waited for
   * @param hardStoppage {@code true} if search is to be stopped when timeout expires
   * @return {@code true} if text is found and {@code false} if it is not found before the timeout
   */
  public <T extends TextView> T waitForText(
      Class<T> classToFilterBy,
      String text,
      int expectedMinimumNumberOfMatches,
      long timeout,
      boolean scroll,
      boolean onlyVisible,
      boolean hardStoppage) {
    final long endTime = SystemClock.uptimeMillis() + timeout;

    while (true) {
      final boolean timedOut = SystemClock.uptimeMillis() > endTime;
      if (timedOut) {
        return null;
      }

      sleeper.sleep();

      if (!hardStoppage) timeout = 0;

      final T textViewToReturn =
          searcher.searchFor(
              classToFilterBy, text, expectedMinimumNumberOfMatches, timeout, scroll, onlyVisible);

      if (textViewToReturn != null) {
        return textViewToReturn;
      }
    }
  }
Esempio n. 3
0
  /**
   * @summary Swaps to a window with a specified title after waiting a specified number of seconds
   *     for the window to display. Pass in a number of seconds (1,2 etc), and the title of the
   *     window you wish to switch to. Do not need to pass in the whole title, can be a part of it
   *     such as "Lilo". Can also pass in a null value ("") if the window does not have a title.
   *     Returns a true if the window was found & switched to and false if not
   * @version Created 10/29/2014
   * @author Jessica Marshall
   * @param driver, title, numOfWaitSeconds
   * @throws NA
   * @return true/false
   */
  public static boolean swapToWindowWithTimeout(
      WebDriver driver, String title, int numOfWaitSeconds) {
    int count = 0;
    // wait for the window count to be greater than 1
    while (driver.getWindowHandles().size() == 1) {
      Sleeper.sleep(1000);

      if (count > numOfWaitSeconds) return false;
      count++;
    }

    for (String winHandle : driver.getWindowHandles()) {

      driver.switchTo().window(winHandle);
      // code to handle if the title of the window you expect to switch to
      // is null
      if (title.equals("")) {
        if (driver.getTitle().toUpperCase().equals("")) return true;
      }
      // code to handle a non null tile of the winodw you wish to switch
      // to
      if (driver.getTitle().toUpperCase().contains(title.toUpperCase())) {
        return true;
      }
    }

    return false;
  }
Esempio n. 4
0
 public void poll() throws TimeoutException {
   long startTime = System.currentTimeMillis();
   while (!condition.validate() && getDurationSince(startTime) < timeout) {
     Sleeper.sleep(pollingInterval);
   }
   if (getDurationSince(startTime) >= timeout) {
     throw condition.exceptionToThrowAfterTimeout();
   }
 }
Esempio n. 5
0
  /**
   * Scrolls a list.
   *
   * @param absListView the list to be scrolled
   * @param direction the direction to be scrolled
   * @param allTheWay {@code true} to scroll the view all the way up or down, {@code false} to
   *     scroll one page up or down
   * @return {@code true} if more scrolling can be done
   */
  public <T extends AbsListView> boolean scrollList(
      T absListView, int direction, boolean allTheWay) {

    if (absListView == null) {
      return false;
    }

    if (direction == DOWN) {

      int listCount = absListView.getCount();
      int lastVisiblePosition = absListView.getLastVisiblePosition();

      if (allTheWay) {
        scrollListToLine(absListView, listCount - 1);
        return false;
      }

      if (lastVisiblePosition >= listCount - 1) {
        if (lastVisiblePosition > 0) {
          scrollListToLine(absListView, lastVisiblePosition);
        }
        return false;
      }

      int firstVisiblePosition = absListView.getFirstVisiblePosition();

      if (firstVisiblePosition != lastVisiblePosition)
        scrollListToLine(absListView, lastVisiblePosition);
      else scrollListToLine(absListView, firstVisiblePosition + 1);

    } else if (direction == UP) {
      int firstVisiblePosition = absListView.getFirstVisiblePosition();

      if (allTheWay || firstVisiblePosition < 2) {
        scrollListToLine(absListView, 0);
        return false;
      }
      int lastVisiblePosition = absListView.getLastVisiblePosition();

      final int lines = lastVisiblePosition - firstVisiblePosition;

      int lineToScrollTo = firstVisiblePosition - lines;

      if (lineToScrollTo == lastVisiblePosition) lineToScrollTo--;

      if (lineToScrollTo < 0) lineToScrollTo = 0;

      scrollListToLine(absListView, lineToScrollTo);
    }
    sleeper.sleep();
    return true;
  }
Esempio n. 6
0
  /**
   * Waits for a log message to appear. Requires read logs permission (android.permission.READ_LOGS)
   * in AndroidManifest.xml of the application under test.
   *
   * @param logMessage the log message to wait for
   * @param timeout the amount of time in milliseconds to wait
   * @return true if log message appears and false if it does not appear before the timeout
   */
  public boolean waitForLogMessage(String logMessage, int timeout) {
    StringBuilder stringBuilder = new StringBuilder();

    long endTime = SystemClock.uptimeMillis() + timeout;
    while (SystemClock.uptimeMillis() <= endTime) {

      if (getLog(stringBuilder).lastIndexOf(logMessage) != -1) {
        return true;
      }
      sleeper.sleep();
    }
    return false;
  }
Esempio n. 7
0
  /**
   * Waits for the given {@link Activity}.
   *
   * @param activityClass the class of the {@code Activity} to wait for
   * @param timeout the amount of time in milliseconds to wait
   * @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it
   *     does not
   */
  public boolean waitForActivity(Class<? extends Activity> activityClass, int timeout) {
    Activity currentActivity = activityUtils.getCurrentActivity(false, false);
    final long endTime = SystemClock.uptimeMillis() + timeout;

    while (SystemClock.uptimeMillis() < endTime) {
      if (currentActivity != null && currentActivity.getClass().equals(activityClass)) {
        return true;
      }

      sleeper.sleep(MINISLEEP);
      currentActivity = activityUtils.getCurrentActivity(false, false);
    }
    return false;
  }
Esempio n. 8
0
  /**
   * Waits for a condition to be satisfied.
   *
   * @param condition the condition to wait for
   * @param timeout the amount of time in milliseconds to wait
   * @return {@code true} if condition is satisfied and {@code false} if it is not satisfied before
   *     the timeout
   */
  public boolean waitForCondition(Condition condition, int timeout) {
    final long endTime = SystemClock.uptimeMillis() + timeout;

    while (true) {
      final boolean timedOut = SystemClock.uptimeMillis() > endTime;
      if (timedOut) {
        return false;
      }

      sleeper.sleep();

      if (condition.isSatisfied()) {
        return true;
      }
    }
  }
Esempio n. 9
0
  /**
   * Waits for a view to be shown.
   *
   * @param viewClass the {@code View} class to wait for
   * @param index the index of the view that is expected to be shown.
   * @param timeout the amount of time in milliseconds to wait
   * @param scroll {@code true} if scrolling should be performed
   * @return {@code true} if view is shown and {@code false} if it is not shown before the timeout
   */
  public <T extends View> boolean waitForView(
      final Class<T> viewClass, final int index, final int timeout, final boolean scroll) {
    Set<T> uniqueViews = new HashSet<T>();
    final long endTime = SystemClock.uptimeMillis() + timeout;
    boolean foundMatchingView;

    while (SystemClock.uptimeMillis() < endTime) {
      sleeper.sleep();

      foundMatchingView = searcher.searchFor(uniqueViews, viewClass, index);

      if (foundMatchingView) return true;

      if (scroll) scroller.scrollDown();
    }
    return false;
  }
Esempio n. 10
0
  /**
   * Waits for the given {@link Activity}.
   *
   * @param name the name of the {@code Activity} to wait for e.g. {@code "MyActivity"}
   * @param timeout the amount of time in milliseconds to wait
   * @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it
   *     does not
   */
  public boolean waitForActivity(String name, int timeout) {
    Activity currentActivity = activityUtils.getCurrentActivity(false, false);
    final long endTime = SystemClock.uptimeMillis() + timeout;

    while (SystemClock.uptimeMillis() < endTime) {
      if (currentActivity != null && currentActivity.getClass().getSimpleName().equals(name)) {
        return true;
      }

      if (activityUtils.getCurrentActivityName().contains(name)) {
        return true;
      }

      sleeper.sleep(MINISLEEP);
      currentActivity = activityUtils.getCurrentActivity(false, false);
    }
    return false;
  }
Esempio n. 11
0
  /**
   * Waits for a view to be shown.
   *
   * @param viewClass the {@code View} class to wait for
   * @param index the index of the view that is expected to be shown
   * @param sleep true if should sleep
   * @param scroll {@code true} if scrolling should be performed
   * @return {@code true} if view is shown and {@code false} if it is not shown before the timeout
   */
  public <T extends View> boolean waitForView(
      final Class<T> viewClass, final int index, boolean sleep, boolean scroll) {
    Set<T> uniqueViews = new HashSet<T>();
    boolean foundMatchingView;

    while (true) {

      if (sleep) sleeper.sleep();

      foundMatchingView = searcher.searchFor(uniqueViews, viewClass, index);

      if (foundMatchingView) return true;

      if (scroll && !scroller.scrollDown()) return false;

      if (!scroll) return false;
    }
  }
Esempio n. 12
0
  /**
   * Waits for two views to be shown.
   *
   * @param scrollMethod {@code true} if it's a method used for scrolling
   * @param classes the classes to wait for
   * @return {@code true} if any of the views are shown and {@code false} if none of the views are
   *     shown before the timeout
   */
  public <T extends View> boolean waitForViews(
      boolean scrollMethod, Class<? extends T>... classes) {
    final long endTime = SystemClock.uptimeMillis() + Timeout.getSmallTimeout();

    while (SystemClock.uptimeMillis() < endTime) {

      for (Class<? extends T> classToWaitFor : classes) {
        if (waitForView(classToWaitFor, 0, false, false)) {
          return true;
        }
      }
      if (scrollMethod) {
        scroller.scroll(Scroller.DOWN);
      } else {
        scroller.scrollDown();
      }
      sleeper.sleep();
    }
    return false;
  }
Esempio n. 13
0
  /**
   * Waits for a certain view.
   *
   * @param view the id of the view to wait for
   * @param index the index of the {@link View}. {@code 0} if only one is available
   * @return the specified View
   */
  public View waitForView(int id, int index, int timeout, boolean scroll) {
    Set<View> uniqueViewsMatchingId = new HashSet<View>();
    long endTime = SystemClock.uptimeMillis() + timeout;

    while (SystemClock.uptimeMillis() <= endTime) {
      sleeper.sleep();

      for (View view : viewFetcher.getAllViews(false)) {
        Integer idOfView = Integer.valueOf(view.getId());

        if (idOfView.equals(id)) {
          uniqueViewsMatchingId.add(view);

          if (uniqueViewsMatchingId.size() > index) {
            return view;
          }
        }
      }
      if (scroll) scroller.scrollDown();
    }
    return null;
  }
Esempio n. 14
0
  /**
   * Repeatedly applies this instance's input value to the given function until one of the following
   * occurs:
   *
   * <ol>
   *   <li>the function returns neither null nor false,
   *   <li>the function throws an unignored exception,
   *   <li>the timeout expires,
   *   <li>
   *   <li>the current thread is interrupted
   * </ol>
   *
   * @param isTrue the parameter to pass to the {@link ExpectedCondition}
   * @param <V> The function's expected return type.
   * @return The functions' return value.
   */
  public <V> V until(Function<? super T, V> isTrue) {
    long end = clock.laterBy(timeout.in(MILLISECONDS));
    Throwable lastException = null;
    while (true) {
      try {
        V value = isTrue.apply(input);
        if (value != null && Boolean.class.equals(value.getClass())) {
          if (Boolean.TRUE.equals(value)) {
            return value;
          }
        } else if (value != null) {
          return value;
        }
      } catch (Throwable e) {
        lastException = propagateIfNotIngored(e);
      }

      // Check the timeout after evaluating the function to ensure conditions
      // with a zero timeout can succeed.
      if (!clock.isNowBefore(end)) {
        String toAppend = message == null ? " waiting for " + isTrue.toString() : ": " + message;

        String timeoutMessage =
            String.format("Timed out after %d seconds%s", timeout.in(SECONDS), toAppend);
        if (lastException instanceof RuntimeException || lastException == null) {
          throw timeoutException(timeoutMessage, (RuntimeException) lastException);
        }
        throw timeoutException(timeoutMessage, lastException);
      }

      try {
        sleeper.sleep(interval);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new WebDriverException(e);
      }
    }
  }
Esempio n. 15
0
  /**
   * Waits for a web element.
   *
   * @param by the By object. Examples are By.id("id") and By.name("name")
   * @param minimumNumberOfMatches the minimum number of matches that are expected to be shown.
   *     {@code 0} means any number of matches
   * @param timeout the the amount of time in milliseconds to wait
   * @param scroll {@code true} if scrolling should be performed
   */
  public WebElement waitForWebElement(
      final By by, int minimumNumberOfMatches, int timeout, boolean scroll) {
    final long endTime = SystemClock.uptimeMillis() + timeout;

    while (true) {

      final boolean timedOut = SystemClock.uptimeMillis() > endTime;

      if (timedOut) {
        searcher.logMatchesFound(by.getValue());
        return null;
      }
      sleeper.sleep();

      WebElement webElementToReturn = searcher.searchForWebElement(by, minimumNumberOfMatches);

      if (webElementToReturn != null) return webElementToReturn;

      if (scroll) {
        scroller.scrollDown();
      }
    }
  }
Esempio n. 16
0
 /**
  * Asserts that an expected {@link Activity} is currently active one.
  *
  * @param message the message that should be displayed if the assert fails
  * @param name the name of the {@code Activity} that is expected to be active e.g. {@code
  *     "MyActivity"}
  */
 public void assertCurrentActivity(String message, String name) {
   sleeper.sleep();
   Assert.assertEquals(
       message, name, activityUtils.getCurrentActivity().getClass().getSimpleName());
 }
Esempio n. 17
0
 /**
  * Asserts that an expected {@link Activity} is currently active one.
  *
  * @param message the message that should be displayed if the assert fails
  * @param expectedClass the {@code Class} object that is expected to be active e.g. {@code
  *     MyActivity.class}
  */
 public void assertCurrentActivity(String message, Class<? extends Activity> expectedClass) {
   sleeper.sleep();
   Assert.assertEquals(
       message, expectedClass.getName(), activityUtils.getCurrentActivity().getClass().getName());
 }