Android Uiautomator Instance Use

Source: Internet
Author: User

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?
  1. All UI automated tests are inherited uiautomatortestcase
  2. Public class Messagetestcase extends Uiautomatortestcase {
  3. public void Testdemo () throws uiobjectnotfoundexception{
  4. //Get Uidevice object
  5. Uidevice device = Getuidevice ();
  6. //Click home button to operate
  7. Device.presshome ();
  8. //As mentioned in, we use CONTENT-DESC properties apps to select all Apps list
  9. UiObject appstab = new UiObject (new Uiselector (). Description ("Apps"));
  10. Appstab.click ();
  11. //Because the SMS interface is on the second list page, so we can first swipe
  12. //Select slide view by scrollable property
  13. Uiscrollable appviews = new Uiscrollable (new Uiselector (). Scrollable (true));
  14. Appviews.setashorizontallist ();
  15. Appviews.scrollforward ();
  16. //Find SMS App icon via class name and text, text gets through Uiautomatorviewer
  17. UiObject Messageapp = Appviews.getchildbytext (new Uiselector (). ClassName (Android.widget.TextView.   Class.getname ()), "Messaging");
  18. //Tap and wait for the SMS app to open
  19. Messageapp.clickandwaitfornewwindow ();
  20. //Select new text message
  21. UiObject newmessage = new UiObject (new Uiselector (). Description ("new Message"));
  22. Newmessage.click ();
  23. //Select edit information
  24. UiObject editmessage = new UiObject (new Uiselector (). Text ("Type text message"));
  25. Editmessage.click ();
  26. Editmessage.settext ("Hello world!");
  27. }
  28. }

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

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.