Robotium第一个例子(NotePad)
3.导入被测试源码
打开Eclipse,点击File,选择Import,然后按照下图选择
Import窗口,选择Existing Projects into Workspace
按下图,找到下载下来的源码压缩包,导入Eclipse
删除原始APK
重新编译生成新的APK
Project----Clena
选中需要重新编译的APK 点OK
选择工程点Run as----Android Application
运行之后会在BIN 目录重新生成一个APK
4.新建测试工程
File-New-Project
Android----Android Test Project
按红色框框选择
在测试工程目录下新建Lib 目录
复制Robotium,jar jxl.jar ojdbc.jar 到Lib 目录下
成功后如下图
其它配置
选择测试工程----Build Path---Configure Build Path
一定要在 Order And Export 中将新加入的jar包打勾,不然在测试过程中会报Solo 异常
点击测试工程,确认以下两个属性配置是否正确
1:Android 的版本信息
选中测试工程,右键-Properties
2: 字体格式----UTF-8
3:签名文件
在测试工程中的SRC 目录下新建一个包
Src---new---package
包名:com.robotium.test
将NotePadTest.java copy 到com\robotium\test 目录下
/* * 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@gmail.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>{ private Solo solo; public NotePadTest() { super(NotesList.class); } @Override public void setUp() throws Exception { //setUp() is run before a test case is started. //This is where the solo object is created. solo = new Solo(getInstrumentation(), getActivity()); } @Override public void tearDown() throws Exception { //tearDown() is run after a test case has finished. //finishOpenedActivities() will finish all the activities that have been opened during the test execution. solo.finishOpenedActivities(); } public void testAddNote() throws Exception { solo.unlockScreen(); 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); } 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.goBack(); boolean expected = true; // (Regexp) case insensitive boolean actual = solo.waitForText("(?i).*?note 1 test"); //Assert that Note 1 test is found assertEquals("Note 1 test is not found", expected, actual); } 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("Delete"); actual = solo.searchText("Note 2"); //Assert that Note 2 is not found assertEquals("Note 2 is found", expected, actual); } }
修改AndroidManifest.xml
配置完就可以用启动测试工程了
Run as ---Android Junit Test (注意这里需要选 Junit)
如果一切顺利的话画面就是这样的