UIAutomator中滚动ListView获得目标TextView控件对象的经验点滴

当创建一个UiScrollable对象时,如果指定的参数是new UiSelector().scrollable(true),那么会出现以下问题

  • 当可滚动控件(比如ListView)不满一页不需要滚动时,创建的UiSrollable对象返回值是为空的。
所以以下代码是错误的:
  1. //Find out the new added note entry  
  2.      UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true));  //would be null if the scrollable widget's not more than one page  
  3.      UiObject note = null;  
  1. note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1"true);    
  1. <pre name="code" class="java">     assertThat(note,notNullValue());  
note.longClick();
我们可以做一个增强,当判断返回的UiScrollable对象是空的时候,我们直接去当前页面查找目标控件:
  1. //Find out the new added note entry  
  2.      UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true));   
  3.      UiObject note = null;  
  1. if(noteList.exists()) {  
  2.     note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1"true);    
  3. }  
  4. else {  
  5.     note = new UiObject(new UiSelector().text("Note1"));  
  6. }  
  7. assertThat(note,notNullValue());  
  8.   
  9. note.longClick();  
另外一个个人认为更好的解决办法是,不要以“UiSelector().scrollable(true)”来初始化UiScrollable对象,而是明确的指定className为“android.widget.ListView"来初始化UiScrollable对象。实践证明这样子做的话 就算ListView的内容很少不需要翻页时,也能够找到指定的当前页面的目标控件
  1. //Find out the new added note entry  
  2.      UiScrollable noteList = new UiScrollable( new UiSelector().className("android.widget.ListView"));    
  3.      UiObject note = null;  
  4.        
  5.      note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1"true);    
  6.      assertThat(note,notNullValue());  
  7.        
  8.      note.longClick();  

Item

Description

Warning

Author

天地会珠海分舵

转载请注明出处!

更多精彩文章请查看本人博客!

Blog Address

http://blog.csdn.net/zhubaitian


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值