Robotium environment Construction and beginner's introductory tutorial
- Preparation tool: Robotium data download
- Knowledge Preparation:
- Java fundamentals, such as basic data structures, syntax structures, classes, inheritance, and more
- Familiarity with Android, understanding four components, writing simple Android apps
- Familiarize yourself with the Eclipse IDE use method
- Have enough patience with the spirit of exploration, encounter problems can be skilled Google
- Install JDK, configure environment variables, if not please Google
- Download and install the Android SDK and update
- Download Eclipse and install the ADT plugin, integrated with the Android SDK
- Download the Robotium official example and the latest JAR package
- Open Eclipse Import tested project
Click to view larger image
- Create a test project: Select Android Test Project and click Next
Click to view larger image
- Enter the project name and click Next
Click to view larger image
- Select the project Notepad to be tested and click Finish
Click to view larger image
- Create a new Lib folder in the test project
Click to view larger image
- Copy the downloaded Robotium jar package into Lib and bring the jar package to the project in Build path
Click to view larger image
- Open the Manifest.xml file in the test project and modify the following
Click to view larger image
- Create a new JUnit test case, select JUNIT4, enter the class name, and click Done
Click to view larger image
- Open the newly created test class, modify the inheritance Activityinstrumentationtestcase2<noteslist>, create a constructor method, and write the test code, specifically
Click to view larger image
01 |
packagecom.example.android.notepad.test; |
03 |
importandroid.test.ActivityInstrumentationTestCase2; |
05 |
importcom.example.android.notepad.NotesList; |
06 |
importcom.jayway.android.robotium.solo.Solo; |
08 |
publicclassNotePadTest extendsActivityInstrumentationTestCase2 |
10 |
privateSolo solo;//声明Solo |
11 |
publicNotePadTest()//构造方法 |
13 |
super(NotesList.class); |
18 |
publicvoid setUp() throwsException |
20 |
solo = newSolo(getInstrumentation(), getActivity()); |
24 |
publicvoid tearDown() throwsException |
26 |
solo.finishOpenedActivities(); |
29 |
publicvoid testAddNote() throwsException |
32 |
solo.clickOnMenuItem("Add note"); |
34 |
solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); |
36 |
solo.enterText(0, "Note 1"); |
40 |
solo.clickOnMenuItem("Add note"); |
42 |
solo.enterText(0, "Note 2"); |
43 |
//返回NotesList activity |
44 |
solo.goBackToActivity("NotesList"); |
46 |
solo.takeScreenshot(); |
47 |
booleanexpected = true; |
48 |
booleanactual = solo.searchText("Note 1") ; solo.searchText("Note 2"); |
49 |
assertEquals("Note 1 and/or Note 2 are not found", expected, actual); |
- After writing, open the emulator or connect to the real machine, right-click the test class, select Android JUnit test
Click to view larger image
- Eclipse switches to the JUnit interface, where the emulator (the real machine) automatically executes the test and the results are visible through the left interface
Click to view larger image
- A simple test is over. If you do not have a problem, then congratulate you continue to learn further, if you have problems, do not hurry ~ Please make sure that you do not have any errors in the process, and then I list the errors you may encounter, if you have not resolved please Google or contact us
1, the most common: Java.lang.NoClassDefFoundError:com.jayway.android.robotium.solo.Solo solution: After operation and clean project
Android Automation Test (2) Robotium Environment Building and beginners tutorial