使用原因

公司需要自动化测试一些功能,上级说这个可以有效debug一些功能bug,so让我看看。但是这个技术貌似也已经挺久了。

安装

我使用的IDE是as,eclipse等也可以。
首先在app根目录的build.gradle文件中加入依赖:

 // AS默认配置,如果如果没有记得加上
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

其次配置一下testInstrumentation为AndroidJunitRunner:

defaultConfig {
...
// 这个AS会为我们默认配置,如果没有记得加上
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

第一个坑:
1.这是网上老早的代码改过的,我的sdk不符合所以直接复制到gradle了,记得同步一波;
2.还有targetSdkVersion和compileSdkVersion一致;
3.把出错的包改成正确一致的版本就ok
4.但是我的layout文件里的layout报错,直接改为简单的LinearLayout就ok了。(大概就是远古时期的sdk没有这些包所以导致的)

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.yl.uiautomatordemo"
        minSdkVersion 18
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:support-v4:25.0.0'
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
好了,gradle配置好了以后,就到了测试了(这之前你的项目代码已经写好),我用的是简单的按钮点击事件来测试。直观简单。

简单的测试

这个步骤只是为了了解功能,不是实际操作。

步骤:

1.把要测试的App安装到手机中。

2.打开UI分析工具uiautomatorviewer.bat(在android sdk/tools/bin路径),分析当前UI的界面元素,确保App的各个控件可以被测试工具获取到。

3.根据App使用流程编写测试用例。

4.运行测试用例进行测试,定位bug,解决bug。

在打开bat以后,获取到界面元素后,就可以用获取到的元素id之类的写测试用例了。

3.在以下的目录下创建新的TestUi类,之后在内部创建方法(命名要是test开头)
请输入图片描述

至于测试的方法具体内容涉及到的api是关键,但先不具体说,他们其实很简明。
以下放testButton代码

public void testButton() throws UiObjectNotFoundException {
        // 获取设备对象
        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        UiDevice uiDevice = UiDevice.getInstance(instrumentation);
        // 获取上下文
        Context context = instrumentation.getContext();

        // 启动测试App
        Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.yl.uiautomatordemo");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);

        // 打开CollapsingToolbarLayout,此处的resourceId在bat中可以查看
        String resourceId = "com.yl.uiautomatordemo:id/butt";  

        UiObject button = uiDevice.findObject(new UiSelector().resourceId(resourceId));
        button.click();

        for (int i = 0; i < 5; i++) {
           button.click();

        }
    }

其实,这个bat只是提供查询各个控件的名称之类信息,结合api的使用就可以用ui测试了。

第二坑:(之前看到的代码太老,已经过时了。)
要使用muiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

不能使用UiDevice.getInstance()或者getUiDevice()。

标签: none

添加新评论