Android开发之Instrumentation(自动化测试)
2014-04-07 16:03
218 查看
Android在JUnit的之外给我们又提供了Instrumentation测试框架。通过Instrumentation可以模拟按键按下、抬起、屏幕点击、滚动等事件,有效地控制Activity进行自动化测试。
Instrumentation是执行application instrumentation代码的基类。当应用程序运行的时候instrumentation处于开启,Instrumentation将在任何应用程序运行前初始化,可以通过它监测系统与应用程序之间的交互。
1. Manifest.xml中描述:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="Tests for My App"
android:targetPackage="com.my.test" />
<span style="font-size:18px;"><uses-permission android:name="android.permission.INJECT_EVENTS" /></span>
2.模拟发送按键:
/**
* 传入需要的键值即可
* @param keyCode
*/
private void sendKeyCode(final int keyCode){
new Thread () {
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(keyCode);
} catch (Exception e) {
Log.e("Exception when sendPointerSync", e.toString());
}
}
}.start();
}
}
3.模拟鼠标事件:
new Thread () {
public void run () {
try {
Instrumentation inst=new Instrumentation();
inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN, 240, 400, 0));
MotionEvent.ACTION_UP, 240, 400, 0));
} catch(Exception e) {
Log.e("Exception when sendPointerSync", e.toString());
}
}
}.start();
Instrumentation是执行application instrumentation代码的基类。当应用程序运行的时候instrumentation处于开启,Instrumentation将在任何应用程序运行前初始化,可以通过它监测系统与应用程序之间的交互。
1. Manifest.xml中描述:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="Tests for My App"
android:targetPackage="com.my.test" />
<span style="font-size:18px;"><uses-permission android:name="android.permission.INJECT_EVENTS" /></span>
2.模拟发送按键:
/**
* 传入需要的键值即可
* @param keyCode
*/
private void sendKeyCode(final int keyCode){
new Thread () {
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(keyCode);
} catch (Exception e) {
Log.e("Exception when sendPointerSync", e.toString());
}
}
}.start();
}
}
3.模拟鼠标事件:
new Thread () {
public void run () {
try {
Instrumentation inst=new Instrumentation();
inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN, 240, 400, 0));
inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 240, 400, 0));
} catch(Exception e) {
Log.e("Exception when sendPointerSync", e.toString());
}
}
}.start();
相关文章推荐
- 浅析android应用增量升级
- Android程序运行中动态加载Lib的方法
- Android 源码的下载和编译环境的安装及编译
- Android中dip、dp、sp、pt和px的区别
- Android学习之图像的处理
- Android四大组件之Service
- android widget使用
- chromium34源码下载及chromium for Android编译环境的搭建
- Android中通知栏的使用方法
- Android应用如何监听自己是否被卸载及卸载反馈功能的实现
- Android用列表显式联系人
- android中的WebView与JS交互
- android开发时遇到的类型转换问题
- android 编程技巧总结
- 编写android程序调用jni本地方法的实现
- Android Gallery3D源码分析
- Android开发把项目打包成apk
- Android 各国语言缩写-各国语言简称
- 异步下载图片和图片缓存
- handler机制的原理