(二)UI Automator测试项目的建立

来源:互联网 发布:大数据主要来源于哪些 编辑:程序博客网 时间:2024/05/29 12:22

上篇对UI Automator做了一个大概的叙述,这篇就讲讲怎么搭建一个UI Automator项目的测试环境。
1.打开Android Studio。(Android Studio环境怎么搭建可以自己百度查查)

2.点击新建项目,我这边新建的项目名字叫Test。(这里需要注意我们要建立的项目是没有任何activity)

这里写图片描述

3.然后一直Next,等待整个项目建立起来。

4.项目建立起来,然后点击左侧上面的视图曾,选择项目为Project。然后我们看到的视图层应该是下面图片这种的。

这里写图片描述

5.我们可以看到app → src 下面有三个包,分别的是androidTest,main,test。三个包里面我们用到的就是androidTest。

6.展开androidTest的包。如下图。会发现里面有3个层级了,一个自带的Java包,还有一个自带的包名和一个类文件。

这里写图片描述

7.这个时候我们可以删除他的包名,建立自己的包名和类名。

这里写图片描述

8.建立好之后,我们应该添加我们需要做测试的依赖了。在app → build.gradle里面添加上我们所需要的依赖和sdk版本。然后点击Sync。同步一下(这里我们需要注意下,minSdkVersion 18这里UI Automator支持的sdk最低版本是18)

这里写图片描述

apply plugin: 'com.android.application'android {    compileSdkVersion 26    buildToolsVersion "26.0.0"    defaultConfig {        applicationId "com.example.user.test"        minSdkVersion 18        targetSdkVersion 26        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'])    testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'    compile 'com.android.support:support-annotations:26.0.0-alpha1'    androidTestCompile 'com.android.support:support-annotations:26.0.0-alpha1'    androidTestCompile 'com.android.support.test:runner:0.5'    androidTestCompile 'com.android.support.test:rules:0.5'    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'}

9.然后回到刚才我们建的类里面写上以下代码:

package Test;import android.app.Instrumentation;import android.content.Context;import android.content.Intent;import android.support.test.InstrumentationRegistry;import android.support.test.runner.AndroidJUnit4;import android.support.test.uiautomator.UiDevice;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import org.junit.runner.RunWith;import java.io.IOException;@RunWith(AndroidJUnit4.class)public class Tester {    static Instrumentation instrumentation;    static UiDevice mDevice;    @BeforeClass  //也可以是Before     public static void setUpBeforeClass() throws IOException {        instrumentation = InstrumentationRegistry.getInstrumentation();        mDevice = UiDevice.getInstance(instrumentation);    }    @Test    public void test_Template() throws IOException, InterruptedException {        //启动apk的程序        Context context = InstrumentationRegistry.getInstrumentation().getContext();        Intent intent = context.getPackageManager().getLaunchIntentForPackage("这里填的是包名");        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);        context.startActivity(intent);        //下面写你的逻辑代码    }    @AfterClass  //也可以是After        public static void tearDownAfterClass() throws IOException {        }}

这个是一个UI Automator代码的模板,可以参考一下。

下一篇我会介绍before,beforeclass,after,afterclass的区别。还有UI Automator里面怎么实现截屏,截屏出问题了该怎么办?

第一次写博客,写的不怎么好,条理也不是很清楚的。如果有什么写的不好的地方大家可以指出来。我会随时不定期的修改博客的。

原创粉丝点击