Instrumentation for Android Development (Automated Testing)

Source: Internet
Author: User

In addition to JUnit, Android provides an Instrumentation testing framework. You can use Instrumentation to simulate press, lift, screen click, scroll, and other events to effectively control the Activity for automated testing.

Instrumentation is the base class for executing the application instrumentation code. When the application is running, instrumentation is enabled. Instrumentation is initialized before any application is running. It can be used to monitor the interaction between the system and the application.

1. Description in Manifest. xml:

<Instrumentation

Android: name = "android. test. InstrumentationTestRunner"

Android: label = "Tests for My App"

Android: targetPackage = "com. my. test"/>

<Span style = "font-size: 18px;"> <uses-permission android: name = "android. permission. INJECT_EVENTS"/> </span>

 

2. Analog sending buttons:

/**

* Enter the required key value.

* @ Param keyCode

*/

Private void sendKeyCode (final int keyCode ){

New Thread (){

Public void run (){

Try {

Instrumentation inst = new Instrumentation ();

Inst. sendKeyDownUpSync (keyCode );

} Catch (Exception e ){

Log. e ("Exception when sendPointerSync", e. toString ());

}

}

}. Start ();

}

}

 

3. Simulate mouse events:

New Thread (){

Public void run (){

Try {

Instrumentation inst = new Instrumentation ();

Inst. sendPointerSync (MotionEvent. obtain (SystemClock. uptimeMillis (),

SystemClock. uptimeMillis (),

MotionEvent. ACTION_DOWN, 240,400, 0 ));

Inst. sendPointerSync (MotionEvent. obtain (SystemClock. uptimeMillis (),

SystemClock. uptimeMillis (),

Motionevents. ACTION_UP, 240,400, 0 ));

} Catch (Exception e ){

Log. e ("Exception when sendPointerSync", e. toString ());

}

}

}. Start ();

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.