UiAutomator2.0 - 获取同行控件

问题

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 ;
    }
}
posted @   zeotoone  阅读(1801)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示