Android的自動化的測試研究–Robotium

來源:互聯網
上載者:User

http://www.devdiv.com/home.php?mod=space&uid=14682&do=blog&id=5779

 

Robotium的jar包和程式例子地址:

http://code.google.com/p/robotium/

      Android的自動化的測試研究,我相信有很多在接觸android的人當中,也在網上不斷的搜尋和研究究竟有沒有能應用android自動化測試手段,我也是如此,經過一番尋找研究有幸接觸到了Robotium這個東東,他能一定程度上給我們一些想要的效果,現將小試牛刀的一些東東分享出來.

  

 

Robotium is a test framework created to make it easy to write powerful and  robust automatic black-box test cases for Android applications. With the support  of Robotium, test case developers can write function, system and acceptance test  scenarios, spanning
multiple Android activities.

Robotium has full support for Activities, Dialogs, Toasts, Menus and Context  Menus.

上邊的一段英文是官方一段文字,也就是我們利用Robotium可以做一些對android的一些自動化測試工作,並且他是可跨越多個Activites,且容易寫一些測試case的,一定程度上能做一些黑盒的自動化工作,在這裡先簡單跑一個notpad的例子,看看跑起來的效果是什麼樣的,後續繼續做些更詳細的。

 

一、確保環境

1.已經安裝eclipse

2.Eclipse已經配置好android相關環境

3.存在已經建立模擬器

二、步驟

1.啟動Eclipse執行 New --> Project --> Android Project --> Create Project from existing sample --> NotePad將內建的例子匯入進來.

2.在官方down欄下下載三個檔案,兩個是所需要的jar包,另一個是notepad的測試例子

(當前最新的是2.5Featured,這裡就不提供下載串連了版本更新的還是比較快的)

3.然後執行File --> Import --> Existing Project into workspace --> Select archive file --> ExampleTestProject_v2.5.zip將例子匯入進來(如果錯誤可能需要你在這個項目上將剛才說需要的兩個jar匯入project --> Properties --> Java Build Path --> Add (external) Jar)

4.右鍵選擇剛才匯入的test例子執行Run As --> Run As Android JUnit Test.

如果沒有意外的話會自動啟動模擬器並且運行起來了.

http://www.devdiv.com/data/attachment/album/201110/11/155452zmygarwywa9e9zwv.png

再欣賞一下代碼:

public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList>{

private Solo solo;

public NotePadTest() {
  super("com.example.android.notepad", NotesList.class);

}

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

@Smoke
public void testAddNote() throws Exception {
  solo.clickOnMenuItem("Add note");
  //Assert that NoteEditor activity is opened
  solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor");
  //In text field 0, add Note 1
  solo.enterText(0, "Note 1");
  solo.goBack();
  //Clicks on menu item
  solo.clickOnMenuItem("Add note");
  //In text field 0, add Note 2
  solo.enterText(0, "Note 2");
  //Go back to first activity named "NotesList"
  solo.goBackToActivity("NotesList");
// solo.
  boolean expected = true;
  boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2");
  //Assert that Note 1 & Note 2 are found
  assertEquals("Note 1 and/or Note 2 are not found", expected, actual);

}

@Smoke
public void testEditNote() throws Exception {
  // Click on the second list line
  solo.clickInList(2);
  // Change orientation of activity
  solo.setActivityOrientation(Solo.LANDSCAPE);
  // Change title
  solo.clickOnMenuItem("Edit title");
  //In first text field (0), add test
  solo.enterText(0, " test"); 
  solo.goBackToActivity("NotesList");
  boolean expected = true;
  // (Regexp) case insensitive
  boolean actual = solo.searchText("(?i).*?note 1 test");
  //Assert that Note 1 test is found
  assertEquals("Note 1 test is not found", expected, actual);

}

@Smoke
public void testRemoveNote() throws Exception {
  //(Regexp) case insensitive/text that contains "test"
  solo.clickOnText("(?i).*?test.*");
  //Delete Note 1 test
  solo.clickOnMenuItem("Delete");
  //Note 1 test & Note 2 should not be found
  boolean expected = false;  
  boolean actual = solo.searchText("Note 1 test");
  //Assert that Note 1 test is not found
  assertEquals("Note 1 Test is found", expected, actual); 
  solo.clickLongOnText("Note 2");
  //Clicks on Delete in the context menu
  solo.clickOnText("(?i).*?Delete.*"); 
  actual = solo.searchText("Note 2");
  //Assert that Note 2 is not found
  assertEquals("Note 2 is found", expected, actual); 
}

@Override
public void tearDown() throws Exception {
  try {
   //Robotium will finish all the activities that have been opened
   solo.finalize(); 
  } catch (Throwable e) {
   e.printStackTrace();
  }
  getActivity().finish();
  super.tearDown();
}
}

 

 

/**
* @author 張興業
* 郵箱:xy-zhang@163.com
* qq:363302850
*
*/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.