(二)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里面怎么实现截屏,截屏出问题了该怎么办?
第一次写博客,写的不怎么好,条理也不是很清楚的。如果有什么写的不好的地方大家可以指出来。我会随时不定期的修改博客的。
- (二)UI Automator测试项目的建立
- 实战自动化测试Espresso和Ui automator(二)
- android测试:UI automator
- UI automator的使用
- Android自动化测试 UI Automator
- 实战自动化测试Espresso和Ui automator(一)
- (一)初次认识Google测试框架UI Automator
- 使用UI Automator View做UI测试
- UI Automator Viewer的使用
- UI Automator Viewer的使用
- 使用AndroidStudio执行UI Automator 自动化测试
- UI Automator
- UI automator
- UI automator 应用中遇到的问题
- (三)UI Automator截取屏幕
- 使用AndroidStudio执行UI Automator 自动化测试版本问题
- UI Automator(Android自动化测试)全面学习笔记
- Android自动化测试之——UI Automator
- Gym 101190F Foreign Postcards
- javaScript实现小球碰撞
- git学习3
- 合法IP地址的判断、关于给定字符串,求最大不重复子串 -- JAVA 算法题
- 【快搭】springboot+jsp
- (二)UI Automator测试项目的建立
- Java爬虫--页面跳转爬数据
- HDU5492 Find a path (动态规划)
- MySQL查询表格内容基本语句1
- kafka 安装和使用测试(mac版)
- PAT (Basic Level) Practise (中文)1010. 一元多项式求导 (25)
- Hadoop之MapReduce调度:通过shell进行多日期的串行跑批统计
- json版的轮播图
- Html中行内元素有哪些?块级元素有哪些?