zoukankan      html  css  js  c++  java
  • 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。





  • 相关阅读:
    可变性编程 不可变性编程 可变性变量 不可变性变量 并发编程 命令式编程 函数式编程
    hashable
    优先采用面向表达式编程
    内存转储文件 Memory.dmp
    windows update 文件 路径
    tmp
    查询局域网内全部电脑IP和mac地址等信息
    iptraf 网卡 ip 端口 监控 netstat 关闭端口方法
    Error 99 connecting to 192.168.3.212:6379. Cannot assign requested address
    t
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6875783.html
Copyright © 2011-2022 走看看