首页 > 代码库 > bootstrap之ScrollTo

bootstrap之ScrollTo

ScrollTo


package io.appium.android.bootstrap.handler;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import io.appium.android.bootstrap.*;
import org.json.JSONException;

import java.util.Hashtable;

/**
 * This handler is used to scroll to elements in the Android UI.
 * 
 * Based on the element Id of the scrollable, scroll to the object with the
 * text.
 * 
 */
public class ScrollTo extends CommandHandler {

  /*
   * @param command The {@link AndroidCommand}
   * 
   * @return {@link AndroidCommandResult}
   * 
   * @throws JSONException
   * 
   * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
   * bootstrap.AndroidCommand)
   */
  @Override
  public AndroidCommandResult execute(final AndroidCommand command)
      throws JSONException {
    if (!command.isElementCommand()) {
      return getErrorResult("A scrollable view is required for this command.");
    }

    try {
      Boolean result;
      final Hashtable<String, Object> params = command.params();
      final String text = params.get("text").toString();
      final String direction = params.get("direction").toString();

      final AndroidElement el = command.getElement();

      if (!el.getUiObject().isScrollable()) {
        return getErrorResult("The provided view is not scrollable.");
      }

      final UiScrollable view = new UiScrollable(el.getUiObject().getSelector());

      if (direction.toLowerCase().contentEquals("horizontal")
          || view.getClassName().contentEquals(
              "android.widget.HorizontalScrollView")) {
        view.setAsHorizontalList();
      }
      view.scrollToBeginning(100);
      view.setMaxSearchSwipes(100);
      result = view.scrollTextIntoView(text);
      view.waitForExists(5000);

      // make sure we can get to the item
      UiObject listViewItem = view.getChildByInstance(
          new UiSelector().text(text), 0);

      // We need to make sure that the item exists (visible)
      if (!(result && listViewItem.exists())) {
        return getErrorResult("Could not scroll element into view: " + text);
      }
      return getSuccessResult(result);
    } catch (final UiObjectNotFoundException e) {
      return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
    } catch (final NullPointerException e) { // el is null
      return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
    } catch (final Exception e) {
      return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
    }
  }
}

在uiautomator中有时候需要在一个滚动的list中找到某一个item,而这个item的位置又不定,这个时候我们可以通过UiScrollable的scrollTo来找到特定text的控件。而bootstrap的这个ScrollTo就是封装这样一种需求的。


首先判断控件是否是可以滚动的,然后创建UiScrollable对象,因为默认的滚动方式是垂直方向的,如果需要的是水平方向的的话,还要设置方向为水平。


view.setAsHorizontalList();


然后将滚动控件滚到最开始的地方,然后设置最大的搜索范围为100次,因为不可能永远搜索下去。然后开始调用


view.scrollTextIntoView(text);


开始滚动,最后确认是否滚动到制定目标:


 view.waitForExists(5000);

因为有可能有刷新到时间,所以调用方法到时候传入了时间5秒钟。


UiObject listViewItem = view.getChildByInstance(
          new UiSelector().text(text), 0);

最后检查结果,获取制定text的对象,判断其是否存在然后返回相应结果给客户端。




bootstrap之ScrollTo