目录android
问题:UI测试时,在同一个界面出现相同的属性的控件(如图),对于这种控件的获取非常无奈。若是直接经过控件id去查找的话老是会返回界面该类型的第一个控件。
测试
解决:
1.UiObject2 中已经给出了解决方法,能够经过 getParent()方法处理。缺点:因为UiObjec2t控件与视图进行绑定,当视图变化后该控件对象就被销毁了。因此屡次使用则很是不便利ui
@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 去能查找到控件,可是界面变更的话脚本也得变更,可靠性不强。只能采起折中的方式来获取了。很少说,直接上代码。code
UiObject switchBnt3 = device.findObject(new UiSelector().resourceId("android:id/switch_widget").instance(2));orm
测试类:对象
@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()); }
帮助类:blog
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 ; } }