Why Choose Uiautomatoras a pro-son Uiautomator with Android, with the Android version of the synchronization upgrade, after several iterations are now quite stable. compared to the Monkeyrunner,uiautomator interface is rich and easy to use, can support all Android event operations, event operations do not depend on the control coordinates, can be asserted and verified correctness, very suitable for UI testing. Uiautomator does not require testers to understand the code implementation details, which are functional and black-box testing. The test code is simple in structure, easy to write and low in learning curve. Based on Java, a single compilation can run on all Android devices.
Note: Uiautomator is not suitable for OpenGL and HTML-based programs, because such programs do not use the Android view system.
Preparation for the use of Uiautomator
1. First introduce the UI tool for the Uiautomator test framework: UiautomatorviewerUiautomatorviewer is located in the Sdk/tools directory, you can scan, analyze the application interface to be tested, analysis results can be exported to XML and. This tool allows you to analyze various properties, such as id,text,focusable of UI controls, and even hierarchical relationships on the layout.
can be started in Ddms, as follows:
Uiautomatorviewer, the left side is the current screen of the phone, the upper right side of the view control hierarchy, the lower part of the current selected control of the various information. Select a component on the left-hand screen to see the properties of the control, and when we write the script, we can find the control based on the corresponding property information and then manipulate it, or according to some attributes, such as whether it can be clicked, etc. ~
2.uiautomator IntroductionUiautomator is a jar package that contains a set of UI test APIs and supports running test programs. The jar package is located in Sdk/platforms/android-*/uiautomator.jar.You need to be aware that your SDK version needs to be greater than 21 and that the SDK tools version needs to be larger. the use of Uiautomator is actually very simple, depending on the property to find the control, and then manipulate the control, presumably like:
Uiautomator Steps to useHere is an example of the use of uiautomator. In this example we will impersonate the app login and exit. take eclipse as an example:1. Create a Java projectFile-->new-->java Project Enter a project name, such as Messagetsetcase. Click Finish
2. Add the necessary jar packagesin Project Explorer, right-click the project you just created and select Properties-->java build path to add the following jar packageA. Select the Add library-->junit select Junit3B. Select Add External JARS, select Uiautomator.jar and Android.jar in the sdk/platforms/android-*/directory, * Please use the latest version downloaded from the path below.
3.file-->new-->class Create a new file.
4. Writing test cases, you can usually write a separate method for a test case:
Package Com.iflytek.test;import Com.android.uiautomator.core.uiobject;import Com.android.uiautomator.core.uiobjectnotfoundexception;import Com.android.uiautomator.core.uiselector;import Com.android.uiautomator.testrunner.uiautomatortestcase;public class Messagetestcase extends UiAutomatorTestCase { public void Testdemo () throws Uiobjectnotfoundexception {//Analog HOME key Click event Getuidevice (). Presshome (); Locate the Apps tab button UiObject Appstab = new UiObject (new Uiselector (). Text ("Smart community")); Appstab.click (); Press the back key to Exit Getuidevice (). Pressback (); Getuidevice (). Pressback (); Getuidevice (). Pressback (); Getuidevice (). Pressback (); Sleep (2000); Open the App if (Appstab.exists ()) {Appstab.clickandwaitfornewwindow (); Sleep (5000);} Go to my page UiObject mytab = new UiObject (new Uiselector (). Text ("my")); Mytab.clickandwaitfornewwindow (); Set the number of cycles for (int i =20; i > 0; i--) {//Enter login page UiObject login = new UiObject (new Uiselector (). resourceId ("com . Iflytek.smartzone:id/mine_login_layout ")); if (login.exists ()) {Login.clickandwaitfornewwindow (); Enter user name password UiObject username = new UiObject (new Uiselector (). ResourceId ("Com.iflytek.smartzone:id/login_edituser")) ; Username.cleartextfield (); Username.settext ("18019900009"); UiObject passwd = new UiObject (new Uiselector (). ResourceId ("Com.iflytek.smartzone:id/login_editpassword")); Passwd.cleartextfield (); Passwd.settext ("111111"); Click the login button UiObject login = new UiObject (new Uiselector (). Text ("login")); Login.clickandwaitfornewwindow ();} Log out UiObject MyInfo = new UiObject (new Uiselector (). ResourceId ("Com.iflytek.smartzone:id/mine_head_layout")); Myinfo.click (); UiObject loginout = new UiObject (new Uiselector (). ResourceId ("Com.iflytek.smartzone:id/persOn_info_exitlogin ")); Loginout.click (); UiObject OKBT = new UiObject (new Uiselector (). Text ("Confirm exit")); Okbt.clickandwaitfornewwindow (); } }}
5. Compiling test Casesbefore compiling, we need to generate Build.xml, which is generated as follows:
<android-sdk><name><path>
where <name> is the test project name that contains the Uiautomator test source file,<path> is the path to the corresponding test project directory. the parameter after-T is the target value of the Android version in the current SDK, and typically we will download multiple versions of Android platform in one SDKYou can view it by following the commands:
<android-sdk> /tools/android List
The output is as follows:View Plaincopy to Clipboardprint?
- </Pre><Pre name="Code" class="HTML">Available android targets:----------id: 1 or "android-16" Name: Android 4.1.2 Type: Platform api level: 16 revision: 4 skins : wqvga400, wsvga, wxga800, hvga, wvga854, qvga, wvga800 (default), wqvga432, wxga720, wxga800-7in tag/abis : default/armeabi-v7a----------ID: 2 or "Android-19" Name: Android 4.4.2 Type: Platform API level: 19 &NBSP;REVISION:&NBSP;3&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SKINS:&NBSP;WQVGA400,&NBSP;WSVGA,&NBSP;WXGA800,&NBSP;HVGA, WVGA854, QVGA, WVGA800 (default), wqvga432, wxga720, wxga800-7in tag/ abis : default/armeabi-v7a----------id: 3 or "android-20" Name: Android 4.4w type: platform api level: 20 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;REVISION:&NBSP;1&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SKINS:&NBSP;WQVGA400,&NBSP;WSVGA, WXGA800, HVGA, WVGA854, QVGA, WVGA800 (default), wqvga432, wxga720, WXGA800-7in, AndroidWearRound, AndroidWear Square, AndroidWearRound, androidwearsquare tag/abis : android-wear/armeabi-v7a, android-wear/x86
this should be consistent or updated with the previously added library selection, so-t after I wrote 1.
after running the output is as follows:
Updated file D:\Users\qxb-810\workspace\MessageTestCase\build.xml
Here , Build.xml has been generated, and below we go into the project directory to compile:
Ant Build
then copy the jar file to the phone's /data/local/tmp/directory:
ADB push d:\users\qxb-810\workspace\messagetestcase\bin\messagetestcase.jar/data/local/tmp
Finally, the case can be executed on the phone:
adb shell Uiautomator runtest messagetestcase.jar-c com.iflytek.test.MessageTestCase
Getting Started with Android automated test-uiautomator