Android Robotium測試框架

Robotium是一個擴展於JUnit的開源庫,運用多種有用的方法來支持Android UI測試。它提供的強大的自動化黑箱測試範例,可用於Android應用(原生的和混合的)和web測試。只要源代碼允許,你就可以通過Robotium寫功能、系統和驗收測試方案,以及測試應用。

 

1.Robotium---環境搭建

 

http://www.tuicool.com/articles/ZZvUbaz

 

2.常用語法

 

2.1.等待timeout毫秒一個名字爲name的Activity啓動:waitForActivity(String name, int timeout)

實例:assertTrue("無法啓動啓動類", solo.waitForActivity ("MainActivity", 30000));

 

2.2.Robotium將睡眠設置的毫秒數:sleep(inttime)

實例:solo.sleep(5000)

 

2.3.清空EditText的內容:clearEditText(android.widget.EditTexteditText)

實例:solo.clearEditText((EditText)solo.getView("edtInsertName"))

 

2.4.根據按鈕上的文字點擊按鈕:clickOnButton(Stringtext)

實例:solo.clickOnButton("^綠色$");

 

2.5.根據文字點擊控件:clickOnText(Stringtext)

實例:solo.clickOnText("控件上顯示文字");

 

2.6.輸入內容:enterText(android.widget.EditTexteditText, String text)

solo.enterText((EditText)solo.getView("edtInsertName"),"說些什麼好呢?");

 

2.7.返回:goBack()

 

2.8.截屏並保存爲設置的名字:takeScreenshot(Stringname)

默認保存在: /sdcard/Robotium-Screenshots/

 

 

2.9.解鎖屏幕:unlockScreen()

 

 

3.編寫Robotium測試程序

 

1)在測試方法前覆寫父類的setUp()方法: 該方法用來初始化solo,綁定對應的Activity。

@Override  public void setUp() throws Exception {   

super.setUp();   

solo = new Solo(getInstrumentation(), getActivity());   

 

2)在測試方法後覆寫父類的tearDown()方法: 

該方法用來清理資源垃圾,關閉activity。  

public void tearDown() throws Exception {    

solo.finishOpenedActivities(); 

}   

 

3)Solo類運用 

Solo類中提供了自動點擊、取得、拖拽、搜索等各種方法。 聲明Solo類型的成員變量private Solo solo; 

 

 

典型方法: 

① 點擊: 

 

clickOnButton(int)—Clicks on a Button with a given index. 

clickOnButton(String)—Clicks on a Button with a given text. 

clickOnCheckBox(int)—Clicks on a CheckBox with a given index. 

clickOnView(View)—Clicks on a given View.  

 

clickOnText(String)—Clicks on a View displaying a given text. 

clickLongOnText(String)—Long clicks on a given View. 

 

clickOnRadioButton(int)—Clicks on a RadioButton with a given index. 

clickOnScreen(float, float)—Clicks on a given coordinate on the screen. 

 

② 取得: 

 

getCurrentActivity()—Returns the current Activity.  

 

getText(String)—Returns a TextView which shows a given text.  

getView(int)—Returns a View with a given id.  

 

getEditText(String)—Returns an EditText which shows a given text.   

getImage(int)—Returns an ImageView with a given index.  

③ 拖拽: 

drag(float, float, float, float, int)—Simulate touching a given location and dragging it to a new location. 

 

④ 搜索: 

 

searchText(String)—Searches for a text string and returns true if at least one item is found with the expected text. 

 

searchEditText(String)—Searches for a text string in the EditText objects located in the current Activity.  

searchButton(String, boolean)—Searches for a Button with the given text string and returns true if at least one Button is found. 

 

4)創建需要的測試方法  可以根據不同目的編寫多個測試方法。注意方法名稱必須以test開頭,程序運行會自動調用以test開頭的方法。每次調用測試方法都會運行一次測試工程。

 

 

4.如何獲取控件ID-兩種方法

 

(1)Android 實用工具Hierarchy Viewer實戰

 

是隨AndroidSDK發佈的工具,位置在tools文件夾下,名爲hierarchyviewer.bat

需要運行測試項目,在調試環境下才可以檢測到模擬器的

(2)運行命令行記錄log,然後點擊對應Activity,接着可以在logcat看到

 

 

 

 

 

 

 

5.實戰-針對APK進行的測試

 

robotium要求被測應用和測試代碼要有一致的簽名

 

 被測試項目爲demo1,下面是實戰的具體步驟

 

5.1. 配置ANDROID_HOME爲android sdk的安卓目錄,例如:D:\android-sdk

 

5.2. 在path下添加這兩個:%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;

 

5.3. 需要把APK重新簽名,因爲robotium要求被測應用和測試代碼要有一致的簽名, 所以我們需要把下載到的apk,通過re-sign.jar來產生debug key的apk,這個重新生成的apk就會跟測試項目簽名一致了

 

5.4. 下載完後,需要配置ANDROID_HOME,就是安卓SDK的位置,然後把APK拉到圖標上,就會自動生成一個debug key的apk,如果無法直接單擊re-sign.jar運行,需要切換到放置該jar文件的目錄,cmd執行java -jar re-sign.jar產生新apk的過程中會彈出一個信息框,記得截下圖,因爲裏面有兩個信息我們等會的代碼中需要用到

 

5.5. 安裝產生的apk。然後打開模擬器(模擬器器一定要打開才能安裝成功),然後打開命令行  adb install mitalk_debug.apk(新生成apk的名稱) , 或者雙擊apk文件也可以安裝

 

安裝成功就可以再模擬器裏看到該應用的圖標了

 

5.6. 打開Eclipse,點擊File->New一個AndroidTest Project  TestDemo1, 然後點擊下一步的時候選擇Thisproject(因爲我們測試的是APK),然後選擇要在哪個android版本上測試

 

5.7. 在該項目下創建一個包,com.example.demo1.test,在該包下創建TestDemo1Apk類,如下

 

package com.example.demo1.test;

 

import com.robotium.solo.Solo;

 

importandroid.test.ActivityInstrumentationTestCase2;

import android.widget.EditText;

 

@SuppressWarnings("rawtypes")

public class TestDemo1Apk extendsActivityInstrumentationTestCase2 {

 

       private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME ="com.example.demo1.MainActivity";//啓動類

 

       private static Class<?> launcherActivityClass;

       static{

                try {

                        launcherActivityClass =Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);

                } catch (ClassNotFoundExceptione) {

                        throw newRuntimeException(e);

                }

       }

       

       @SuppressWarnings("unchecked")

       public TestDemo1Apk() throws ClassNotFoundException {

                super(launcherActivityClass);

       }

       

       private Solo solo;

       

       @Override

       protected void setUp() throws Exception {

                solo = newSolo(getInstrumentation(), getActivity());

       }

 

 

             public void testcase001() throwsException {

                        //等待  Activity "MainActivity" 啓動

                       assertTrue("無法啓動啓動類",solo.waitForActivity("MainActivity", 30000));

           solo.sleep(5000);

 

         //輸入文字:"131243"

           solo.enterText((EditText)solo.getView("edtInsertName"), "說些什麼好呢?");

           solo.sleep(2000);

           

           //清空輸入框的內容

           solo.clearEditText((EditText)solo.getView("edtInsertName"));

           

           

           //按下按鈕 "綠色"

           solo.clickOnButton("^綠色$");

           solo.sleep(2000);

 

           //按下按鈕 "黃色"

           solo.clickOnButton("^黃色$");

           solo.sleep(2000);

 

           //按下按鈕 "藍色"

           solo.clickOnButton("^藍色$");

           solo.sleep(2000);

 

 

           //按下 TextView "看我變變變~~~"

           solo.clickOnText("^看我變變變~~~$");

           solo.sleep(5000);     

           

             }

            

 

  @Override

  public void tearDown() throws Exception {

                solo.finishOpenedActivities();

 

  }

 

 

}

 

 

5.8.右鍵該項目,選擇property然後選擇javabuild path, 選擇 Add JARs,選擇下到的robotium.jar

 

Add Library,點擊Junit,選擇Junit4

 

 

 

 

 

 

 

6.運行多個測試用例

http://jingyan.baidu.com/article/948f59240f687cd80ff5f921.html

 

 

 

 

7.命令行運行Android Robotium自動化用例或單元測試用例

http://blog.csdn.net/wirelessqa/article/details/8999433

 

1)運行所有的測試用例

舉個栗子:運行測試工程下的所有用例

 

adb shell am instrument -wcom.taobao.taobao.test/android.test.InstrumentationTestRunner

 

 

2)運行單個測試類或某個TestSuite

舉個栗子:運行測試類com.taobao.taobao.test.TestRegister

 

 

adb shell am instrument -e classcom.taobao.taobao.test.TestRegister -wcom.taobao.taobao.test/android.test.InstrumentationTestRunner

 

 

3)運行某個測試類裏面的某個測試方法

舉個栗子:運行com.taobao.taobao.test.TestRegister中的測試方法testRegister

 

adb shell am instrument -e classcom.taobao.taobao.test.TestRegister#testRegister -wcom.taobao.taobao.test/android.test.InstrumentationTestRunner

 

4)運行兩個不同的測試類或類中的方法

舉個栗子:運行com.taobao.taobao.test.TestLogin

 

和com.taobao.taobao.test.TestRegister類中的方法testRegister

 

 

 

 

adb shell am instrument -e classcom.taobao.taobao.test.TestLogin,com.taobao.taobao.test.TestRegister#testRegister  -wcom.taobao.taobao.test/android.test.InstrumentationTestRunner

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章