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的对象,推断其是否存在然后返回对应结果给client。





转载于:https://www.cnblogs.com/yxysuanfa/p/6875783.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值