Test function points
- Click on the main interface
MainActivity
button Button
to jump to another interfaceOtherActivity
Test method
- Use
Instrumentation
, perform a Click button action, determine whether the activity of the interface after the jump is expected activity
- Use
ActivityMonitor
, this class is used to monitor a single activity in an application, monitoring some specified intent.
- Once the instance is created,
ActivityMonitor
the instance is added by calling the Instrumentation.addMonitor
method, and when the target activity starts, the system matches the list of Activitymonitor instances in the instrumentation, and if it matches, it accumulates the counter
Test code
PackageCom.example.demo.test;Importcom.example.demo.MainActivity;ImportAndroid.app.Instrumentation.ActivityMonitor;ImportAndroid.test.ActivityInstrumentationTestCase2;ImportAndroid.widget.Button; Public class mainactivitytest extendsActivityInstrumentationTestCase2< Mainactivity> { PrivateMainactivity mactivity;PrivateButton button; Public mainactivitytest() {Super(Mainactivity.class); }@Override protected void setUp()throwsException {Super. SetUp ();//Start the application under test and open the activity to be testedMactivity = Getactivity (); Button = (button) mactivity. Findviewbyid (Com.example.demo.r.id.button); }//Test button Public void Testbutton() {//The activity name of the interface after the jump is named Com.example.demo.OtherActivityActivitymonitor am = getinstrumentation (). Addmonitor ("Com.example.demo.OtherActivity",NULL,false);//Click operation in the thread of the application under testMactivity.runonuithread (NewRunnable () {@Override Public void Run() {Button.performclick (); } });//Set the activity to wait for the requirement to be created successfully, wait up to 5sAm.waitforactivitywithtimeout ( the);//Activity created successfully, Am.gethits () value is 1, otherwise 0Assertequals (1, Am.gethits ()); }}
Android instrumentation to determine the activity after the jump