bootstrap之ScrollTo
2024-09-30 07:39:16
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。
最新文章
- 初识Mybatis框架,实现增删改查等操作(动态拼接和动态修改)
- MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案
- ffmpeg解码
- hdu 2049 不容易系列之(4)——考新郎
- 第 2 章 代理模式【Proxy Pattern】
- js 异步流程控制之 avQ(avril.queue)
- 识别Andriod APK签名证书类型
- 使用Three.js渲染Sketchup导出的dae
- Qt5:无边框窗口拖动
- java历史版本下载地址
- tp5的phpword使用
- C# SharpMap的简单使用
- Dubbo与Zookeeper在Window上的安装与简单使用
- 1.3.8、CDH 搭建Hadoop在安装之前(端口---Apache Flume和Apache Solr使用的端口)
- SQL Server错误处理
- Shimmer辉光动画效果
- 接Window服务(二)
- December 19th 2016 Week 52nd Sunday
- Android 之 GridView具体解释
- Ubuntu下编译C语言程序(同时给编译生成的文件命名)
热门文章
- 剑指Offer(书):二叉树的镜像
- Spring中线程池的使用
- LeetCode(121) Best Time to Buy and Sell Stock
- 【HIHOCODER 1323】回文字符串(区间DP)
- Knockout v3.4.0 中文版教程-7-计算监控-依赖跟踪如何工作
- loj2141 「SHOI2017」期末考试
- 图论trainning-part-1 G. Stockbroker Grapevine
- [uiautomator篇]recent
- IIS中如何应用程序启用https协议
- 转自kuangbin的AC自动机(赛前最后一博)