zoukankan      html  css  js  c++  java
  • UiAutomator2.0入门

    总是听说UiAutomator这个框架,但从来没有使用过。找了篇入门,实践一下。实践之后感觉,uiautomator写测试代码,还是有点费劲。接口名比较多,比较长。网易的atx里使用的uiautomator相当于原生uiautomator的python wrapper。接口就显得简洁很多。

    1. 新建项目

    使用推荐的android studio创建android空工程。File->New -> New project,在application name处填上工程名,点击 Next, 在target android devices页面,选择 支持的phone and tablet的最小sdk。这个可根据自己设备上的android版本选择。点出next,先把add no activity,完成了一个空工程的新建。

    2. 配置

    在Module:app里的build.gradle,新增绿色的内容。

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.0', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
    
        compile 'com.android.support:appcompat-v7:24.+'
        testCompile 'junit:junit:4.12'
        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    
    }

     我使用的gradle版本为2.14.1,相应的android plugin version是2.1.3。之前配置不正确,总是提示gradle sync不成功,然后测试代码就不能正常运行。另外,如果dependencies里的插件配置有重复,没有下载到本地的,还需要处理好。将项目设置为Android Tests, 可看到目录结构为下图所示。与test相关的文件背景变为绿色。

    3. case代码

    case代码写在androidTest目录下,示例内容如下:

    @RunWith(AndroidJUnit4.class)
    public class ExampleInstrumentedTest {
         private UiDevice mUIDevice = null;
        private Context mContext = null;
        String APP = "XXX";
    
        @Before
        public void setUp() throws RemoteException{
            mUIDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());  //获得device对象
            mContext = InstrumentationRegistry.getContext();
    
            if(!mUIDevice.isScreenOn()){  //唤醒屏幕
                mUIDevice.wakeUp();
            }
            mUIDevice.pressHome();  //按home键
        }
    
        @Test
        public void test1(){
            Intent myIntent = mContext.getPackageManager().getLaunchIntentForPackage(APP);  //启动app
            mContext.startActivity(myIntent);
            mUIDevice.waitForWindowUpdate(APP, 5 * 2000);
            UiObject sender = mUIDevice.findObject(new UiSelector().text("Send"));  //定位text内容为Send的控键
            try {
                sender.click();  //点击按键
            }catch (Exception e){
                e.printStackTrace();
            }
            assertTrue(true); //断言,随便乱写的,此处未起到断言作用
        }
    
    }

    连接设备,点击运行测试代码后,可看到运行的日志如下。

    $ adb push D:AndroidBookm_adr_atom_hotelHelloUI2appuildoutputsapkapp-debug.apk /data/local/tmp/com.example.mandasun.helloui2
    $ adb shell pm install -r "/data/local/tmp/com.example.mandasun.helloui2"
    	pkg: /data/local/tmp/com.example.mandasun.helloui2
    Success
    
    
    $ adb push D:AndroidBookm_adr_atom_hotelHelloUI2appuildoutputsapkapp-debug-androidTest-unaligned.apk /data/local/tmp/com.example.mandasun.helloui2.test
    $ adb shell pm install -r "/data/local/tmp/com.example.mandasun.helloui2.test"
    	pkg: /data/local/tmp/com.example.mandasun.helloui2.test
    Success
    
    
    Running tests
    
    $ adb shell am instrument -w -r -e package com.example.mandasun.helloui2 -e debug false com.example.mandasun.helloui2.test/android.support.test.runner.AndroidJUnitRunner
    Client not ready yet..
    Started running tests
    Tests ran to completion.
    

      从日志中可以看到,uiautomator将测试代码打成的app-debug.apk包和app-debug-androidTest-unaligned.apk推到adr机上,然后安装。之后,使用命令运行了这两个 apk。

    本文参考文档:uiautomator 2.0 demo与使用  uiautomator使用入门官方教程  android测试之UI自动化测试工具Uiautomator介绍

  • 相关阅读:
    mysql赋权限
    MySQL Server 5.6安装
    Class.isAssignableFrom(Class clz)方法 与 instanceof 关键字的区别
    每天一个linux命令
    解锁scott用户
    堆栈的理解
    类与对象的理解
    数组copy
    ArrayList 的代码
    intput/output 文件的复制练习
  • 原文地址:https://www.cnblogs.com/sunada2005/p/6433471.html
Copyright © 2011-2022 走看看