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

  • 相关阅读:
    ioctlsocket()
    阻塞式socket例子学习
    accept()
    listen()
    WSAStartup
    C#动态操作DataTable(新增行、列、查询行、列等)
    C# 连接SQL Server数据库的几种方式--server+data source等方式
    ExcelHelper类
    c#使用椭圆签名算法制作软件序列号
    LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count 等
  • 原文地址:https://www.cnblogs.com/fsw-blog/p/4544074.html
Copyright © 2011-2022 走看看