Android instrumentation to determine the activity after the jump

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.