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。

最新文章

  1. 初识Mybatis框架,实现增删改查等操作(动态拼接和动态修改)
  2. MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案
  3. ffmpeg解码
  4. hdu 2049 不容易系列之(4)——考新郎
  5. 第 2 章 代理模式【Proxy Pattern】
  6. js 异步流程控制之 avQ(avril.queue)
  7. 识别Andriod APK签名证书类型
  8. 使用Three.js渲染Sketchup导出的dae
  9. Qt5:无边框窗口拖动
  10. java历史版本下载地址
  11. tp5的phpword使用
  12. C# SharpMap的简单使用
  13. Dubbo与Zookeeper在Window上的安装与简单使用
  14. 1.3.8、CDH 搭建Hadoop在安装之前(端口---Apache Flume和Apache Solr使用的端口)
  15. SQL Server错误处理
  16. Shimmer辉光动画效果
  17. 接Window服务(二)
  18. December 19th 2016 Week 52nd Sunday
  19. Android 之 GridView具体解释
  20. Ubuntu下编译C语言程序(同时给编译生成的文件命名)

热门文章

  1. 剑指Offer(书):二叉树的镜像
  2. Spring中线程池的使用
  3. LeetCode(121) Best Time to Buy and Sell Stock
  4. 【HIHOCODER 1323】回文字符串(区间DP)
  5. Knockout v3.4.0 中文版教程-7-计算监控-依赖跟踪如何工作
  6. loj2141 「SHOI2017」期末考试
  7. 图论trainning-part-1 G. Stockbroker Grapevine
  8. [uiautomator篇]recent
  9. IIS中如何应用程序启用https协议
  10. 转自kuangbin的AC自动机(赛前最后一博)