zoukankan      html  css  js  c++  java
  • 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 ;
        }
    }
    
  • 相关阅读:
    ajax post 数组
    Hello 2018 ABC
    A. The Way to Home
    Codeforces Round #453 (Div. 2) ABC
    Codeforces Round #452 (Div. 2) D
    Codeforces Round #452 (Div. 2) ABC
    Python 常用技巧
    Codeforces Round #451 (Div. 2) E
    Codeforces Round #451 (Div. 2) D. Alarm Clock
    Codeforces Round #451 (Div. 2) ABC
  • 原文地址:https://www.cnblogs.com/zeo-to-one/p/9812958.html
Copyright © 2011-2022 走看看