天天看點

Android 自動化測試—robotium(一)

Android 的開發可以說已經遍地都是,不說精緻的app,隻要看些書,看點教學視訊,學習二至三個月,都可以随便開發幾個小項目,當然隻能是自娛自樂的。最近突然想起了,關于android 的自動化測試,于是網上搜了相關資料學習,最後決定先嘗試 robotium。

robotium wiki:http://code.google.com/p/robotium/w/list

這裡有篇文章對于robotium的介紹很貼切:robotium 是 android 自帶類 Instrumentation 的一個封裝,友善測試人員直接調用封裝好的接口,也就是說,實際上我們直接使用Instrumentation 也能夠進行自動化測試,但robotium可以簡化我們的測試步驟,我們隻需要調用某個robotium的API,傳幾個參數,就等于我們在調用一部分的Instrumentation幫我們實作測試。robotium 就是富二代!!高帥富!!

http://www.51testing.com/?uid-22381-action-viewspace-itemid-238847

需要注意:

1.測試項目:例如:HelloWorldTest,Build Path需要導入robotium-solo.jar包

2.Eclipse:3.7 版本,需要勾選Order and Export中的内容

Android 自動化測試—robotium(一)
# package com.luwenjie.helloworld.test; 
#  
# import android.test.ActivityInstrumentationTestCase2; 
# import com.luwenjie.helloworld.HelloWorldActivity; 
# import com.jayway.android.robotium.solo.Solo; 
#  
# public class HelloWorldTest extends ActivityInstrumentationTestCase2
# <HelloWorldActivity>{ 
#  
#     private Solo solo; 
#  
# //需要測試的app是什麼?
# //這裡需要測試com.luwenjie.helloworld包下的HelloWorldActivity這個應用
#  
#     public HelloWorldTest(){ 
#          super("com.luwenjie.helloworld", HelloWorldActivity.class); 
#     } 
#    
# //打開HelloWorld這個應用
#  
#     public void setUp() throws Exception{ 
#          solo = new Solo(getInstrumentation(), getActivity()); 
#     } 
#  
# //執行測試
# //searchText(String str):驗證字元串是否存在
#  
#     public void testUI() throws Exception { 
#         boolean expected = true; 
#         boolean actual = solo.searchText("Hello") && solo.searchText("World"); 
#  
#         assertEquals("This and/or is are not found", expected, actual); 
#     } 
# } 
           

花了一點時間寫了一個計算标準體重的小應用,當然目的是為了測試 robotium 的使用情況。經過一段泡在robotium的API文檔上,對一些基本操作也有所了解,開始了更進一步的嘗試。

robotium API:http://code.google.com/p/robotium/downloads/list

雖然API文檔已經把相關知識點解釋的很全,作為學習還需不斷使用了解鞏固知識

以下先簡單介紹一部分API

// 單擊一個單選按鈕

clickOnRadioButton(int index)

index:用來辨別哪個RadioButton, 隻有1個RadioButton,index = 0 以此類推

// 單擊一個EditText表單

clickOnEditText(int index)

index: 用來辨別哪個EditText,隻有1個EditText, index = 0 以此類推

// 在EditText中輸入Text

enterText(int index, String text)

index: 用來辨別哪個EditText

text : 輸入的内容

// 單擊一個按鈕

clickOnButton(String name)

name : 按鈕的名稱

// 傳回上一頁

goBack()

// 清空EditText表單

clearEditText(int index)

index: 用來辨別哪個EditText

1. package com.luwenjie.standweight.test; 
   2.  
   3. import android.test.ActivityInstrumentationTestCase2; 
   4. import com.luwenjie.standweight.StandWeightActivity; 
   5. import com.jayway.android.robotium.solo.Solo; 
   6.  
   7. public class weightText extends ActivityInstrumentationTestCase2<StandWeightActivity> { 
   8.     private Solo solo; 
   9.     public weightText() { 
  10.         super("com.luwenjie.standweight", StandWeightActivity.class); 
  11.     } 
  12.      
  13.     public void setUp() throws Exception{ 
  14.          solo = new Solo(getInstrumentation(), getActivity());    
  15.     } 
  16.      
  17.     public void testUI() throws Exception { 
  18.         boolean expected = true; 
  19.          
  20.         //驗證男孩180cm的标準體重為70公斤 
  21.         solo.clickOnRadioButton(0); 
  22.         solo.clickOnEditText(0); 
  23.         solo.enterText(0, "180"); 
  24.         solo.clickOnButton("計算"); 
  25.         boolean actual1 = solo.searchText("70.00"); 
  26.         assertEquals("This and/or is are not found", expected, actual1); 
  27.          
  28.         //傳回清空editText表單 
  29.         solo.goBack(); 
  30.         solo.clearEditText(0); 
  31.          
  32.         //驗證女孩160cm的标準體重為70公斤 
  33.         solo.clickOnRadioButton(1); 
  34.         solo.clickOnEditText(0); 
  35.         solo.enterText(0, "160"); 
  36.         solo.clickOnButton("計算"); 
  37.         boolean actual2 = solo.searchText("54.00"); 
  38.         assertEquals("This and/or is are not found", expected, actual2); 
  39.     } 
  40. } 
           

為了更好的嘗試Robotium的AIP,自己編寫了部分android控件,提供測試。

EditText 控件:

Android 自動化測試—robotium(一)

操作步驟:

1.單擊 EditText 控件

2.輸入文字内容:This is EditTextActivity

3.單擊 Submit 按鈕

驗證:頁面傳回文字:This is EditTextActivity

1. public void testUI() throws Exception { 
   2.     this.EditText(); 
   3. } 
   4.  
   5. public void EditText(){ 
   6.     boolean expected = true; 
   7.     solo.clickOnButton("EditText"); 
   8.     solo.enterText(0, "This is EditTextActivity"); 
   9.     solo.clickOnButton("Submit"); 
  10.     boolean actual = solo.searchText("This is EditTextActivity"); 
  11.     assertEquals("This is not found",expected,actual); 
  12.     solo.goBack(); 
  13. } 
           

assertEquals 擁有三個參數

assertEquals(String message,boolean expected, boolean actual)

message:出錯時傳回的資訊

expected:預期結果,是個布爾值

actual:實際結果,也是個布爾值

如果 expected 和 actual 的值相同時(可以都為false),測試通過,否則失敗。

CheckBox 控件:

單擊CheckBox1勾選              再次單擊CheckBox取消勾選

Android 自動化測試—robotium(一)
Android 自動化測試—robotium(一)

操作步驟:

1. 單擊 CheckBox 1 複選框

驗證:傳回提示: Check Box 1被選中

2. 再次單擊 Check Box1 複選框(達到取消勾選效果)

驗證:傳回提示:Check Box 1取消選中

1. public void testUI() throws Exception { 
   2.     this.CheckBox(); 
   3. } 
   4.  
   5. public void CheckBox(){ 
   6.     boolean expected = true; 
   7.     solo.clickOnButton("CheckBox"); 
   8.      
   9.     solo.clickOnCheckBox(0); 
  10.     boolean actual = solo.searchText("Check Box 1被選中"); 
  11.     assertEquals("This is not found",expected,actual); 
  12.      
  13.     solo.clickOnCheckBox(0); 
  14.     boolean actual2 = solo.searchText("Check Box 1取消選中"); 
  15.     assertEquals("This is not found",expected,actual2); 
  16.     solo.goBack(); 
  17. } 
           

這裡solo.clickOnCheckBox(0) 代表第一個CheckBox元素:Check Box 1

以此類推,1 代表第二個CheckBox元素:Check Box 2

CheckBox實作:http://luwenjie.blog.51cto.com/925779/915848

Spinner 控件:

Android 自動化測試—robotium(一)

預設顯示:【選擇的是:北京】

操作步驟:

1.點選【城市】下拉框

2.選擇【上海】

驗證:TextView 顯示:【選擇的是:上海】

1. public void testUI() throws Exception { 
   2.        this.Spinner(); 
   3. } 
   4.  
   5. public void Spinner(){ 
   6.     solo.clickOnButton("Spinner"); 
   7.     boolean actual = solo.isSpinnerTextSelected(0,"北京"); 
   8.  
   9.     solo.pressSpinnerItem(0, 1); 
  10.     boolean actual1 = solo.searchText("選擇的是:上海"); 
  11.     assertEquals("This is not found",true, actual1); 
  12. } 
           

isSpinnerTextSelected 擁有兩個參數

public boolean isSpinnerTextSelected(int index, String text)

index:定位選擇的Spinner,第一個為0

text:所選擇的Spinner存在的文本

這個API傳回的是一個布爾值,當滿足條件時傳回true

pressSpinnerItem 擁有兩個參數

public void pressSpinnerItem(int spinnerIndex, int itemIndex)

spinnerIndex:定位要使用的Spinner,第一個為0

intemIndex:定位所要按下的下拉項,如圖:北京=0 上海=1 天津=2 ....

繼續閱讀