1. 程式人生 > >安卓自動化測試:Robotium(一)測試demo

安卓自動化測試:Robotium(一)測試demo

for 切換 ini too demo edittext pin dex bsp

1、下載Robotium
打開: http://code.google.com/p/robotium/
下載:robotium-solo-3.4.1.jar (寫測試用例時需要依賴此包)
2、需要把APK重新簽名,因為robotium要求被測應用和測試代碼要有一致的簽名, 所以我們需要把下載到的apk,通過re-sign.jar來產生debug key的apk,這個重新生成的apk就會跟測試項目簽名一致了。下載完後,需要配置ANDROID_HOME,就是安卓SDK的位置,然後把APK拉到圖標上,就會自動生成一個debug key的apk,如果無法直接單擊re-sign.jar運行,需要切換到放置該jar文件的目錄,cmd執行java -jar re-sign.jar產生新apk的過程中會彈出一個信息框,記得截下圖,因為裏面有兩個信息我們等會的代碼中需要用到安裝產生的apk。然後連接測試機(或打開模擬器,模擬器器一定要打開才能安裝成功),然後打開命令行 adb install xx_debug.apk(新生成apk的名稱) , 或者雙擊apk文件也可以安裝
2、創建測試工程
1)File -》New-》Other -》Android Test Project ,由於這是基於apk測試,選擇this project
2)在工程中添加 android-junit-report-1.5.8.jar 以及 robotium-solo-5.6.3.jar 這兩個包
3)Manifest.xml修改
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kamoCtest.login.hwj" //包名
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="13" />

<instrumentation
android:name="com.zutubi.android.junitreport.JUnitReportTestRunner" //導入單元測試報告jar
android:targetPackage="com.coolwi.km.user" /> //被測應用package名
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> //給予可寫權限

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
4)新建測試類SHSCListActivity 繼承ActivityInstrumentationTestCase2 ,編寫測試代碼,運行測試用例選擇 Run AS->AndroidJUnitTest
5)記一個測試demo
/*
@測試模塊:測試賬號管理
@測試內容:賬號管理模塊中各個小模塊鏈接點擊正常不報錯

*/
package com.coolwi.km.test;

import com.robotium.solo.*;
import android.test.ActivityInstrumentationTestCase2;


@SuppressWarnings("rawtypes")
public class SHSCListActivity extends ActivityInstrumentationTestCase2 {
private Solo solo;

private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.coolwi.km.activity.StartActivity";

private static Class<?> launcherActivityClass;
static{
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

@SuppressWarnings("unchecked")
public SHSCListActivity() throws ClassNotFoundException {
super(launcherActivityClass);
}

public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation());
getActivity();
}

@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}

public void testRun() {
// Take screenshot
solo.takeScreenshot();
// Wait for activity: ‘com.coolwi.km.activity.StartActivity‘
solo.waitForActivity("StartActivity", 2000);

// Wait for activity: ‘com.coolwi.km.activity.LoginActivity‘
assertTrue("LoginActivity is not found!", solo.waitForActivity("LoginActivity"));
// Enter the text: ‘15088132075‘
solo.clearEditText((android.widget.EditText) solo.getView("et_phone"));
solo.enterText((android.widget.EditText) solo.getView("et_phone"), "15088132075");
// Click on Empty Text View
solo.clickOnView(solo.getView("et_pwd"));
// Enter the text: ‘123456ab‘
solo.clearEditText((android.widget.EditText) solo.getView("et_pwd"));
solo.enterText((android.widget.EditText) solo.getView("et_pwd"), "123456ab");
// Click on 登 錄
solo.clickOnView(solo.getView("bt_ok"));
// Wait for activity: ‘com.coolwi.km.main.MainActivity‘
assertTrue("MainActivity is not found!", solo.waitForActivity("MainActivity"));
// Wait for dialog
solo.waitForDialogToOpen(5000);
// Set default small timeout to 10377 milliseconds
Timeout.setSmallTimeout(10377);
// Click on 賬號管理
solo.clickOnView(solo.getView("radio4"));
//商戶商城購買測試
// Click on 商戶商城
solo.clickOnView(solo.getView("tv_shsc"));
// Wait for activity: ‘com.coolwi.km.main.account.shopping.SHSCListActivity‘
assertTrue("SHSCListActivity is not found!", solo.waitForActivity("SHSCListActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_ddjl"));
// Wait for activity: ‘com.coolwi.km.main.account.shopping.SHSCMyOrderActivity‘
assertTrue("SHSCMyOrderActivity is not found!", solo.waitForActivity("SHSCMyOrderActivity"));
// Scroll to ImageView
android.widget.ListView listView0 = (android.widget.ListView) solo.getView(android.widget.ListView.class, 0);
solo.scrollListToLine(listView0, 0);
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on Empty Text View
solo.clickOnView(solo.getView("et_search"));
// Enter the text: ‘卡莫‘
solo.clearEditText((android.widget.EditText) solo.getView("et_search"));
solo.enterText((android.widget.EditText) solo.getView("et_search"), "卡莫");
// Take screenshot
solo.takeScreenshot();
// Click on 卡莫
solo.clickOnView(solo.getView("et_search"));
// Press search button
solo.pressSoftKeyboardSearchButton();
// Set default small timeout to 12368 milliseconds
Timeout.setSmallTimeout(12368);
// Click on 立即購買
solo.clickOnView(solo.getView("bt_pay"));
// Wait for activity: ‘com.coolwi.km.main.account.shopping.SHSCWebActivity‘
assertTrue("SHSCWebActivity is not found!", solo.waitForActivity("SHSCWebActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Take screenshot
solo.takeScreenshot();
// Take screenshot
solo.takeScreenshot();
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
/*
// Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));*/
// Click on 商戶協議
solo.clickOnView(solo.getView("tv_mdxy"));
// Take screenshot
solo.takeScreenshot();
// Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
/* // Click on 認證信息
solo.clickOnView(solo.getView("tv_rzxx"));
// Wait for activity: ‘com.coolwi.km.main.account.ZZRZActivity‘
assertTrue("ZZRZActivity is not found!", solo.waitForActivity("ZZRZActivity"));*/
// Click on 認證信息
solo.clickOnView(solo.getView("tv_rzxx"));
// Wait for activity: ‘com.coolwi.km.main.account.ZZRZActivity‘
assertTrue("ZZRZActivity is not found!", solo.waitForActivity("ZZRZActivity"));
// Take screenshot
solo.takeScreenshot();
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 系統設置
solo.clickOnView(solo.getView("tv_xtsz"));

// Take screenshot
solo.takeScreenshot();
// Wait for activity: ‘com.coolwi.km.main.account.XTSZActivity‘
assertTrue("XTSZActivity is not found!", solo.waitForActivity("XTSZActivity"));
// Click on 修改密碼
solo.clickOnView(solo.getView("rela_xgmm"));
// Wait for activity: ‘com.coolwi.km.main.account.UpdatePwdActivity‘
assertTrue("UpdatePwdActivity is not found!", solo.waitForActivity("UpdatePwdActivity"));
// Click on Empty Text View
solo.clickOnView(solo.getView("et_yzm"));
// Enter the text: ‘159753‘
solo.clearEditText((android.widget.EditText) solo.getView("et_yzm"));
solo.enterText((android.widget.EditText) solo.getView("et_yzm"), "159753");
// Click on Empty Text View
solo.clickOnView(solo.getView("et_pwd"));
// Enter the text: ‘123456ab‘
solo.clearEditText((android.widget.EditText) solo.getView("et_pwd"));
solo.enterText((android.widget.EditText) solo.getView("et_pwd"), "123456ab");
// Click on Empty Text View
solo.clickOnView(solo.getView("et_confirm_pwd"));
// Enter the text: ‘123456ab‘
solo.clearEditText((android.widget.EditText) solo.getView("et_confirm_pwd"));
solo.enterText((android.widget.EditText) solo.getView("et_confirm_pwd"), "123456ab");
// Click on 確 認
solo.clickOnView(solo.getView("bt_ok"));
// Click on 意見反饋
solo.clickOnView(solo.getView("rela_yjfk"));
// Wait for activity: ‘com.coolwi.km.main.account.FeedBackActivity‘
assertTrue("FeedBackActivity is not found!", solo.waitForActivity("FeedBackActivity"));
// Take screenshot
solo.takeScreenshot();
/* // Click on Empty Text View
solo.clickOnView(solo.getView("et_content"));*/
// Click on Empty Text View
solo.clickOnView(solo.getView("et_content"));
// Enter the text: ‘Gyg‘
solo.clearEditText((android.widget.EditText) solo.getView("et_content"));
solo.enterText((android.widget.EditText) solo.getView("et_content"), "Gyg");
// Click on 提 交
solo.clickOnView(solo.getView("bt_submit"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 使用幫助
solo.clickOnView(solo.getView("tv_help"));
// Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));
/* // Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));*/
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 賬戶充值
solo.clickOnView(solo.getView("tv_zhcz"));
// Wait for activity: ‘com.coolwi.km.main.account.ZHCZActivity‘
assertTrue("ZHCZActivity is not found!", solo.waitForActivity("ZHCZActivity"));
// Click on 100元
solo.clickOnView(solo.getView("bt1"));
// Enter the text: ‘100‘
solo.clearEditText((android.widget.EditText) solo.getView("et_amount"));
solo.enterText((android.widget.EditText) solo.getView("et_amount"), "100");
// Click on ImageView
solo.clickOnView(solo.getView("ib_czjl"));
// Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 月結賬單
solo.clickOnView(solo.getView("tv_yjzd"));
// Take screenshot
solo.takeScreenshot();
// Wait for activity: ‘com.coolwi.km.main.account.YJZDActivity‘
assertTrue("YJZDActivity is not found!", solo.waitForActivity("YJZDActivity"));

// Scroll to ImageView
android.widget.ListView listView1 = (android.widget.ListView) solo.getView(android.widget.ListView.class, 0);
solo.scrollListToLine(listView1, 0);
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 關於卡莫
solo.clickOnView(solo.getView("tv_about"));
// Wait for activity: ‘com.coolwi.km.main.account.AboutActivity‘
assertTrue("AboutActivity is not found!", solo.waitForActivity("AboutActivity"));
// Click on 新版本檢測 當前版本號:2.5.16-T 已是最新版
solo.clickOnView(solo.getView("re_check"));
// Click on 聯系我們
solo.clickOnView(solo.getView("re_lxwm"));
// Wait for activity: ‘com.coolwi.km.main.account.RZTSActivity‘
assertTrue("RZTSActivity is not found!", solo.waitForActivity("RZTSActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 分享二維碼給好友 點擊二維碼分享給好友
solo.clickOnView(solo.getView("re_code"));
// Wait for activity: ‘com.mob.tools.MobUIShell‘
assertTrue("MobUIShell is not found!", solo.waitForActivity("MobUIShell"));
// Click on Empty Text View
solo.clickOnView(solo.getView(android.widget.TextView.class, 0));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 系統設置
solo.clickOnView(solo.getView("tv_xtsz"));
// Wait for activity: ‘com.coolwi.km.main.account.XTSZActivity‘
assertTrue("XTSZActivity is not found!", solo.waitForActivity("XTSZActivity"));
// Click on 退出登錄
solo.clickOnView(solo.getView("rela_tcdl"));
// Wait for dialog
solo.waitForDialogToOpen(5000);
// Click on 確定
solo.clickOnView(solo.getView("bt_confirm"));
// Wait for activity: ‘com.coolwi.km.activity.LoginActivity‘
assertTrue("LoginActivity is not found!", solo.waitForActivity("LoginActivity"));
}
}

安卓自動化測試:Robotium(一)測試demo