Reprinted from: http://blog.csdn.net/huiguixian/article/details/22398193
Android test tools, Monkey runner as long as a few simple instructions, but his limitations are based on pixel location operations, not portability .
The automated test tools written by Uiautomator can be implemented based on the Uiselector Select Direct Operation UIObject to write a test based on the portability of the attribute Operation .
Note:
Android official version conditions are as follows, if below these versions please download the new version first
Android SDK Tools, Revision or higher
Android SDK Platform, API, or higher
1. First let's look at the property operation and use Uiautomatorviewer to analyze the application UI component.
Tool Path: <android-sdk>/tools/.
On the left is the current screenshot, moving to the top has a red hover box indicating the currently selected node, the currently selected will be in the upper right corner of the blue highlight, the lower right corner of the view is the property.
As for what this does, we'll look at it later (note the Content-desc attribute, which we'll use later).
2. Open Eclipse, create a new Java Project, I'm building a messagetestcase
After 2.1 Right-click Project Property->java Build path->libraries->add library->junit->junit3->finish
2.2 Back to the property interface or choose Libraries->add External jars-> Select the Android SDK path \platforms\android-**-> Check Android.java and Uiautomator.jar
Click OK after 2.3 to close the property window
3. After we edit code to complete the operation, the main operation is to enter the SMS interface, edit a Hello World
[Java]View Plaincopyprint?
- All UI automated tests are inherited uiautomatortestcase
- Public class Messagetestcase extends Uiautomatortestcase {
- public void Testdemo () throws uiobjectnotfoundexception{
- //Get Uidevice object
- Uidevice device = Getuidevice ();
- //Click home button to operate
- Device.presshome ();
- //As mentioned in, we use CONTENT-DESC properties apps to select all Apps list
- UiObject appstab = new UiObject (new Uiselector (). Description ("Apps"));
- Appstab.click ();
- //Because the SMS interface is on the second list page, so we can first swipe
- //Select slide view by scrollable property
- Uiscrollable appviews = new Uiscrollable (new Uiselector (). Scrollable (true));
- Appviews.setashorizontallist ();
- Appviews.scrollforward ();
- //Find SMS App icon via class name and text, text gets through Uiautomatorviewer
- UiObject Messageapp = Appviews.getchildbytext (new Uiselector (). ClassName (Android.widget.TextView. Class.getname ()), "Messaging");
- //Tap and wait for the SMS app to open
- Messageapp.clickandwaitfornewwindow ();
- //Select new text message
- UiObject newmessage = new UiObject (new Uiselector (). Description ("new Message"));
- Newmessage.click ();
- //Select edit information
- UiObject editmessage = new UiObject (new Uiselector (). Text ("Type text message"));
- Editmessage.click ();
- Editmessage.settext ("Hello world!");
- }
- }
4. Then compile and publish the Uiautomator test
4.1 Enter the SDK tools directory using the following command:
Android.bat Create Uitest-project-n messagetestcase -t 1-p "C:\Documents and Settings\***\workspace\ Messagetestcase"
Where Messagetestcase is the project name, the second path is the project's path
A build.xml file is then generated on the path where the project is located
4.2 Use the following command to set up Android Home
Set android_home=<path_to_your_sdk>
4.3 Then you need to download Ant and set ant as environment variable (or not, but use this command to precede the path)
First switch the path to the directory where the project is located, then compile with the following command
Apache-ant-1.9.3\bin\ant Build
The. jar file is then generated in the bin directory of the project.
4.4 Push jar files into a directory, such as
ADB push <path_to_output_jar>/data/local/tmp
4.5 Running the test
adb shell Uiautomator runtest messagetestcase.jar-c com.wjh.test.MessageTestCase
Android Uiautomator Instance Use