InstrumentationTestCase类的使用
来源:互联网 发布:表格数据分析表 编辑:程序博客网 时间:2024/06/15 23:31
本文参考:http://blog.csdn.net/xianming01/article/details/7893391
InstrumentationTestCase类封装了Junit类,是Android的测试类。下面以一个实例讲解它的使用:
该实例是用来测试Webkit的加载的,测试的是WebView的loadUrl()方法:
WebkitLoad.java
package com.android.webkit;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebView;import android.widget.Button;public class WebkitLoad extends Activity { /** Called when the activity is first created. */private Button btn1, btn2;private WebView webview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button)findViewById(R.id.btn1); btn2 = (Button)findViewById(R.id.btn2); webview = (WebView)findViewById(R.id.webView1); btn1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubwebview.loadUrl("file:///android_asset/3.htm");}}); btn2.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubwebview.loadUrl("file:///android_asset/1.html");}}); }}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><Button android:text="Button" android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="Button" android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent"></WebView></LinearLayout>
以上是被测试对象的代码,下面是测试的代码:
(1)改写AndroidManifest.xml文件:
主要是添加“<uses-library android:name="android.test.runner" />” 和 “<instrumentation android:targetPackage="com.android.webkit" android:name="android.test.InstrumentationTestRunner" />”
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.webkit" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="1" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="android.test.runner" /> <activity android:name=".WebkitLoad" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <instrumentation android:targetPackage="com.android.webkit" android:name="android.test.InstrumentationTestRunner" /> </manifest>
(2)在src目录下添加目录com.anroid.webkit.test,
WebkitLoadTest.java
package com.android.webkit.test;import com.android.webkit.R;import com.android.webkit.WebkitLoad;import android.content.Intent;import android.os.SystemClock;import android.test.InstrumentationTestCase;import android.widget.Button;public class WebkitLoadTest extends InstrumentationTestCase {private Button btn1, btn2;private WebkitLoad webkitload;@Overrideprotected void tearDown() throws Exception {// TODO Auto-generated method stubwebkitload.finish();super.tearDown();}@Overrideprotected void setUp() throws Exception {// TODO Auto-generated method stubsuper.setUp();Intent intent = new Intent();intent.setClassName("com.android.webkit", WebkitLoad.class.getName());intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);webkitload = (WebkitLoad)getInstrumentation().startActivitySync(intent);btn1 = (Button)webkitload.findViewById(R.id.btn1);btn2 = (Button)webkitload.findViewById(R.id.btn2);}public void testActivity() throws Exception{SystemClock.sleep(2000);for(int i = 0; i < 10; i++){getInstrumentation().runOnMainSync(new PerformClick(btn1));SystemClock.sleep(2000);getInstrumentation().runOnMainSync(new PerformClick(btn2));SystemClock.sleep(2000);}}}
PerformClick.java
package com.android.webkit.test;import android.widget.Button;public class PerformClick implements Runnable {Button btn;public PerformClick(Button button){btn = button;}@Overridepublic void run() {// TODO Auto-generated method stubbtn.performClick();}}
- InstrumentationTestCase类的使用
- Andriod 单元测试—InstrumentationTestCase
- Andriod 自动化测试—InstrumentationTestCase
- instrumentation中的InstrumentationTestCase学习笔记(1)
- 在InstrumentationTestCase中获取Context对象
- 【学习笔记】系列十:Android自动化测试InstrumentationTestCase
- 【Android单元测试】如何在InstrumentationTestCase中获取Context对象
- CRectTracker类的使用
- CBitmapButton类的使用
- SQLDMO类的使用
- C# 类的使用
- SQLDMO类的使用
- Vector类的使用
- SQLDMO类的使用
- Vector类的使用
- 类Introspector的使用
- 类工厂的使用
- 集合类的使用
- Hadoop如此之火,我该如何选择?
- 谷歌数据中心“风水学”
- CityEngine使用GIS模板创造三维数字城市(一)
- AJAX-onreadystatechange事件
- 英特尔击败AMD,但输给了高通
- InstrumentationTestCase类的使用
- 七年,Google数据中心从保守走向开放
- background:url(imagepath)不能显示图片
- hdu 1106
- 从谷歌宕机事件认识互联网工作原理
- 五大科技公司觊觎智能眼镜:谷歌微软新战场
- 关于Google智能眼镜的设计
- Android 实现程序完全退出
- 十大超级计算机强势来袭 Linux真安全还是假忽悠