zoukankan      html  css  js  c++  java
  • Android无线测试之—UiAutomator UiDevice API介绍一

    UiDevice 类介绍

    1.UiDevice 代表设备状态

    2.UiDevice 为单例模式

      获取UiDevice实例的方式:

      1) UiDevice.getInstance()

      2) getUiDevice()

      注意:第二种方式获取UiDevice实例,当含有该实例的类被别的类调用时会报空指针错误。

      举例:

      TestGetUiDevice1.java

    package com.uiautomatortest;
    
    import com.android.uiautomator.testrunner.UiAutomatorTestCase;
    
    public class TestGetUiDevice1 extends UiAutomatorTestCase {
        
        public void press(){
            
            getUiDevice().pressMenu();
            getUiDevice().pressHome();
            
        }
    
    }
    TestGetUiDevice1.java

      TestGetUiDevice2.java

    package com.uiautomatortest;
    
    import android.os.Bundle;
    import android.os.RemoteException;
    
    import com.android.uiautomator.core.UiDevice;
    import com.android.uiautomator.testrunner.UiAutomatorTestCase;
    
    public class TestGetUiDevice2 extends UiAutomatorTestCase {
        
        public void testDevice(){
            
            TestGetUiDevice device1=new TestGetUiDevice();
            device1.press();
            
        }
        
        public static void main(String[] args){
            
            String jarName, testClass, testName, androidId;
            jarName="DemoTest";
            testClass="com.uiautomatortest.Test";
            testName="testDevice";
            androidId="1";
            new UiAutomatorHelper(jarName, testClass, testName, androidId);
    
        }
    
    }
    TestGetUiDevice2.java

      类TestGetUiDevice2里调用类TestGetUiDevice1中包含getUiDevice()的press方法时报空指针错误:java.lang.NullPointerException

      如果将TestGetUiDevice1.java中的getUiDevice()换成UiDevice.getInstance()则可以正常运行,修改后的TestGetUiDevice1.java如下:

    package com.uiautomatortest;
    
    import com.android.uiautomator.core.UiDevice;
    import com.android.uiautomator.testrunner.UiAutomatorTestCase;
    
    public class TestGetUiDevice1 extends UiAutomatorTestCase {
        
        public void press(){
            
    //        getUiDevice().pressMenu();
    //        getUiDevice().pressHome();
            
            UiDevice.getInstance().pressMenu();
            UiDevice.getInstance().pressHome();
            
        }
    
    }
    TestGetUiDevice1.java

      因此目前都是采用UiDevice.getInstance()方式获取UiDevice实例。

    3.UiDevice 功能

      1)获取设备信息:屏幕分辨率,旋转状态,亮灭屏状态等

      2)操作:按键,坐标操作,滑动,拖拽,灭屏唤醒屏幕,截图等

      3)监听功能

  • 相关阅读:
    第11次作业
    第十次作业
    找回感觉的练习
    Tomact学习笔记
    移动端问题小计
    节流和防抖函数
    requestAnimationFrame动画封装
    svg实现渐变进度圆环
    手机端判断安卓,iso,微信
    git常用指令
  • 原文地址:https://www.cnblogs.com/fsw-blog/p/4544074.html
Copyright © 2011-2022 走看看