In the Android testing tool, Monkey Runner only needs a few simple commands, but its limitations are:Pixel-based operations are not portable.
The automated test tool compiled by uiautomator can be used to select and directly operate uiObject Based on UiSelector.Property-based operation portability Testing.
Note:
The official android version conditions are as follows. If you are earlier than these versions, download the new version first.
Android SDK Tools, Revision 21 or higher
Android SDK Platform, API 16 or higher
1. First, let's take a look at attribute operations and use uiautomatorviewer to analyze the application UI components.
Tool Path:/tools /.
The current screenshot is displayed on the left, and a red floating box is displayed on the top to indicate the selected node. The selected node is highlighted in blue in the upper right corner, and the attribute of this view is displayed in the lower right corner.
As to what the functions are, let's look at it later (please pay attention to the content-desc attribute and we will use it later ).
2. Open eclipse and create a new one.Java Project,Here I create MessageTestCase
After 2.1, right-click the project Property-> Java Build Path-> Libraries-> Add Library-> JUnit-> JUnit3-> Finish
2.2 go back to the Property page and choose Libraries-> Add External JARs-> Android SDK path \ platforms \ android-**-> Android. java and uiautomator. jar.
Click OK after 2.3 to close the Property Window
3. Edit the code to complete the operation. The main operation is to enter the text message interface and edit a hello world
// All UI automatic tests must inherit UiAutomatorTestCasepublic class MessageTestCase extends UiAutomatorTestCase {public void testDemo () throws UiObjectNotFoundException {// obtain the UiDevice object UiDevice device = getUiDevice (); // click the home key to operate the device. pressHome (); // As mentioned in, we use the content-desc attribute Apps to select all application lists. UiObject appsTab = new UiObject (new UiSelector (). description ("Apps"); appsTab. click (); // because the text message interface is on the second list page, you can slide the page first. // use the scrollable attribute to select the slide viewU. IScrollable appViews = new UiScrollable (new UiSelector (). scrollable (true); appViews. setAsHorizontalList (); appViews. scrollForward (); // locate the Text message application icon through the class name and Text, and obtain the Text through uiautomatorviewerUiObject messageApp = appViews. getChildByText (new UiSelector (). className (android. widget. textView. class. getName (), "Messaging"); // click and wait for the messageApp to be opened. clickAndWaitForNewWindow (); // select create SMS UiObject newMessage = new UiObject (ne W UiSelector (). description ("New message"); newMessage. click (); // select the Edit information UiObject editMessage = new UiObject (new UiSelector (). text ("Type text message"); editMessage. click (); editMessage. setText ("hello world! ");}}
4. then compile and release the uiautomator test.
4.1 go to the sdk tools directory and run the following command:
Android. bat create uitest-project-nMessageTestCase-T 1-p"C: \ Documents ents and Settings \ *** \ workspace \ MessageTestCase"
MessageTestCase indicates the project name, and the second path indicates the project path.
A build. xml file is generated in the path of the project.
4.2 Use the following command to set Android Home
Set ANDROID_HOME =
4.3 then download ant and set ant as the environment variable (you can also leave it unspecified, but add the path before using this command)
First switch the path to the directory where the project is located, and then use the following command to compile
Apache-ant-1.9.3 \ bin \ ant build
Then, generate the. jar file in the bin directory of the project.
4.4 push the jar file to a directory, such
Adb push /Data/local/tmp
4.5 run the test
Adb shell uiautomator runtest MessageTestCase. jar-c com. wjh. test. MessageTestCase