Robotium代碼解析:[java] /* * This is an example test project created in Eclipse to test NotePad which is a sample * project located in AndroidSDK/samples/android-11/NotePad * * * You can run these test cases either on the emulator or on device. Right click * the test project and select Run As --> Run As Android JUnit Test * * @author Renas Reda, renas.reda@jayway.com * */ package com.jayway.test; import com.example.android.notepad.NotesList; import com.jayway.android.robotium.solo.Solo; import android.test.ActivityInstrumentationTestCase2; public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList>{ <span style="white-space:pre"> </span>private Solo solo; <span style="white-space:pre"> </span>public NotePadTest() { <span style="white-space:pre"> </span>super(NotesList.class); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void setUp() throws Exception { <span style="white-space:pre"> </span>//setUp() is run before a test case is started. <span style="white-space:pre"> </span>//This is where the solo object is created. <span style="white-space:pre"> </span>solo = new Solo(getInstrumentation(), getActivity()); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void tearDown() throws Exception { <span style="white-space:pre"> </span>//tearDown() is run after a test case has finished. <span style="white-space:pre"> </span>//finishOpenedActivities() will finish all the activities that have been opened during the test execution. <span style="white-space:pre"> </span>solo.finishOpenedActivities(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void testAddNote() throws Exception { <span style="white-space:pre"> </span>solo.clickOnMenuItem("Add note"); <span style="white-space:pre"> </span>//Assert that NoteEditor activity is opened <span style="white-space:pre"> </span>solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); <span style="white-space:pre"> </span>//In text field 0, add Note 1 <span style="white-space:pre"> </span>solo.enterText(0, "Note 1"); <span style="white-space:pre"> </span>solo.goBack(); <span style="white-space:pre"> </span>//Clicks on menu item <span style="white-space:pre"> </span>solo.clickOnMenuItem("Add note"); <span style="white-space:pre"> </span>//In text field 0, add Note 2 <span style="white-space:pre"> </span>solo.enterText(0, "Note 2"); <span style="white-space:pre"> </span>//Go back to first activity named "NotesList" <span style="white-space:pre"> </span>solo.goBackToActivity("NotesList"); <span style="white-space:pre"> </span>//Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/". <span style="white-space:pre"> </span>solo.takeScreenshot(); <span style="white-space:pre"> </span>boolean expected = true; <span style="white-space:pre"> </span>boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2"); <span style="white-space:pre"> </span>//Assert that Note 1 & Note 2 are found <span style="white-space:pre"> </span>assertEquals("Note 1 and/or Note 2 are not found", expected, actual); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void testEditNote() throws Exception { <span style="white-space:pre"> </span>// Click on the second list line <span style="white-space:pre"> </span>solo.clickInList(2); <span style="white-space:pre"> </span>// Change orientation of activity <span style="white-space:pre"> </span>solo.setActivityOrientation(Solo.LANDSCAPE); <span style="white-space:pre"> </span>// Change title <span style="white-space:pre"> </span>solo.clickOnMenuItem("Edit title"); <span style="white-space:pre"> </span>//In first text field (0), add test <span style="white-space:pre"> </span>solo.enterText(0, " test"); <span style="white-space:pre"> </span>solo.goBack(); <span style="white-space:pre"> </span>boolean expected = true; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>// (Regexp) case insensitive <span style="white-space:pre"> </span>boolean actual = solo.waitForText("(?i).*?note 1 test"); <span style="white-space:pre"> </span>//Assert that Note 1 test is found <span style="white-space:pre"> </span>assertEquals("Note 1 test is not found", expected, actual); <span style="white-space:pre"> </span>solo. <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void testRemoveNote() throws Exception { <span style="white-space:pre"> </span>//(Regexp) case insensitive/text that contains "test" <span style="white-space:pre"> </span>solo.clickOnText("(?i).*?test.*"); <span style="white-space:pre"> </span>//Delete Note 1 test <span style="white-space:pre"> </span>solo.clickOnMenuItem("Delete"); <span style="white-space:pre"> </span>//Note 1 test & Note 2 should not be found <span style="white-space:pre"> </span>boolean expected = false; <span style="white-space:pre"> </span>boolean actual = solo.searchText("Note 1 test"); <span style="white-space:pre"> </span>//Assert that Note 1 test is not found <span style="white-space:pre"> </span>assertEquals("Note 1 Test is found", expected, actual); <span style="white-space:pre"> </span>solo.clickLongOnText("Note 2"); <span style="white-space:pre"> </span>//Clicks on Delete in the context menu <span style="white-space:pre"> </span>solo.clickOnText("Delete"); <span style="white-space:pre"> </span>actual = solo.searchText("Note 2"); <span style="white-space:pre"> </span>//Assert that Note 2 is not found <span style="white-space:pre"> </span>assertEquals("Note 2 is found", expected, actual); <span style="white-space:pre"> </span>} } 1、在Robotium工程中需要引用用robotium-solo-3.6.jar包。[java] public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList> 2、繼承類ActivityInstrumentationTestCase2<NotesList>,並在 測試工程中定義一個solo對象,使用此對象做自動化操作。[java] private Solo solo; 3、在構造方法中指定被測程式中的主Activity,即被測項目原始碼中查看AndroidManifest.xml檔案中註冊的activity的action屬性為:<activity android:name="NotesList" android:label="@string/title_notes_list"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>在這裡對應的是NotePad項目中的NotePad類。[java] public NotePadTest() { super(NotesList.class); } 4、setUp( )方法是在自動化操作前的做一下配置。[java] @Override public void setUp() throws Exception { solo = new Solo(getInstrumentation(), getActivity()); } 5、tearDown()方法在測試案例已經執行完成時,關閉所有在測試過程中的活動事件。[java] @Override public void tearDown() throws Exception { solo.finishOpenedActivities(); } 6、常用操作包括點擊、長按、滾動螢幕、設定等待時間等操作。[java] public void testAddNote() throws Exception { <span> </span>solo.clickOnMenuItem("Add note"); <span> </span>//Assert that NoteEditor activity is opened <span> </span>solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); <span> </span>//In text field 0, add Note 1 <span> </span>solo.enterText(0, "Note 1"); <span> </span>solo.goBack(); <span> </span>//Clicks on menu item 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"); //Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/". solo.takeScreenshot(); 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); } 7、assertEquals( )驗證實際結果和期望結果,如果實際結果和期望結果不一致,則該測試案例執行失敗,並停止執行該用例。例子中給出的是三個用例組成的測試集,當其中一個測試案例執行失敗,不影響其他測試案例執行。Junit測試結果中給出了詳細的執行結果以及執行失敗原因的提示。 以上是對Android Robotium自動化測試載入器的基本解析,大家可以調試體驗其他的一些功能。