1. Project tested:
Create an android project: D_session; it has an activity: D_sessionActivity; package name: com. mysession
Ii. Test Project:
Create a test project: D_sessionTest. The type is android test project;
1. menifest file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. mysession. test"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "8"/>
<Instrumentation
Android: name = "android. test. InstrumentationTestRunner"
Android: targetPackage = "com. mysession"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Uses-library android: name = "android. test. runner"/>
</Application>
</Manifest>
2. A base activity defines the actions of various simulated testers and judges the test results. Each test class inherits this class:
Package com. mysession. test;
Import android. app. Activity;
Import android. app. Instrumentation;
Import android. app. Instrumentation. ActivityMonitor;
Import android. content. Intent;
Import android. test. InstrumentationTestCase;
Import android. test. TouchUtils;
Import android. widget. Button;
Import android. widget. TextView;
Import com. mysession. D_sessionActivity;
Public class SessionActivityTest extends InstrumentationTestCase {
Private Instrumentation mInstrumentation;
Private ActivityMonitor mSessionMonitor;
Private Activity mCurrentActivity, mSessionActivity;
Private String TextNotEqual = "text not equal .";
Private static final String PackageName = "com. mysession ";
@ Override
Protected void setUp () throws Exception {
// Initialization
Super. setUp ();
If (mInstrumentation = null ){
MInstrumentation = getInstrumentation ();
}
MSessionActivity = null;
}
@ Override
Protected void tearDown () throws Exception {
Super. tearDown ();
// Release resources
CloseActivity (mSessionActivity );
MCurrentActivity = null;
}
Private void closeActivity (Activity activity ){
If (activity! = Null ){
Activity. finish ();
Activity = null;
}
}
Public void openSessionActivity (){
// Open the session activity
Try {
SetUp ();
} Catch (Exception e ){
E. printStackTrace ();
}
MSessionMonitor = mInstrumentation. addMonitor (
D_sessionActivity.class.getName (), null, false );
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Intent. setClassName (PackageName, D_sessionActivity.class.getName ());
MInstrumentation. startActivitySync (intent );
MSessionActivity = getInstrumentation (). waitForMonitor (mSessionMonitor );
AssertNotNull (mSessionActivity );
MCurrentActivity = mSessionActivity;
}
// Determine whether the text is correct
Public void assertTextEqual (int resId, String strText ){
TextView textView = (TextView) mCurrentActivity. findViewById (resId );
AssertNotNull (textView );
AssertEquals (TextNotEqual,
StrText,
TextView. getText (). toString ());
};
// Simulate Button clicking event
Public void clickButton (int resId ){
Button button = (Button) mCurrentActivity. findViewById (resId );
AssertNotNull (button );
TouchUtils. clickView (this, button );
}
}
3. Various test classes:
Test Class 1:
Package com. mysession. test. cases;
Import com. mysession. R;
Import com. mysession. test. SessionActivityTest;
Public class MyCase1 extends SessionActivityTest {
Public void testCase1 (){
OpenSessionActivity ();
AssertTextEqual (R. id. etUrl,
"Http: // 172.20.230.5/iportal/samples/jsapi/mobile.html ");
}
Public void testCase3 (){
OpenSessionActivity ();
ClickButton (R. id. btnLoad );
ClickButton (R. id. btnHistory );
}
}
Test Class 2:
Package com. mysession. test. cases;
Import com. mysession. R;
Import com. mysession. test. SessionActivityTest;
Public class MyCase2 extends SessionActivityTest {
Public void testCase2 (){
OpenSessionActivity ();
ClickButton (R. id. btnLoad );
}
}
3. Some actions (such as clicking menu) need to be done through the package robotium-solo-1.8.0.jar.
Therefore, in Build Path-> Configure Build Path... Import in: robotium-solo-1.8.0.jar
Program:
Private Solo solo;
Solo = new Solo (getInstrumentation (), getActivity );
You can use solo, for example:
Solo. clickOnMenuItem (text );
Solo. goBack ();
From fhy_2008