码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-24 09:40:56      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:uiautomator

当创建一个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();



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

标签:uiautomator

原文地址:http://blog.csdn.net/zhubaitian/article/details/39509181

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!