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)监听功能

  • 相关阅读:
    如何对抗信息茧房?
    术语
    2021.07.17软件更新公告
    【C#】C#中使用GDAL3(二):Windows下读写Shape文件及超详细解决中文乱码问题
    【C#】C#中使用GDAL3(一):Windows下超详细编译C#版GDAL3.3.0(VS2015+.NET 4+32位/64位)
    k8s使用私有镜像仓库
    四、Abp vNext 基础篇丨领域构建
    Abp vNext 番外篇-疑难杂症丨认证授权
    三、Abp vNext 基础篇丨分层架构
    知识全聚集 .Net Core 目录篇
  • 原文地址:https://www.cnblogs.com/fsw-blog/p/4544074.html
Copyright © 2011-2022 走看看