当创建一个UiScrollable对象时,如果指定的参数是new UiSelector().scrollable(true),那么会出现以下问题
- 当可滚动控件(比如ListView)不满一页不需要滚动时,创建的UiSrollable对象返回值是为空的。
所以以下代码是错误的:
//Find out the new added note entry UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true)); //would be null if the scrollable widget's not more than one page UiObject note = null;
note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1", true);
<pre name="code" class="java"> assertThat(note,notNullValue());
note.longClick();
我们可以做一个增强,当判断返回的UiScrollable对象是空的时候,我们直接去当前页面查找目标控件:
//Find out the new added note entry UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true)); UiObject note = null;
if(noteList.exists()) { note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1", true); } else { note = new UiObject(new UiSelector().text("Note1")); } assertThat(note,notNullValue()); note.longClick();
另外一个个人认为更好的解决办法是,不要以“UiSelector().scrollable(true)”来初始化UiScrollable对象,而是明确的指定className为“android.widget.ListView"来初始化UiScrollable对象。实践证明这样子做的话
就算ListView的内容很少不需要翻页时,也能够找到指定的当前页面的目标控件。
//Find out the new added note entry UiScrollable noteList = new UiScrollable( new UiSelector().className("android.widget.ListView")); UiObject note = null; note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1", true); assertThat(note,notNullValue()); note.longClick();
作者 | 自主博客 | 微信 | CSDN |
天地会珠海分舵 | | 服务号:TechGoGoGo 扫描码:
| ©著作权归作者所有:来自51CTO博客作者zhukev的原创作品,如需转载,请注明出处,否则将追究法律责任 0 收藏 猜你喜欢
我的友情链接
最新HTML BroadcastChannel API引荐
Matrix源码分析之第一篇
使用LiveDataBus进行组件通信,让你永无后顾之忧
React Native集成到Android项目当中
面试经历:记一次Android面试
Android:实现模块 api 化
在Android中集成flutter
带你深入理解Android中的自定义属性!!!
程序员笔记|JSBridge框架解决通信问题实现移动端跨平台开发
Java注解实战之APT构建模块化的第一步
怎么让软键盘弹出时,部分控件上移
MVVM 架构与数据绑定库
Android自定义Dialog及与Activity的交互
开发十年,就只剩下这套架构体系了!
Flutter交互实战-即刻App探索页下拉&拖拽效果
Fragments片段
扫一扫,领取大礼包 |
转载于:https://blog.51cto.com/techgogogo/1608442
Ctrl+Enter 发布
发布
取消