transferred from: http://www.cnblogs.com/rexmzk/archive/2012/12/26/2834380.html
Recently, the company is conducting automated testing of Android, and developers in the United States have built an Android Automation framework using Android's UI testing and PowerShell. Through a few days of study, probably familiar with their set of framework.
The new Android SDK provides 2 tools to automate the UI:
uiautomatorviewer-A GUI tool to scan the controls above the recognition UI
uiautomator-Java class library, which contains some custom UI functional tests to automate and run the test case engine.
These two tools are only in Android SDK tools, Revision or higher, and Android SDK Platform, API, or higher.
After you install the SDK, you can use the AVD to create a virtual machine or use USB to connect to the actual device for automation. Note: If you want to use real device automation, the Android version of the machine must be more than 4.1, and in the development mode of the USB debugging open.
When the AVD is created, or the device is connected to the computer via USB, you can check the ID of the device (this will be used later when you write the program, especially if you are connecting more than one device at a time)
<ANDROID-SDK>\PLATFORM-TOOLS>ADB devices
Today, the steps to build a UI testing test development environment are written down, which is also available for reference. Because I did not have any programming foundation before, so the content may be wrong. If found wrong, also hope that you can put forward, thank you.
1. Install the JDK, this direct Google search, you can. My current version is 6.0.
2. Setting Java_home Environment variables
3. Download and install the Android SDK
4. Download the latest SDK (version >21) via SDK Manager, Platform-tools (version >16)
5. Download the SDK Platform installed under Android 4.2 (API17). If you need to use the AVD (Android virtual machine), you can put the entire API down. (Note: only Android 4.1.2 (API 16) supports Uiautomator)
6. Setting Android_home Environment variables
7. Download Ant and set the environment variables for ant:: http://ant.apache.org/, the 1.7.0 version is downloaded in this article . Unzip to a directory (for example, E: "apache-ant-1.7.0") and configure the environment variable to be used.
Ant environment variable Settings
There are 2 main environment variables used by Windows ANT, Ant_home, PATH.
Set the ant_home point to the ant's installation directory.
Setup method:
Ant_home = d:/apache_ant_1.7.0
Will%ant_home%/bin; The%ant_home%/lib is added to the path of the environment variable.
Setup method:
PATH =%ant_home%/bin; %ant_home%/lib
8. Install eclipse-Create a new Java Project
9. Click Java project,menu-project-properties
Ten. Java Build path-libraries
One. Add External JARs ...-Add the Android.jar and Uiautomator.jar in ANDROID-SDK\PLATFORM\ANDROID-XX
Add Library ...-adding the Junit-junit3 class Library
13. Add a directory in SRC src/com/uia/example/my
14. Under My, add a junit Test case named Launchsettings
15. Puthttp://developer.android.com/tools/testing/testing_ui.htmlCopy paste to TestCase, save
16. Run cmd
17. Run the command:
-<android-sdk>/tools> Android Create uitest-project-n <name>-T 1-p <path> (<name> is Launchs Ettings, <path> is the directory where the project is located)
18. Switch to the directory where the project is located (the directory where the Build.xml file is located), run
-Ant Build
After completion, the Launchsettings.jar will be generated under/bin
19. Use the ADB push command to upload test case (Launchsettings.jar) to the device being tested
-<android-sdk>\platform-tools>adb push <path_to_output_jar> / data/local/tmp/ (<path_to_output_jar>:jar's file directory)
20. Finally, run the command, the sample test The case will run on the device.
- <android-sdk>\platform-tools>adb Shell Uiautomator runtest launchsettings.-c com....
For more information on Android UI Automation, you can view it online (http://developer.android.com/tools/testing/testing_ui.html)
Androidui Automated Test Tool-uiautomator