问题

UI测试时,在同一个界面出现相同的属性的控件(如图),对于这种控件的获取很是无奈。如果直接通过控件id去查找的话总是会返回界面该类型的第一个控件。

解决

1.UiObject2 中已经给出了解决方法,可以通过 getParent()方法处理。缺点:由于UiObjec2t控件与视图进行绑定,当视图变化后该控件对象就被销毁了。所以多次使用则非常不便利

@Test
public void testCase_Btn(){
UiObject2 switchBtn = device.findObject(By.text("Automatic 24‑hour format"))
.getParent().getParent().findObject(By.res("android:id/switch_widget"));
if(switchBtn.isChecked()){
switchBtn.click();
}
assertTrue("switch btn is open", !switchBtn.isChecked());
}

2.UiObject中通过id + instance 去能查找到控件,但是界面变动的话脚本也得变动,可靠性不强。只能采取折中的方式来获取了。不多说,直接上代码。

UiObject switchBnt3 = device.findObject(new UiSelector().resourceId("android:id/switch_widget").instance(2));

测试类:

@Test
public void testCase_Btn() throws UiObjectNotFoundException {
UiObject timeFormat = device.findObject(new UiSelector().text("Automatic 24‑hour format"));
UiObject switchBtn = ControlObj.getUiObject(timeFormat);
if(switchBtn.isChecked()){
switchBtn.click();
}
assertTrue("switch btn is open", !switchBtn.isChecked());
}

帮助类:

package com.zzw.commonutils.commons;

import android.graphics.Rect;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiCollection;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.util.Log; /**
* @author zzw
*/
public class ControlObj {
private static final String TAG = ControlObj.class.getSimpleName(); public static UiObject getUiObject(UiObject obj) throws UiObjectNotFoundException {
UiCollection list = new UiCollection(new UiSelector().resourceId("com.android.settings:id/list"));
UiObject switchBtn = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
.findObject(new UiSelector().resourceId("android:id/switch_widget"));
return getUiObject(list, obj,switchBtn );
} /**
* 获取同行的控件
* @param uic UiCollection
* @param uio The same row UiObject as the target UiObject
* @param uio2 Target UiObject
* @return An UiObject
* @throws UiObjectNotFoundException maybe can't find UiObject
*/
public static UiObject getUiObject(UiCollection uic, UiObject uio, UiObject uio2) throws UiObjectNotFoundException {
UiObject obj = null;
UiSelector uis = uio2.getSelector();
Log.i(TAG, "getUiObject: "+ uic.getChildCount(uis));
for(int i=0; i< uic.getChildCount(uis); i++){
UiObject uiObject = uic.getChildByInstance(uis, i);
boolean result = isSameLine(uio, uiObject);
Log.i(TAG, "getUiObject: "+result );
if(result){
obj = uiObject;
break;
}
}
if(obj == null){ throw new RuntimeException("Get "+ uio.getSelector()+" same line UiObject error");}
return obj;
} /**
* 判断两个控件是否在同一行
* @param obj1 UiObject 1
* @param obj2 UiObject 2
* @return return true, if is same line
* @throws UiObjectNotFoundException maybe can't find UiObject
*/
private static boolean isSameLine(UiObject obj1, UiObject obj2) throws UiObjectNotFoundException {
Rect first = obj1.getBounds();
Rect second = obj2.getBounds();
Log.i(TAG, "isSameLine: f:"+ first + ", s"+second);
// 比较区域
return first.top< second.bottom && first.bottom > second.top ;
}
}

最新文章

  1. Java Web学习笔记---用GET实现搜索引擎
  2. Ajax_05之跨域请求
  3. SQL Server 2008 R2的发布订阅配置实践
  4. codeforces 257div2 B. Jzzhu and Sequences(细节决定一切)
  5. 响应式Web设计(Responsive Web design)
  6. jenkins邮件模板
  7. poj1961 kmp
  8. Axure设计分析作业-实例解析
  9. 通过Jetty搭建一个简单的Servlet运行环境
  10. jQuery中间each实施例的方法
  11. JavaScriptSerializer返回一条Json,页面获取值问题,数据绑定
  12. MVC5的控制器,使用HttpPost方式时,接收的参数为null的原因
  13. js对象个人理解及记录
  14. Object.keys 及表单清空
  15. bzoj4326 树链剖分 + 线段树 // 二分 lca + 树上差分
  16. ROS tf 两个常用的函数
  17. FakeUserAgentError(&#39;Maximum amount of retries reached&#39;) 彻底解决办法
  18. leetcode48
  19. Ubuntu16下配置支持Windows访问的samba共享
  20. idea集成python插件

热门文章

  1. monkeyrunner简介
  2. sprigboot recontroller 是responsebody与controller结合 这样 就使每个方法默认返回json
  3. 继收购Magento十个月,Adobe推出Commerce Cloud商务云服务
  4. js slice 假分页
  5. Gradle+IDEA使用说明
  6. nsx-edge虚拟机抓包实践
  7. IIS搭建校内小站
  8. java反射机制简单实例
  9. Java基础 -- Collection和Iterator接口的实现
  10. Day058--django--app