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